无法使用期望产生(ssh) [英] could not able to spawn(ssh) using expect

查看:103
本文介绍了无法使用期望产生(ssh)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行时

$expect filename.exp user pwd ip

我得到了错误

无法执行"ssh -l用户ip":没有这样的文件或目录

could not execute "ssh -l user ip": no such file or directory

filename.exp的内容是这样的:

#!/opt/sfw/bin/expect -D

set OMC_Usr  [lindex $argv 0]
set OMC_Pwd  [lindex $argv 1]
set OMC_IP  [lindex $argv 2]

set cmd "ssh -l $OMC_Usr $OMC_IP"
spawn $cmd
expect "Password:"
send "$OMC_Pwd\r"
interact

帮助我继续
谢谢...

help me to proceed
Thanks...

推荐答案

问题是spawn需要命令及其参数作为多个Tcl参数传递给它,就像标准Tcl exec命令一样.解决方法是直接执行此操作:

The problem is that spawn needs the command and its arguments passed to it as multiple Tcl arguments, just like the standard Tcl exec command. The fix is to do this directly:

spawn ssh -l $OMC_Usr $OMC_IP

或者,如果您愿意(并且您拥有Tcl 8.5):

Or, if you prefer (and you've got Tcl 8.5):

set cmd "ssh -l $OMC_Usr $OMC_IP"
spawn {*}$cmd

或者,如果您已达到8.4或更低:

Or, if you've got 8.4 or before:

eval spawn [lrange $cmd 0 end]

但是 不要 这样做:

But don't do this:

eval spawn $cmd

因为如果用户名(或IP地址)中包含Tcl元字符,这会意外中断.

because that will break unexpectedly if you have a Tcl metacharacter in the username (or IP address, but that's very unlikely).

当然, real 修复程序是设置RSA密钥对并使用ssh-agent对其进行管理.这样,您将无需在任何命令行上传递密码;这很重要,因为流程的命令行是有关流程的公共信息.真的.您可以使用诸如ps -efww之类的琐碎内容(或与您的操作系统等效的内容)来查找它.环境变量也同样不安全. ps也可以显示它们.

Of course, the real fix is to set up an RSA keypair and use ssh-agent to manage it. Like that, you won't need to pass passwords on any command line; that's important because the command line of a process is public information about the process. Really. You can find it out with something trivial like ps -efww (or the equivalent for your OS). Environment variables are just as insecure too; there's an option to ps to show them too.

这篇关于无法使用期望产生(ssh)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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