0

I'm using expect to login to remote machine. But i really don't know what exactly this expect is. By googling i got to know about these commands. How to save these scripts & how to execute it ?

spawn telnet 10.123.9.111
expect login {send username\r}
expect password {send password\r}

is this correct ? Suggestion to improve this script most welcome. Thank you.

1 Answer 1

1

Save this to a file telnet.exp:

#!/usr/bin/expect -f
spawn telnet 10.123.9.111
expect login {send username\r}
expect password {send password\r}
interact  ;# I assume you want to do something on the remote machine here

Make it executable: chmod 700 telnet.exp -- you want to use restrictive permissions since you're storing your password in a plain-text file which is not a good idea (you should be using ssh with keys unless the device cannot run ssh)

Run it: ./telnet.exp

If you want to be able to pass variables to your script:

#!/usr/bin/expect -f
lassign $argv ip user passwd
spawn telnet $ip
expect login {send $user\r}
expect password {send $passwd\r}
interact  ;# I assume you want to do something on the remote machine here

Run it: ./telnet.exp 10.123.9.111 username secret

This is bad too because now the password will show up in a ps listing.

1
  • @ Glenn : How can i execute it on windows ? Aug 13, 2012 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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