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 8742943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:35:22+00:00 2026-06-13T11:35:22+00:00

I am trying to create an expect script which will send a different password

  • 0

I am trying to create an expect script which will send a different password string based on the “expect”

  • Condition A: If a cisco device has not been setup with a username then the first prompt will simply be “Password:” – then it should use passwordA (no username)

  • Condition B: If it has been setup with a username then the prompt will be “Username:” followed by “Password:” – then it should use Username and PasswordB

#!/bin/bash
# Declare host variable as the input variable
host=$1 
 # Start the expect script
(expect -c "
set timeout 20
# Start the session with the input variable and the rest of the hostname
spawn telnet $host
set timeout 3 
if {expect \"Password:\"} {
send \"PasswordA\"}
elseif { expect \"Username:\"}
send \"UsersName\r\"}
expect \"Password:\"
log_user 0 
send \"PasswordB\r\"
log_user 1
expect \"*>\"
# send \"show version\r\"
# set results $expect_out(buffer) 
#expect \"Password:\"
#send \"SomeEnablePassword\r\"
# Allow us to interact with the switch ourselves
# stop the expect script once the telnet session is closed
send \"quit\r\"
expect eof
")
  • 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-13T11:35:23+00:00Added an answer on June 13, 2026 at 11:35 am

    You’re doing it wrong. 🙂

    The expect statement doesn’t look to see what comes first, it waits until it sees what you ask for (and times out if it doesn’t arrive in time), and then runs a command you pass to it. I think you can use it something like the way you’re trying to, but it’s not good.

    expect can take a list of alternatives to look for, like a C switch statement, or a shell case statement, and that’s what you need here.

    I’ve not tested this, but what you want should look something like this:

    expect {
      -ex "Username:" {
        send "UsersName\r"
        expect -ex "Password:" {send "PasswordB\r"}
      }
      -ex "Password:" {send "PasswordA\r"}
    }
    

    In words, expect will look for either “Username:” or “Password:” (-ex means exact match, no regexp), whichever comes first, and run the command associated with it.


    In response to the comments, I would try a second password like this (assuming that a successful login gives a ‘#’ prompt):

    expect {
      -ex "Username:" {
        send "UsersName\r"
        expect -ex "Password:" {send "PasswordB\r"}
      }
      -ex "Password:" {
        send "PasswordA1\r"
        expect {
          -ex "Password:" {send "PasswordA2\r"}
          -ex "#" {}
        }
      }
    }
    

    You could do it without looking for the # prompt, but you’d have to rely on the second Password: expect timing-out, which isn’t ideal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an FTP script that will copy a local folder structure
I have been trying to create an expect script to automatically login to my
I am trying to create a server which I expect to have high performance
I'm trying to expect an error in an rspec test. lambda {Participant.create!({:user_id => three.id,
I'm no expert on Regex. I'm trying to create a regular expression that will
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Ok so I am trying create a login script, here I am using PHP5
I am trying to create an Online Quiz script like expert rating test on
I'm trying to create a generic graphics export tool which works by implementing the
I'm trying to create an MVC view that will display some data in a

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.