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
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems

Phone System reconfiguration 11.1.2018 to 26.1.2018 - we are on email but incoming landline not available. Temp number 0797 4 925928
telnet login using expect

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

spawn telnet
expect "login:"
exp_send  "myname\r"
expect "password: "
exp_send "123456\r"

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?


Posted by pgpatel (pgpatel), 29 July 2005
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

Posted by Custard (Custard), 29 July 2005

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.



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

# Set the DISPLAY environment variable (as a test)
set env(DISPLAY) chilli:0
spawn telnet
# 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 {\$}

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?



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 real i will use some other commands to get some system status...

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.

log_user 0
spawn ping -c 3
expect eof
set rres [open got.txt w]
puts $rres $expect_out(buffer)

This page is a thread posted to the opentalk forum at 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., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho