Ожидайте: используйте вывод удаленной команды - PullRequest
0 голосов
/ 02 апреля 2012

Я новичок в использовании ожидаемого, и я искал ответ на следующий вопрос, но еще не нашел его.Я пытаюсь войти на сервер и запустить команду на этом сервере.Команда выведет текст, включая другую команду, которую я хочу выполнить, и пароль, который мне понадобится.

Например,

#!/usr/bin/expect --

<initialization of variables>

spawn ssh $user@$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>

Вывод "send $ command" выше:

-bash-3.00$ <command from send>
Log into the next device by copying and pasting the following line: ssh new-user@new-host

The password to login is: Ex17dsk5                                                                        


-bash-3.00$

Теперь мне нужно взять "ssh new-user @ new-host" и "Ex17dsk5" и использовать их в $ next_command и $ new_password.Я думаю, что мне нужно использовать wait_out (X, string) для сопоставления выражений, но я не смог понять, как все это сделать.

Любая помощь с благодарностью.


РЕДАКТИРОВАТЬ: ДОПОЛНИТЕЛЬНАЯ ИНФОРМАЦИЯ ПОСЛЕ ОТВЕТА НИЖЕ


Спасибо за совет!Тем не менее, это все еще не работает, но намного ближе.Код, который я сейчас использую:

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)"}
        }

Я думал, что?сделал бы это не жадным.Однако, похоже, что он совпадает с последним \ n в буфере (см. Ниже).И поскольку он совпадает с последним \ n, wait_out (1, строка) содержит информацию о пароле, которую я пытаюсь сопоставить с ожидаемым.Код выдает следующую отладочную информацию:

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 0005B9-cdmaremoteHost-0002F5007546@5.0.31.62                                                                       
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 0005B9-cdmaremoteHost-0002F5007546@5.0.31.62                                                                       \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 0005B9-cdmaremoteHost-0002F5007546@5.0.31.62                                                                       \r\nThe password to login to the remoteHost is: B4dZsePI                                                                        \r\n \r\n \r\n"
expect: set expect_out(1,string) "0005B9-cdmaremoteHost-0002F5007546@5.0.31.62                                                                       \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 0005B9-cdmaremoteHost-0002F5007546@5.0.31.62                                                                       \r\nThe password to login to the remoteHost is: B4dZsePI                                                                        \r\n \r\n \r\n"
expect: continuing expect

EDIT

Спасибо, ребята, за вашу помощь!Вот то, на чем я наконец остановился: «

* 1027»

1 Ответ

0 голосов
/ 03 апреля 2012

Дайте этому шанс.Также используйте exp_internal 1 в своем скрипте, чтобы включить внутреннюю диагностику для устранения неполадок.

expect -re "Log into the next device by copying and pasting the following line: (.*)\n" {
  set loginstr "$expect_out(1,string)" }
expect -re "The password to login is: (.*)\n" {
  set passwordstr "$expect_out(1,string)" }
send "$loginstr\r"
expect "*?assword*"
send "$passwordstr\r"
...