Hey Everyone,
I have found this script online that has almost all the features I am looking for. However, I do not know enough expect to debug the problem.
http://linuxgazette.net/100/misc/tips/sshtool.expect.txt
First, it Traps out after it collects the user's password. I do not know why it will not continue.
Secondly I have tried with this script and other ones to try to pass MORE then one command to ssh to execute on a remote system. Every time I do I get one of the following.
- When I pass a set of commands expect interprets the ";" as the end of the line.
- If i use hex for the ";" it cannot run the second script correctly to run.
- If I try to use double quotes when passing the commands from bash to expect, expect now wraps those multiple entries in curly braces, breaking the expect spawn command.
#!/usr/bin/expect --
# set Variables
set usname [lrange $argv 0 0]
set password [lrange $argv 1 1]
set ipaddr [lrange $argv 2 2]
set commands [lrange $argv 3 end]
# now connect to remote UNIX box (ipaddr) with given script to execute
# Set the size of the buffer, and ensure it hasn't been undefined.
match_max 10000
set expect_out(buffer) {}
# Set the timeout before expect assumes the spawn has not responded or lost link
set timeout 60
# now connect to remote UNIX box (ipaddr) with given script to execute
spawn ssh -4 -2 -k -o StrictHostKeyChecking=no $usname@$ipaddr $commands
expect {
"*?assword:" {
send "$password\r"
} "yes/no)?" {
send "yes\r"
set timeout -1
} timeout {
exit
} -re . {
exp_continue
} eof {
exit
}
}
# Keep connection active as long as text is returned to terminal.
expect -re . { exp_continue } eof { exit }
expect eof