Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 165 lines (141 sloc) 3.167 kB
d271e02 @hxw [scripts] new methos of starting screen
hxw authored
1 #!/bin/sh
2 # -*- tcl -*- \
3 exec expect -f "$0" "$@"
4
5
6 set DefaultUser wr
7 set DefaultPort 22
8 set timeout 120
9
10
11 proc usage {arg} {
12 global argv0 DefaultUser DefaultPort
13 if {$arg != ""} {
14 puts "error: ${arg}"
15 }
e6a6b5f @hxw [scripts] Add start directory for screen
hxw authored
16 puts "usage: ${argv0} {--verbose} {--cd=dir} {--login=user} host{:port}"
d271e02 @hxw [scripts] new methos of starting screen
hxw authored
17 puts " default user = ${DefaultUser}"
18 puts " default port = ${DefaultPort}"
19 exit 1
20 }
21
22 if {[llength $argv] <= 0} {
23 usage "missing arguments"
24 }
25
26
27 # assumes the command will execute non-interactively
e6a6b5f @hxw [scripts] Add start directory for screen
hxw authored
28 proc probe {user host port dir} {
d271e02 @hxw [scripts] new methos of starting screen
hxw authored
29
30 spawn ssh -l $user -p $port $host
31
32 set rc 0
33 set ErrorList {}
34
35 expect {
36 -re {Offending[[:space:]]+.*in[[:space:]]+([^:]+):([[:digit:]]+)} {
37 lappend ErrorList [list $expect_out(1,string) $expect_out(2,string)]
38 exp_continue
39 }
40 "Are you sure you want to continue connecting (yes/no)?" {
41 exp_send "yes\r"
42 exp_continue
43 }
44 "Please type 'yes' or 'no':" {
45 exp_send "yes\r"
46 exp_continue
47 }
48 -re {[pP]assword:} {
49 puts "Set up ssh authorized_keys on remote for: ${user}@${host}"
50 set rc 1
51 }
52 "Permission denied (publickey,keyboard-interactive)." {
e6a6b5f @hxw [scripts] Add start directory for screen
hxw authored
53 puts "Set up ssh authorized_keys on remote for: ${user}@${host}"
d271e02 @hxw [scripts] new methos of starting screen
hxw authored
54 set rc 1
55 }
56 -re "(No route to host|hostname nor servname provided, or not known|Host key verification failed)" {
57 send_user "\nTerminating\r\n"
58 puts "Failed to connect to: ${host}:${port}"
59 set rc 1
60 }
e6a6b5f @hxw [scripts] Add start directory for screen
hxw authored
61 -ex {$} {
62 set rc 0
d271e02 @hxw [scripts] new methos of starting screen
hxw authored
63 }
64 timeout {
65 set rc 1
66 }
67 }
68
69 if {0 == $rc} {
e6a6b5f @hxw [scripts] Add start directory for screen
hxw authored
70 exp_send "cd '${dir}' && byobu-launcher\r"
d271e02 @hxw [scripts] new methos of starting screen
hxw authored
71 expect {
e6a6b5f @hxw [scripts] Add start directory for screen
hxw authored
72 -ex "Menu:" {
73 # send the power detach sequence
74 exp_send "\1DD"
d271e02 @hxw [scripts] new methos of starting screen
hxw authored
75 }
76 timeout {
77 set rc 1
78 }
79 }
80 }
81
82 if {0 == $rc} {
83 expect {
84 "closed" {
85 puts "screen started"
86 }
87 timeout {
88 set rc 1
89 }
90 }
91 }
92
93 catch {
94 close
95 } ErrorMessage
96
97 return $rc
98 }
99
100
101 # Main Program
102 # ============
103
104 set verbose 0
105 set host ""
106 set user $DefaultUser
107 set port $DefaultPort
e6a6b5f @hxw [scripts] Add start directory for screen
hxw authored
108 set dir "."
d271e02 @hxw [scripts] new methos of starting screen
hxw authored
109
110 set i 0
111 set arg [lindex $argv $i]
112 while {$arg != ""} {
113 switch -regexp -- $arg {
114 "^--verbose" {
115 set verbose 1
116 }
117 "^-v$" {
118 set verbose 1
119 }
e6a6b5f @hxw [scripts] Add start directory for screen
hxw authored
120 "^--cd=.+" {
121 set pos [string first "=" $arg]
122 set dir [string range $arg [expr $pos + 1] end]
123 }
d271e02 @hxw [scripts] new methos of starting screen
hxw authored
124 "^--login=.+" {
125 set pos [string first "=" $arg]
126 set user [string range $arg [expr $pos + 1] end]
127 }
128 "^--user=.+" {
129 set pos [string first "=" $arg]
130 set user [string range $arg [expr $pos + 1] end]
131 }
132 "^[^:]+$" {
133 set host $arg
134 set port $DefaultPort
135 }
136 "^[^:]+:[^:]+$" {
137 set pos [string first ":" $arg]
138 set host [string range $arg 0 [expr $pos - 1]]
139 set port [string range $arg [expr $pos + 1] end]
140 }
141 default {
142 usage "Invalid argument $arg"
143 }
144 }
145 incr i
146 set arg [lindex $argv $i]
147 }
148
149 if {$user == ""} {
150 usage "--login is not set"
151 }
152
153 if {$port == ""} {
154 usage "port is not specified"
155 }
156
157 if {$host == ""} {
158 usage "no host given"
159 }
160
161
e6a6b5f @hxw [scripts] Add start directory for screen
hxw authored
162 if {[probe $user $host $port $dir] != 0} {
d271e02 @hxw [scripts] new methos of starting screen
hxw authored
163 usage "failed to start screen"
164 }
Something went wrong with that request. Please try again.