简体   繁体   中英

Expect: use output of remote command

I am new to using expect, and I've searched for an answer to the following question but haven't found it yet. I'm trying to log in to a server and run a command on that server. The command will output text including another command that I want to run and a password that I'll need.

Eg

#!/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>

The output of "send $command" above is

-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$

Now what I need is to grab "ssh new-user@new-host" and "Ex17dsk5" and use them in $next_command and $new_password. I think I need to use expect_out(X,string) as I match expressions, but I haven't been able to figure out how to do it all.

Any help is greatly appreciated.


EDIT: ADDITIONAL INFORMATION AFTER RESPONSE BELOW


Thanks for the tip! However, it's still not working, but a lot closer. The code that I'm currently using is:

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

My thought was that the ? would make it non-greedy. However, it seems to be matching to the last \\n in the buffer (see below). And since it matches to the last \\n, expect_out(1,string) includes the password information that I'm trying to match with expect. The code produces the following debugging 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 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

Thanks guys for you help! Below is what I finally settled on for my matching: :

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

Give this a shot. Also use exp_internal 1 in your script to enable internal diagnostics to troubleshoot.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM