forked from PecanProject/pecan
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsshtunnel.sh
executable file
·58 lines (51 loc) · 1.28 KB
/
sshtunnel.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/expect -f
if {[llength $argv] != 3} {
puts "${argv0} hostname username folder"
puts "folder should contain a file called password with the actual password"
exit -1
}
# set Variables
set sshHostname [lrange $argv 0 0]
set sshUsername [lrange $argv 1 1]
set sshTunnelDir [lrange $argv 2 2]
set sshPassFile [file join $sshTunnelDir password]
set sshTunnel [file join $sshTunnelDir tunnel]
set sshPID [file join $sshTunnelDir pid]
# check if tunnel already exists
if {[file exists $sshTunnel]} {
puts "${sshTunnel} already exists"
exit -1
}
# load the file
if {! [file exists $sshPassFile]} {
puts "missing ${sshPassFile} file"
exit -1
}
# read password and delete file
set f [open $sshPassFile]
set sshPassword [string trim [read $f]]
close $f
file delete $sshPassFile
# start ssh
spawn ssh -nN -o ControlMaster=yes -o ControlPath="$sshTunnel" -l $sshUsername $sshHostname
set f [open $sshPID "w"]
puts $f [exp_pid]
close $f
# answer some questions (like password) and just wait
set timeout -1
expect {
"yes/no" {
send "yes\r"
exp_continue
}
"*?assword" {
puts "got password prompt"
sleep .5
send "${sshPassword}\r"
exp_continue
}
eof {
file delete $sshPID
exit -1
}
}