Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Telnet with Expect

Status
Not open for further replies.

sumeet1802

IS-IT--Management
Jul 7, 2003
3
CA
Hi ,

I am trying to telnet to a server using an expect script which I want to be embedded in Perl.
Once a telnet session is established I should be able to send commands and get data from the result.
I have implemented so that I am able to send commands and get the complete session on a file. I would have to search and parse data and do all sort of editing.
I need a live telnet session which should be able to send commands and get results in one session.
The script used so far is:-
#!/usr/pde/tcl/bin/hppa/expect
spawn /usr/bin/telnet servername
expect login:
send "name password"
send "\n"
expect "TERM = (xterm)"
expect "SOME_PROMPT (I.E. $>)"
send "commands\r"
expect "SOME_PROMPT (I.E. $>)"
send "more commands \r"
expect "SOME_PROMPT (I.E. $>)"
send "logout"\n"
send "\n"
interact {
}

There is some problem at logout .......too .....

Need help for the same ...

Thanks,
Sumeet
 
Not exactly sure what you are trying to do, but you can try this:

#!/usr/bin/expect --
spawn /usr/bin/telnet
expect "telnet>"
send "SERVERNAME\r"
expect "ogin:" # Do this for portability
send "LOGIN_NAME\r"
expect "ssword:" # Do this for portability, you can add the 'a' if you want.
send "PASSWORD\r"
??? expect "TERM = (xterm)" ???
# What is this for? If this is part of the profile, it should not display.
expect "$>"
send "COMMAND1\r"
expect "$>"
send "COMMAND2\r"
expect "$>"
send "logout, exit, etc\r"
#EOS

You shouldn't need an interact. Each send line should be followed with \r", not \n".

On your logout line you have listed, you have 'send "logout"\n"', to many quotes.
 
Great Thanks for the help !!!
Actually I am embedding the expect script in Perl ( I am confortable with). So I want that from the web the user should be able to send some commands and the commands be sent to the server through expect telnet script.
Now if I have to send a command say exit I can hardcode it in expect script. But what do I do if it should be a command to be entered by the user at the web interace. I cannot hardcode it so how do I set it ?


So once again there is a web interface provided by Perl where it will take inputs from the user, send it to the expect script which would telnet and then send commands to the server which are entered by the user.

Any ideas ..?

Thanks for the help,
Sumeet

 
Why not try this, it should be faster and cleaner than having to embed Expect within your PERL script.


This PERL module allows you to execute telnet and then other commands. You are inviting a security risk by doing this, so I would set up the CGI to perform the telnet, but based on input from the user, run only certain commands, unless you are in a very trusted environment. Something like, a page with check boxes that have the commands pre-specified and then run those through a loop or something of that nature.
 
GREAT THANKS A BUNCH ....
I GUESS THIS WILL WORK ....LOOKS NEAT ........
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top