期待ssh脚本(在远程机器上执行命令)

时间:2016-01-24 03:38:01

标签: ssh expect

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

命令语法:ssh <classname> <command>

代码:

set ip "hive28.cs.berkeley.edu"
set class [lindex $argv 0]
// set $user and $ip according to class

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

不幸的是,我收到了这个错误:

~/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 <...>

但是当我直接运行生成的命令时,它可以工作(忽略gdircolors警告):

~/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/***

1 个答案:

答案 0 :(得分:1)

请改为尝试:

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"是两个不同的论点。