Tcl Expect无法将SSH生成到服务器,但是命令行中的SSH可以工作 [英] Tcl Expect fails spawning SSH to server but SSH from command line works

查看:131
本文介绍了Tcl Expect无法将SSH生成到服务器,但是命令行中的SSH可以工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些用于连接服务器并执行一些命令的代码.代码如下:

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"
    }
}

我感到非常奇怪的是,当我从命令行进行SSH时,SSH命令没有问题.但是,当我从shell脚本进行SSH时,出现以下错误:

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

同一脚本针对3台服务器运行,但是3台服务器中的2台总是失败.但是,如果我尝试手动登录服务器,则所有三台服务器都会通过该工作.

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.

您知道这里可能会发生什么吗?我完全迷住了.该代码一直运行到大约2周前,根据服务器管理员的说法,服务器端配置没有任何变化.

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.

推荐答案

修剪空白似乎可以解决此问题:

Trimming any whitespace seemed to solve the issue:

set serverTrimmed [string trim $server]

这篇关于Tcl Expect无法将SSH生成到服务器,但是命令行中的SSH可以工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆