Regex Expect:使用远程命令的输出

Regex Expect:使用远程命令的输出,regex,command,send,expect,Regex,Command,Send,Expect,我不熟悉使用expect,我已经搜索了以下问题的答案,但还没有找到。我正在尝试登录到服务器并在该服务器上运行命令。该命令将输出文本,包括我要运行的另一个命令和我需要的密码 例如 我的想法是什么?会让它不贪婪。但是,它似乎与缓冲区中的最后一个\n匹配(请参见下文)。由于它与最后一个\n匹配,expect\u out(1,字符串)包含我试图与expect匹配的密码信息。该代码生成以下调试信息: You must ssh to the remoteHost from one of the log se

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

例如

我的想法是什么?会让它不贪婪。但是,它似乎与缓冲区中的最后一个\n匹配(请参见下文)。由于它与最后一个\n匹配,expect\u out(1,字符串)包含我试图与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

编辑

谢谢你们的帮助!以下是我最终决定匹配的内容: :


试试看。还可以在脚本中使用
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"

我还不能完全测试这个,但它看起来很有希望。早上我应该能够验证这是否有效。谢谢谢谢你的提示。这不是很有效,我想这是因为其中一个模式是贪婪匹配,但我还没有弄清楚如何纠正它。如果您能看一下上面帖子中添加的信息,我将不胜感激。为了使其不贪婪,您需要将
直接放在
*
之后,因此它应该是这样的:
行:ssh(.*)\n?”
谢谢,我也发现了这一点。我最后使用了以下表达式:
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)"}
        }
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)"}
        }
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"