需要ssh脚本(在远程计算机上执行命令)

需要ssh脚本(在远程计算机上执行命令),ssh,expect,Ssh,Expect,我正在编写一个expect脚本,它使用ssh在远程服务器上执行命令 命令语法:ssh 代码: 不幸的是,我遇到了以下错误: ~/foo> ssh2 162 'pwd' spawn ssh ***@hive28.cs.berkeley.edu 'pwd' ssh: Could not resolve hostname hive28.cs.berkeley.edu 'pwd': Name or service not known send: spawn id exp6 not open

我正在编写一个expect脚本,它使用ssh在远程服务器上执行命令

命令语法:
ssh

代码:

不幸的是,我遇到了以下错误:

~/foo> ssh2 162 'pwd'

spawn ssh ***@hive28.cs.berkeley.edu 'pwd'
ssh: Could not resolve hostname hive28.cs.berkeley.edu 'pwd': Name or service not known
send: spawn id exp6 not open
    while executing
"send "$password\r""
    invoked from within <...>
请尝试以下方法:

set ip "hive28.cs.berkeley.edu"
set class [lindex $argv 0]

set cmd [lindex $argv 1]
spawn ssh "$user@$ip" "$cmd"
expect "assword"
send "$password\r";
interact

问题似乎在于你的报价
“$user\@$ip”
“$cmd”
是两个独立的参数。

关于Tcl的2个小点(除此之外):1
@
不是特殊字符,不需要转义;2.Tcl不需要在变量周围加引号,即使变量值包含空格;我试图对OP的脚本做最小的修改。引用是为了清晰;它们不会造成伤害。但是,在评论中指出这一点很好!我将删除
@
转义,即使它位于原始代码中。谢谢太棒了,很高兴能帮上忙:)如果你先
ssh-keygen
ssh-copy-id
,你根本不需要expect。
~/foo> ssh ***@hive28.cs.berkeley.edu 'pwd'
***@hive28.cs.berkeley.edu's password: 
/home/ff/cs162/adm/bashrc.d/70-gnu.sh: line 36: gdircolors: command not found
/home/cc/cs162/sp16/class/***
set ip "hive28.cs.berkeley.edu"
set class [lindex $argv 0]

set cmd [lindex $argv 1]
spawn ssh "$user@$ip" "$cmd"
expect "assword"
send "$password\r";
interact