【问题标题】:Expect's spawn interprets ssh's -i as it's ownExpect 的 spawn 将 ssh 的 -i 解释为它自己的
【发布时间】: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 ..."。
  • 就是这样!请提交您的回复作为答案,我会投票赞成您的答案。

标签: linux tcl expect


【解决方案1】:

Expect 的spawn 命令直接执行它的参数而不将它们传递给shell。这意味着像 i/o 重定向和管道这样的 shell 结构将不起作用。当你运行时...

spawn sudo -s cat /root/.ssh/id_rsa.pub | ssh -i /tmp/key.pem ...

...expect 正在将/root/.ssh/id_rsa.pub 以及之后的所有内容作为参数传递给cat 命令。

如果要运行 shell 管道,则需要显式启动 shell 并将其传递给命令行:

spawn sh -c {sudo -s cat /root/.ssh/id_rsa.pub | ssh -i /tmp/key.pem ...}

例如:

expect1.7> spawn sh -c {echo hello world | sed s/world/nurse/}
spawn sh -c echo hello world | sed s/world/nurse/
14450
expect1.8> expect EOF
hello nurse
expect1.9> 

【讨论】:

  • 不是单引号;底层语言是 Tcl,它使用{...} 来达到同样的目的。
猜你喜欢
  • 1970-01-01
  • 2016-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-14
  • 1970-01-01
  • 2012-04-01
  • 1970-01-01
相关资源
最近更新 更多