-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh.tcl
56 lines (49 loc) · 1.73 KB
/
ssh.tcl
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
package provide 9pm::ssh 1.0
# Wee need Expect TODO: Check if it exists (gracefull error-out)
package require Expect
namespace eval ::9pm::ssh {
proc connect {node args} {
set IP [::9pm::misc::dict::require $node hostname]
set PROMPT [::9pm::misc::dict::require $node prompt]
set PORT [::9pm::misc::dict::get $node port]
set USER [::9pm::misc::dict::get $node username]
set PASS [::9pm::misc::dict::get $node password]
set KEYFILE [::9pm::misc::dict::get $node keyfile]
set opts [dict get $::9pm::core::rc "ssh_opts"]
set ssh_cmd "ssh $opts $IP"
if {$USER != ""} {
append ssh_cmd " -l $USER"
}
if {$PORT != ""} {
append ssh_cmd " -p $PORT"
}
if {$KEYFILE != ""} {
append ssh_cmd " -i $KEYFILE"
}
if {$args != ""} {
append ssh_cmd " $args"
}
::9pm::output::debug "Connecting to \"$IP\" (as \"$USER\")"
expect *
send "$ssh_cmd\n"
expect {
$PROMPT {
::9pm::output::debug "Connected to \"$IP\" (as \"$USER\")"
}
-nocase "password" {
if {$PASS == ""} {
::9pm::fatal ::9pm::output::fail \
"SSH got password prompt but no password is provided in config"
}
send "$PASS\n"
exp_continue -continue_timer
}
timeout {
::9pm::fatal ::9pm::output::fail "SSH connection to \"$IP\" failed (timeout)"
}
eof {
::9pm::fatal ::9pm::output::fail "SSH connection to \"$IP\" failed (eof)"
}
}
}
}