How to use Casper Variables in expect Script

L3nny5
New Contributor III

Hello,

I have an expect script which I copied from user "ktappe" and I want to integrate the $4-$11 variables so I don't have to change the script every time I need to change the password.

This example doesn't work. :-(

#!/usr/bin/expect -f

set newpassword $5
set oldpassword $6

spawn firmwarepasswd -setpasswd
expect {
    "Enter password:" {
        send "$oldpassword
"
        exp_continue
    }
    "Enter new password:" {
        send "$newpassword
"
        exp_continue
    }
    "Re-enter new password:" {
        send "$newpassword
"
        exp_continue
    }
}

Thanks!

3 REPLIES 3

davidacland
Honored Contributor II
Honored Contributor II

Try [lindex $argv 5] and [lindex $argv 6]

#!/usr/bin/expect -f

set new password [lindex $argv 5]
set oldpassword [lindex $argv 6]

spawn firmwarepasswd -setpasswd
expect {
    "Enter password:" {
        send "$oldpassword
"
        exp_continue
    }
    "Enter new password:" {
        send "$newpassword
"
        exp_continue
    }
    "Re-enter new password:" {
        send "$newpassword
"
        exp_continue
    }
}

davidacland
Honored Contributor II
Honored Contributor II

Hi,

Did that work in the end?

L3nny5
New Contributor III

Sorry. Haven't had the time yet to test it until now.

Unfortunately it does not work. But i figured it out myself!

The number of the variable you are using needs to be minus 1. So if you are using variable 4 and 5, you have to use [lindex $argv 3] and [lindex $argv 4]

The Script for variable 4 and 5 looks like this and is working:

#!/usr/bin/expect -f

set newpassword [lindex $argv 3]
set oldpassword [lindex $argv 4]

spawn firmwarepasswd -setpasswd
expect {
    "Enter password:" {
        send "$oldpassword
"
        exp_continue
    }
    "Enter new password:" {
        send "$newpassword
"
        exp_continue
    }
    "Re-enter new password:" {
        send "$newpassword
"
        exp_continue
    }
}