[英]Expect's spawn interprets ssh's -i as it's own
在期望中,有什么方法可以引用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
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
命令的參數傳遞。
如果要運行外殼程序管道,則需要顯式啟動外殼程序並將其傳遞給命令行:
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.