Computing.Net > Forums > Unix > Automate telnet using Expect

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Automate telnet using Expect

Reply to Message Icon

Name: asad
Date: January 6, 2003 at 21:11:49 Pacific
OS: unix
CPU/Ram: unix
Comment:

I am trying to create a script, which will automatically log me into router. I have created the following script but I cannot get it to work. Here is the script.

~~~~~~~~~~~~~~~~~~~~~~~~~
#!/opt/local/bin/expect

log_user 1
set rtr $argv
set login_id "username"
set pswd "password"
#append rtr ".ip"
spawn telnet $rtr
expect {

" login:" { send "$login_id\r" }
"Username:" { send "$login_id\r" }
"login:" { send "$login_id\r" }
"RETURN" { send "\r\r" }
}
expect "assword:" { send "$pswd\r" }
interact
~~~~~~~~~~~~~~~~~~~~~~~~~

I have named the file, router.
I also issued chmod u+x router to make it executable.

Can anyone tell me if there is another step that I am missing or if there is something wrong with the script.

Thanks
Asad




Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: January 6, 2003 at 21:40:11 Pacific
Reply:

Turn on the verbose mode. I'll have a better answer for you in the a.m. It looks like your send, expect section is incorrect.

Try running 'autoexpect' as a macro recorder then doing manually all the steps you want to repeat. It will show you what it needs to do.


0

Response Number 2
Name: David Perry
Date: January 7, 2003 at 04:40:45 Pacific
Reply:

Here is a working code snippet. BTW 'send "\x1d\x0d"' is the same as cntrl - ].

while { $n < 5 } {
spawn "/bin/sh"
send "telnet $LOCALHOST\r"
sleep 1
expect "ogin"
send "$username\r"
expect "Password"
send "$password\r"
send "\r\r"
"Welcome" {
incr pass0
set n 5
set p 0
close
}
"from" {
send "id\r"
expect {
$username {
incr pass0
set n 5
close
}
}
}
"login: $" {
set pass1 0
puts stdout "Expect return: $expect_out(0,string)"
puts stdout "Failed login"
send "\x1d\x0d"
expect "telnet"
send "close\r"
send "quit\r"
close
}
}
incr n
}


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Automate telnet using Expect

Telnet using expect script www.computing.net/answers/unix/telnet-using-expect-script/5207.html

Script to auto login to telnet session www.computing.net/answers/unix/script-to-auto-login-to-telnet-session/2854.html

Unix login through DOS telnet!!!! www.computing.net/answers/unix/unix-login-through-dos-telnet/1019.html