Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
telnet login using expect

Posted by pgpatel (pgpatel), 29 July 2005
Hi,
I am trying to login into the remote system using expect code....I am new in expect...
my code is:

#!/usr/bin/expect
spawn telnet 172.25.126.248
expect "login:"
exp_send  "myname\r"
expect "password: "
exp_send "123456\r"
exit

but when i am executing this code it is asking for password and getting stuck(hang)...
can you please help me?
please give me regarding code....

what is log_user 0
can u explain it please?

thanks


Posted by pgpatel (pgpatel), 29 July 2005
Hello
i am able to login now but i am not getting pompt...
instead of it i am getting message like
plesae press<enter> to continue
or reenter your local terminal hostname
or oress esc to quit


please reply me
tnaks

Posted by Custard (Custard), 29 July 2005
Hi,

The telnet protocol is slightly clever in that the client negotiates with the server what terminal type to use, and also sends other bits of vital information from the environment.

Since you are spawning telnet from within expect, these pieces of information don't appear to be available to send, so the telnet server (telnetd) is asking for them (or actually, probably the .profile script sees that they are not present and asks for them).

So, you could try setting the environment before spawning telnet, or handling the new prompt you have identified and sending the hostname to the server.

I haven't tested this, and I will have a play later, but this *looks* like what is happening.

hth

B

Posted by Custard (Custard), 29 July 2005
I had a little play, and made the following changes :-

Code:
#!/usr/bin/expect
# Set the DISPLAY environment variable (as a test)
set env(DISPLAY) chilli:0
spawn telnet 172.25.126.248
# Not absolutely necessary, but good to keep the spawn_id for later
set telnet $spawn_id
# Case insensitive, just in case..
expect -nocase "login:"
send  "myuser\r"
# My telnetd insisted on an uppercase P. This works bothways
expect -nocase "password: "
send "123456\r"
# Match the prompt (contains a $)
expect -re {\$}
# Get the environment variables
send "env\r"
# Wait for the prompt
expect -re {\$}
exit


Which should print out the environment variables available to the shell you are connecting to.

Compare these with the environment when you connect manually.

Mine seemed to get it right, with DISPLAY and TERM set correctly.
Maybe this test will show up something.

Oh, and I forgot to ask, you are connecting to a Unix telnetd, right?

hth

B

Posted by pgpatel (pgpatel), 1 August 2005
Thanks for your reply....
now i got solution....
now i have one another question...
how can i save output of a particular command into file(log file)
suppose after login into remote shell i give the command ls -l  (i.e. exp_send "ls -l\r")
then how can i save the result of that command in file...
i found some hint like logfile command
but i donot know how to use it...
thanks in advance



Posted by admin (Graham Ellis), 2 August 2005
I would simply exp_send an ls command with a redirect out to file.

Alternative (if you want to analyse results as well) is to save the expect_out(buffer) array member to a file after you've done the ls.

Posted by pgpatel (pgpatel), 8 August 2005
Thanks for reply...
but i didnot get your answer...
can you explain me in detail with code as example please?
in my example i use ls as just example...in real i will use some other commands to get some system status...
thanks...


Posted by admin (Graham Ellis), 8 August 2005
Sample code that spawns a remote ping, grabs the text that comes back, and saves it to a file.

Code:
#!/usr/bin/expect
log_user 0
spawn ping -c 3 www.wellho.net
expect eof
set rres [open got.txt w]
puts $rres $expect_out(buffer)




This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho