-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathexpectdo.exp
More file actions
executable file
·110 lines (94 loc) · 2.44 KB
/
expectdo.exp
File metadata and controls
executable file
·110 lines (94 loc) · 2.44 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/expect --
#
# 0.3 additional optimizations and fixes
# added paths
# removed the 2nd prompt during spawn
#
# SET VARS
set prompt "\\$|#|>"
set passprompt "\[Pp\]assword:"
set timedout ""
set closed ""
# GET VARS
## set timeout to forever
set timeout -1
## get ssh username as $userid
stty echo
send_user "\nSSH USERNAME : "
expect_user -re "(.*)\n"
set userid $expect_out(1,string)
## get ssh password as $pass without echo to console
stty -echo
send_user "SSH PASSWORD : "
expect_user -re "(.*)\n"
set pass $expect_out(1,string)
send_user "\n"
## get filename for the file containing the list
stty echo
send_user "LIST FILE : "
expect_user -re "(.*)\n"
set filename $expect_out(1,string)
if ![file exists $filename] {
error "File $filename does not exist"
}
## get the command that needs to be ran
stty echo
send_user "COMMAND : "
expect_user -re "(.*)\n"
set cmd $expect_out(1,string)
send_user "\n"
set timeout 5
# load the list
set fid [open $filename]
set content [read -nonewline $fid]
close $fid
set hosts [split $content "\n"]
foreach host $hosts {
set skip 0
send_user "\n\[\*DEBUG\] ====================================> $host \n"
# log_user 0 # enable this and line 104 if you want to reduce logging
spawn -noecho ssh -q -t -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $userid@$host
set timeout 10
expect {
$passprompt {
send "$pass\r"
expect -re $prompt
}
-re $prompt {
}
timeout {
send_user "\n\[\*DEBUG\] ====================================> $host did not respond\n"
lappend timedout $host
set skip 1
}
eof {
send_user "\n\[\*DEBUG\] ====================================> $host closed!\n"
lappend closed $host
set skip 1
}
}
if {$skip != 1} {
expect -re $prompt
send "export PATH; PATH=\$PATH:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin:/opt/csw/bin:/opt/csw/sbin:/usr/sfw/bin:/usr/sfw/sbin\n"
expect -re $prompt
send "$cmd\n"
# log_user 1
expect {
-re $passprompt { send "$pass\r" }
-re $prompt { }
}
expect -re $prompt
send "exit"
}
}
# send outputs
send_user "\n\[\*DEBUG\] ====================================> DONE\n"
send_user "\[\*DEBUG\] ========[llength $timedout] hosts timed-out\n"
foreach host $timedout {
send_user "---- $host\n"
}
send_user "\[\*DEBUG\] ========[llength $closed] hosts closed early\n"
foreach host $closed {
send_user "---- $host\n"
}
exit