Ich bin neu zu verwenden erwarten, und ich habe eine Antwort auf die folgende Frage gesucht, aber noch nicht gefunden. Ich versuche, mich bei einem Server anzumelden und einen Befehl auf diesem Server auszuführen. Der Befehl gibt Text aus, einschließlich eines anderen Befehls, den ich ausführen möchte, und eines Passworts, das ich brauche.Erwarten: Verwenden Sie die Ausgabe von Remote-Befehl
z.
#!/usr/bin/expect --
<initialization of variables>
spawn ssh [email protected]$host
expect -re ".*sswor.*"
send "$password\r"
expect $prompt
send $command
expect magical stuff that I can use to create the next command
send $next_command
send $new_password
<more stuff>
Die Ausgabe von oben "$ Befehl senden" ist
-bash-3.00$ <command from send>
Log into the next device by copying and pasting the following line: ssh [email protected]
The password to login is: Ex17dsk5
-bash-3.00$
Nun, was ich brauche ist "ssh new-user @ new-Host" und "Ex17dsk5" und sie in $ zu packen next_command und $ new_password. Ich denke, ich muss expect_out (X, string) verwenden, wenn ich Ausdrücke anpasse, aber ich bin nicht in der Lage, herauszufinden, wie man alles macht.
Jede Hilfe wird sehr geschätzt.
EDIT: ZUSÄTZLICHE INFORMATIONEN NACH UNTEN RESPONSE
Danke für den Tipp! Es funktioniert jedoch immer noch nicht, aber viel näher. Der Code, den ich zur Zeit verwendet, ist:
expect {
-re "Log into the remoteHost by copying and pasting the following line: ssh (.*)?\n?" {set remoteHostlogin "$expect_out(1,string)"; exp_continue}
-re "The password to login to the remoteHost is: (.*)\r\n" {set remoteHostpassword "$expect_out(1,string)"}
}
Mein Gedanke war, dass die? würde es nicht gierig machen. Es scheint jedoch zu dem letzten \ n im Puffer (siehe unten) zu passen. Und da es mit dem letzten \ n übereinstimmt, enthält expect_out (1, string) die Kennwortinformationen, die ich mit expect vergleichen möchte. Der Code erzeugt die folgende Debug-Info:
You must ssh to the remoteHost from one of the log servers. Login to either Log Server 1 or Log Server 2 before logging into the remoteHost.
Log into the remoteHost by copying and pasting the following line: ssh [email protected]
The password to login to the remoteHost is: B4dZsePI
expect: does "\r\nLast login: Tue Apr 3 14:32:48 2012 from log01i\r\r\nUse of CompanyName computing systems is restricted to authorized use only. \r\nThe use of any CompanyName computing system may be monitored and recorded \r\nby CompanyName for administrative and security reasons at any time. Your \r\nuse of these computing systems constitutes consent to this monitoring. \r\nCompanyName reserves the right to take appropriate action against anyone \r\nwho accesses or uses, or attempts to access or use, any CompanyName \r\ncomputing system improperly or without the appropriate authorization.\r\n\r\nYou have new mail.\r\n-bash-3.00$ ./get_remoteHost_login.sh 0002F5007546\r\n \r\n \r\nYou must ssh to the remoteHost from one of the log servers. Login to either Log Server 1 or Log Server 2 before logging into the remoteHost.\r\nLog into the remoteHost by copying and pasting the following line: ssh [email protected] \r\nThe password to login to the remoteHost is: B4dZsePI \r\n \r\n \r\n" (spawn_id exp6) match regular expression "Log into the remoteHost by copying and pasting the following line: ssh (.*)?\n?"? Gate "Log into the remoteHost by copying and pasting the following line: ssh *"? gate=yes re=yes
expect: set expect_out(0,string) "Log into the remoteHost by copying and pasting the following line: ssh [email protected] \r\nThe password to login to the remoteHost is: B4dZsePI \r\n \r\n \r\n"
expect: set expect_out(1,string) "[email protected] \r\nThe password to login to the remoteHost is: B4dZsePI \r\n \r\n \r\n"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "\r\nLast login: Tue Apr 3 14:32:48 2012 from slog01i\r\r\nUse of CompanyName computing systems is restricted to authorized use only. \r\nThe use of any CompanyName computing system may be monitored and recorded \r\nby CompanyName for administrative and security reasons at any time. Your \r\nuse of these computing systems constitutes consent to this monitoring. \r\nCompanyName reserves the right to take appropriate action against anyone \r\nwho accesses or uses, or attempts to access or use, any CompanyName \r\ncomputing system improperly or without the appropriate authorization.\r\n\r\nYou have new mail.\r\n-bash-3.00$ ./get_remoteHost_login.sh 0002F5007546\r\n \r\n \r\nYou must ssh to the remoteHost from one of the log servers. Login to either Log Server 1 or Log Server 2 before logging into the remoteHost.\r\nLog into the remoteHost by copying and pasting the following line: ssh [email protected] \r\nThe password to login to the remoteHost is: B4dZsePI \r\n \r\n \r\n"
expect: continuing expect
EDIT
Danke Jungs für Sie helfen! Im Folgenden finden Sie, was schließlich für meinen Matching auf I angesiedelt: :
expect {
-re "Log into the remoteHost by copying and pasting the following line: (ssh .*\[0-9]+\.\[0-9]+\.\[0-9]+\.\[0-9]+)" {set remoteHostlogin "$expect_out(1,string)"; exp_continue}
-re "The password to login to the remoteHost is: (\[0-9a-zA-Z]{8})" {set remoteHostpassword "$expect_out(1,string)"}
}
Ich konnte dies noch nicht vollständig testen, aber es sieht sehr vielversprechend aus. Am Morgen sollte ich überprüfen können, ob das funktioniert. Vielen Dank! –
Danke für den Tipp. Es funktioniert nicht ganz, und ich denke, es liegt daran, dass eines der Muster ein gieriges Spiel ist, aber ich konnte nicht herausfinden, wie ich es korrigieren kann. Ich würde es begrüßen, wenn Sie sich die zusätzlichen Informationen im obigen Beitrag ansehen könnten. –
Um es nicht gierig zu machen, müssen Sie das '' 'direkt hinter das' * 'setzen, so sollte es so aussehen:' line: ssh (. *?) \ N? "' – resmon6