2016-04-16 7 views
0

In expect, gibt es eine Möglichkeit, den Befehl von Spawn zu zitieren? Ein expect-Skript, das den öffentlichen Schlüssel von einer AWS-Instanz an eine andere AWS-Instanz sendet, spawn manipuliert den Befehl so, dass sshs -i-Option eine ungültige cat Option wird.expect's spawn interpretiert ssh's -i wie es ist

Das Skript.

#!/usr/bin/expect -d 

spawn sudo -s cat /root/.ssh/id_rsa.pub | ssh -i /tmp/key.pem [email protected] sudo -s 'dd of=/root/.ssh/authorized_keys oflag=append conv=notrunc' 
expect { 
    "*yes/no*" { send "yes\r"; exp_continue } 
} 

Der Fehler.

$ 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 [email protected] 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" 

Funktioniert der Befehl, wenn er manuell ausgeführt wird? Ja.

sudo -s cat /root/.ssh/id_rsa.pub | ssh -i /tmp/key.pem [email protected] 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 Version 6.7 (Santiago)

Version 5.44.1.15

+1

Ich glaube nicht, können Sie Übergeben Sie eine Shell-Pipeline an '' spawn' ', wenn Sie nicht 'sh -c" cat ... | ssh ... "' spawnen sollten. – larsks

+0

Das war's! Bitte senden Sie Ihre Antwort als Antwort und ich werde Ihre Antwort abstimmen. – user2569618

Antwort

1

des Erwarten erwarten spawn Befehl ohne seine Argumente direkt führt sie auf eine Shell übergeben. Dies bedeutet, dass Shell-Konstrukte wie I/O-Umleitung und Pipelines nicht funktionieren. Wenn Sie laufen ...

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

... zu erwarten ist vorbei /root/.ssh/id_rsa.pubund alles danach als Argument für den Befehl cat.

Wenn Sie eine Shell-Pipeline ausführen möchten, müssen Sie explizit eine Shell starten und es in der Befehlszeile übergeben:

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

Zum Beispiel:

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> 
+0

Nicht einzelne Anführungszeichen; Die zugrundeliegende Sprache ist Tcl, die '{' '' '' '' '' '' '' verwendet. –

+0

Guter Fang, danke. – larsks