我尝试使用期望脚本向远程服务器发送命令时遇到问题。 如果我在脚本中手动将其插入send命令中,该命令正常,但如果通过参数($ command)将其传递给except脚本,则该命令无效。 我想在远程服务器上运行的命令是:期望命令变量与管道和特殊字符不起作用
top -b -n 2 |头-15 & & ls -lrt/var/log |头-10
例:./myexpectscript密码IP “顶-b -n 2 |头-15 &”
更新:我发现我发的每一个命令(即多单字符串),通过这个脚本在远程服务器括号内执行... 例如:
./myexpectscript密码IP“PWD”是确定
./myexpectscript密码IP“回声你好”答案是:
# {echo hello}
/bin/sh: {echo: not found
如果我删除了双引号的命令作品:
./myexpectscript密码IP回声你好
# echo hello
hello
所以,问题是,我想执行一个多管道单线命令:
top -b -n 2 | head -15 && ls -lrt /var/log | head -10
在服务器上通过不同的脚本我我:
# {top -b -n 2 | head -15 && ls -lrt /var/log | head -10}
/bin/sh: {top: not found
ls: /var/log: No such file or directory
head: invalid number '10}'
这应该使用双引号,但我得到了大括号中的命令。 我在其他类型的服务器上试过这个脚本,并且我得到了相同的行为。
#!/usr/bin/expect -f
set password [lrange $argv 0 0]
set ipaddr [lrange $argv 1 1]
set command [lrange $argv 2 end]
set timeout -1
spawn ssh [email protected]$ipaddr
match_max 100000
expect "*?assword:*"
send -- "$password\r"
expect "*\\\[0-7\\\]:*"
send -- "5\r"
expect "*\\\[0-4\\\]:*"
send -- "3\r"
expect "\\\#*"
#spawn {*}$command
#eval spawn $command
#send -- "$command\r"
send -- "top -b -n 2 | head -15 && ls -lrt /var/tslog | head -10\r"
expect "\\\#*"
send -- "exit\r"
expect "*\\\[0-4\\\]:*"
send -- "0\r"
expect "*\\\[0-7\\\]:*"
send -- "0\r"
expect eof
显示你究竟是如何作为一个参数提供的命令将期望脚本 –