期望:使用远程命令的输出

时间:2012-04-02 19:40:49

标签: regex command send expect

我是使用expect的新手,我已经搜索了以下问题的答案,但还没有找到它。我正在尝试登录服务器并在该服务器上运行命令。该命令将输出文本,包括我想要运行的另一个命令和我需要的密码。

E.g。

#!/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中使用它们。我想我需要使用expect_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匹配,所以expect_out(1,string)包含我试图与expect匹配的密码信息。该代码生成以下调试信息:

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

修改

谢谢你的帮助!以下是我最终确定的匹配方式: :

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

1 个答案:

答案 0 :(得分:0)

给它一个机会。还可以在脚本中使用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"