问题
I am trying to ssh through .tcl script from ActiveState TCL 'tclsh' window. Having WINDOWS OS system.
#!/bin/sh
# \
exec tclsh "$0" ${1+"$@"}
package require Expect
set user [lindex $argv 0]
set password [lindex $argv 1]
set DeviceIpAddr [lindex $argv 2]
set DeviceHostName [lindex $argv 3]
foreach DeviceIp $DeviceIpAddr HostName $DeviceHostName {
spawn ssh $DeviceIp
expect "login as:"
send "$user\r"
expect "Password:"
send "$password\r"
expect "$HostName ~]#"
}
I see below error while execute in tclsh(ActiveTCL)
% tclsh Test.tcl root 321 17.250.217.151 lb02va
The system cannot find the file specified.
while executing
"spawn ssh root@$DeviceIp"
("foreach" body line 3)
invoked from within
"foreach DeviceIp $DeviceIpAddr HostName $DeviceHostName {
spawn ssh root@$DeviceIp
expect "login as:"
send "$user\r"
expect "Password:"
send..."
(file "Test.tcl" line 12)
child process exited abnormally
Kindly assist me resolving this. Thank you.
回答1:
First, make sure that you have ssh installed. From the bash prompt (Mac, Linux, Cygwin) or cmd prompt (Windows), type:
ssh
If you see an error, try to fix it. The most likely cause is ssh not installed, or not in the path.
Next, in your script, you did not use the $user variable, instead you use the hard-coded root. Fix that:
spawn ssh $user@$DeviceIp
The final problem: you already specified the user name from the command line, the ssh program will not ask for user again, so you must delete these two lines:
expect "login as:"
send "$user\r"
After that, hopefully everything will go as planned.
来源:https://stackoverflow.com/questions/30122449/unable-to-spawn-ssh-using-tcl-expect-in-activetcl