Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7725507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:57:19+00:00 2026-06-01T04:57:19+00:00

I am new to using expect, and I’ve searched for an answer to the

  • 0

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.

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>

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)"}
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:57:21+00:00Added an answer on June 1, 2026 at 4:57 am

    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"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a cancellable worker thread using the new threading constructs in
I'm using the new Google Calendar API v3. I'm trying to obtain the users
I am using following code to setup connection string: ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder
i'm very new to using mysql with php, and here's what i found online
I'm trying to add a new bbcode to my phpfusion application. Im using with
Using the following model, I expect that when the Submit button is hit, the
I still very new using Subversion. Is it possible to have a working copy
New at using admob within Android. Is it possible to show ads with certain
when using new Date,I get something like follows: Fri May 29 2009 22:39:02 GMT+0800
I'm using new style classes in Python 2.6 and am having trouble with __getattr__

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.