I want to change the password on my server.

There are four possible passwords that should all be accepted for changing a password on the server.

To achieve this I want write an "expect" script which logs into my server and does something. But I don't know which password the device is using, but I know that it is one of four possible passwords.

Therefore my script needs to try all four passwords one after another. That means that if the first password fails the script should try the second password. If that fails again it should try the third. And if that fails it should try the fourth password.

Here is the code I have so far:

spawn telnet my_host
expect UserName {send admin\r}
expect PassWord [one of array passwords\r]

{if auth ok}
    do something
    try another password

expect -re #
send "logout\r"
expect eof

Can you help me achieve my aforementioned goal?

  • 1
    I guess that English is not your first language, but can you please try to better describe what you are doing and what problem you are having?  Is this about logging in when you don’t know which password is used on which host?  What does it have to do with changing passwords?  What does it have to do with hardware? … … … … … … … … … … … … … … … Please do not respond in comments; edit your question to make it clearer and more complete. – Scott Jul 16 '19 at 5:48
  • I want loggin into my hardware with one of four password. I.e. try logging with first password, if password not much, try next etc... so I dont understand, how say to script this condition – Анатолий Дорошенко Jul 16 '19 at 6:01
  • Why using expect – Rui F Ribeiro Jul 16 '19 at 21:13

perhaps something like this:

spawn telnet my_host

expect UserName 
send "admin\r"

set passwords [list foo bar baz]
set connected false
foreach pw $passwords {
    expect PassWord
    send -- "$pw\r"
    # change the "invalid password" pattern to whatever you need it to be.
    expect {
        "invalid password" {continue}
        -re "#" {
            set connected true

if {! $connected} {
    error "no password succeeded"

send "logout\r"
expect eof

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.