【发布时间】:2016-04-16 00:10:27
【问题描述】:
在期望中,有什么方法可以引用 spawn 运行的命令吗?一个将公钥从一个 AWS 实例推送到另一个 AWS 实例的期望脚本,spawn 操作该命令使得 ssh 的 -i 选项变为无效的 cat 选项。
脚本。
#!/usr/bin/expect -d
spawn sudo -s cat /root/.ssh/id_rsa.pub | ssh -i /tmp/key.pem ec2-user@ip-50-101-23-6 sudo -s 'dd of=/root/.ssh/authorized_keys oflag=append conv=notrunc'
expect {
"*yes/no*" { send "yes\r"; exp_continue }
}
错误。
$ pushSSH.expect
expect version 5.44.1.15
argv[0] = /usr/bin/expect argv[1] = -d argv[2] = ./pushSSH.expect
set argc 0
set argv0 "./pushSSH.expect"
set argv ""
executing commands from command file ./pushSSH.expect
spawn sudo -s cat /root/.ssh/id_rsa.pub | ssh -i /tmp/key.pem ec2-user@ip-50-101-23-6 sudo -s 'dd of=/root/.ssh/authorized_keys oflag=append conv=notrunc'
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {13106}
expect: does "" (spawn_id exp4) match glob pattern "*yes/no*"? no
cat: invalid option -- 'i'
Try `cat --help' for more information.
expect: does "cat: invalid option -- 'i'\r\nTry `cat --help' for more information.\r\n" (spawn_id exp4) match glob pattern "*yes/no*"? no
expect: read eof
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "cat: invalid option -- 'i'\r\nTry `cat --help' for more information.\r\n"
手动运行该命令是否有效?是的。
sudo -s cat /root/.ssh/id_rsa.pub | ssh -i /tmp/key.pem ec2-user@ip-50-101-23-6 sudo -s 'dd of=/root/.ssh/authorized_keys oflag=append conv=notrunc'
0+1 records in
0+1 records out
401 bytes (401 B) copied, 7.8214e-05 s, 5.1 MB/s
操作系统:Red Hat Enterprise Linux Server 6.7 版(圣地亚哥)
预计版本 5.44.1.15
【问题讨论】:
-
我认为你不能像这样将 shell 管道传递给
spawn,除非你是spawn sh -c "cat ... | ssh ..."。 -
就是这样!请提交您的回复作为答案,我会投票赞成您的答案。