简体   繁体   English

Expect的生成将ssh的-i解释为自己的

[英]Expect's spawn interprets ssh's -i as it's own

In expect, is there some way to quote the command run by spawn? 在期望中,有什么方法可以引用spawn运行的命令吗? An expect script which pushes the public key from one AWS instance to another AWS instance, spawn manipulates the command such that ssh's -i option becomes a invalid cat option. 期望脚本将公钥从一个AWS实例推送到另一个AWS实例,spawn操纵该命令,以使ssh的-i选项成为无效的cat选项。

The script. 剧本。

#!/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 }
}

The error. 错误。

$ 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"

Does the command work when run manually? 手动运行时该命令是否起作用? Yes. 是。

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

OS: Red Hat Enterprise Linux Server release 6.7 (Santiago) 操作系统:Red Hat Enterprise Linux Server 6.7版(圣地亚哥)

expect version 5.44.1.15 预期版本5.44.1.15

Expect's spawn command executes its arguments directly without passing them to a shell. Expect的spawn命令直接执行其参数,而无需将其传递给shell。 This means that shell constructs like i/o redirection and pipelines won't work. 这意味着像I / O重定向和管道这样的shell构造将无法工作。 When you run... 当你跑步...

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

...expect is passing /root/.ssh/id_rsa.pub and everything after that as arguments to the cat command. ... expect将/root/.ssh/id_rsa.pub 及其后的所有内容作为cat命令的参数传递。

If you want to run a shell pipeline, you need to explicitly start a shell and pass it the command line: 如果要运行外壳程序管道,则需要显式启动外壳程序并将其传递给命令行:

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

For example: 例如:

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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM