Tcl Expect fails spawning SSH to server but SSH from command line works

﹥>﹥吖頭↗ 提交于 2020-01-25 12:11:46

问题


I have some code that I'm using to connect to a server and perform some commands. The code is as follows:

#!/usr/bin/expect
log_file ./log_std.log

proc setPassword {oldPass newPass} {
    send -- "passwd\r"
    expect "* Old password:"
    send -- "$oldPass\r"
    expect "* New password:"
    send -- "$newPass\r"
    expect "* new password again:"
    send -- "$newPass\r"
}

set server [lindex $argv 0]

spawn /bin/ssh perfgen@$server

# Increase buffer size to support large text responses
match_max 100000

# Conditionally expects a prompt for host authenticity
expect {
    "*The authenticity of host*" {
        send -- "yes\r"
    }
}

What I find very strange is that when I SSH from my command line the SSH command works no problem. However, when I SSH from the shell script I get the following error:

spawn /bin/ssh perfgen@192.168.80.132

ssh: Could not resolve hostname 192.168.80.132
: Name or service not known

The same script runs against 3 servers, but 2 of the 3 servers always fail. However, if I try logging into the servers manually do do the work all three servers pass.

Any idea what might be happening here? I'm completely stumped. This code was working up until about 2 weeks ago and according to the server administrator nothing has changed on the server-side config.


回答1:


Trimming any whitespace seemed to solve the issue:

set serverTrimmed [string trim $server]


来源:https://stackoverflow.com/questions/21637915/tcl-expect-fails-spawning-ssh-to-server-but-ssh-from-command-line-works

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!