views:

2888

answers:

5

Hello,

Does anyone know of an easy way to create a script that can connect to a telnet server, do some common telnety stuff, and then logoff? I am dealing with users who are not familiar with telnet and the commands they will need to run. All I want is for them to double-click on a script, and have that script auto-magically run the commands for them.

You're probably wondering, "What platform are the users on?" They will be on both Windows and Linux. Implementations in languages like Perl, Java, or Python are acceptable. I see that Perl has a Net::Telnet module. Has anyone used that?

My ideal solution would be two create two script files...a BAT file for windows, and a shell script for Linux. While this would make dual maintenance an issue, it would mean I wouldn't have to install Perl/Java/Python/etc... on every machine. Unfortunately, I have not seen any way to automate a telnet session with batch files or shell scripts.

Thanks.

+7  A: 

Expect is built for this and can handle the input/output plus timeouts etc. Note that if you're not a TCL fan, there are Expect modules for Perl/Python/Java.

EDIT: The above page suggests that the Wikipedia Expect entry is a useful resource :-)

Brian Agnew
Expect implementations are also available in other languages, like Perl's Expect.pm module, if you don't want to learn TCL.
Josh Kelley
Sorry, Harper. But it's good to know that it's an appropriate answer :-)
Brian Agnew
The Expect distribution includes a script called autoexpect which will generate an expect script by looking at a session. The generated script is simple enough to tweak.
sigjuice
A: 

I've used various methods for scripting telnet sessions under unix, but the simplest one is probably a sequence of echo and sleep commands, with their output piped into telnet. Piping the output into another command is also a possibility.

Silly example

(echo password; echo "show ip route"; sleep 1; echo "quit" ) | telnet myrouter

This (basicallly) retrieves the routing table of a Cisco router.

Vatine
+1  A: 

Another method is to use netcat (or nc, dependent upon which posix) in the same format as vatine shows or you can create a text file that contains each command on it's own line.

I have found that some posix' telnets do not handle redirect correctly (which is why I suggest netcat)

KevinDTimm
Excellent answer, telnet wasn't working but piping it through nc instead worked.
I82Much
A: 

While not directly related to your question, I would recommend you use ssh instead, as you can execute specific commands via it (plus it's more secure by default).

Brian Mitchell
A: 

Couple of questions:

  1. Can you put stuff on the device that you're telnetting into?
  2. Are the commands executed by the script the same or do they vary by machine/user?
  3. Do you want the person clicking the icon to have to provide a userid and/or password?

That said, I wrote some Java a while ago to talk to a couple of IP-enabled power strips (BayTech RPC3s) which might be of use to you. If you're interested I'll see if I can dig it up and post it someplace.

BonkaBonka