Re: Telnet Script

Occasional Contributor

Telnet Script

I need a way to telnet from a WIN XP machine to a linux machine from somesort of .bat file to run a set amount of commands on the remote linux machine. Some of the commands do change so I need a file that I can edit easly.
(Note: the processor one the linux machine is very slow so running any language fancier the C will not work)

Any suggestion are greatly appreciated.
Balaji N
Honored Contributor

Re: Telnet Script

get this first. or

and then use this script
#!/usr/bin/expect -f
# This script telnets to a specific host as a specifc user and sets the required
# environment variables.

#Change default timeout
set timeout 100

#Variables. Change as necessary
set TERM xterm ;#default TERM
set prompt "(%|#|\\$) $" ;# default prompt

catch {set prompt $env(EXPECT_PROMPT)}

#Host Name
set SERVER "[lrange $argv 0 0]"
set USER "[lrange $argv 1 1]"
set PASSWD "[lrange $argv 2 2]"

#Command List
set COMMANDS "[lrange $argv 3 $argc]"

#Spawn Telnet
spawn telnet
expect "telnet> "

#Initialise a Connection
send "open $SERVER\r"

#Login to the Server
expect {
-re "Connection timed out" {
send_user "Unable to connect to $host. Exiting..."
exit 1
timeout {
send_user "Timed out connecting to $host. Exiting..."
exit 1
"login* "
send "$USER\r"
expect "Password:*"
send "$PASSWD\r"

expect {
-re "Login incorrect*" {
send_user "Looks like the password is wrong. Spawning a shell..."
exit 1
-re $prompt

#loop through arguments
foreach command $COMMANDS {
send "$command\r"
expect -re $prompt

send_user "Expect Scripts Ends. Good Bye!!!\n"

run this script as

scriptname server name password command(s)

Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
Steven E. Protter
Exalted Contributor

Re: Telnet Script

If you don't mind having a password in clear text in a file, someone posted this solution.

In a file (name ToDo) put all commands you want to pass in the telnet session.


In an other file "ScriptFile"
telnet @IP < ToDo

And execute ScriptFile

The password goes across the network clear text, so whats the big deal about having it in an open file.

Openssh would be a better alternative.

Steven E Protter
Owner of ISN Corporation
Honored Contributor

Re: Telnet Script

Could you not use ftp from XP the command file (script) and have a crontab job running every so often (that depend on reaction time is critical), let say 5 minutes for example.

You could use sftp from putty on the XP
this is can be downloaded from many places
in the Telnet category at Tucows

The stting up of the ssh (putty)is not that tedious and once you have your private/public key set up right it would be more secure

hope this track will be of use
whatever keep us informed.

Smile I will feel the difference
Occasional Contributor

Re: Telnet Script

Ok I tried the following command that was suggested above.

telnet < toDo.txt

and all that was returned was

Welcome to Microsoft Telnet Client
Excape Character is 'CTRL+]'

Yet when I go telnet I got the login prompt from my linux machine. I did have the 'telnet < toDo.txt' originally in a .bat file but when nothing happend I tried typing it into the command prompt manually instead.

Any suggestions are greatly appreciated.

New Member

Re: Telnet Script

I would use Perl, but that's me. You can install activeperl or cygwin. If you need help contact me.
New Member

Re: Telnet Script

If you ty to redirect its stdin or stdout, it will not work, it will die. That's how telnet works :(

BUT, take a look at:
Telnet Scripting Tool v.1.0
by Albert Yale

I think that homepage is not functional anymore, but it can be found literally anywhere on the Net (try Google, you find it).

It lets you write a very, very, very simple script, in which you put commands to be executed on the remote system, and stores output in a text file. After it sends a command, it waits for a specified answer, so the speed of the remote system is not relevant.

Can be envoked from .BAT, and Windows Scripting Host Files (.VBs, .JS)
Gustavo Feijo
New Member

Re: Telnet Script

I'm trying to do that in bash script, but I'm stopping at the same problem. How to send a password to a telnet login prompt?

:) gustavo
Stuart Browne
Honored Contributor

Re: Telnet Script

Actually, you can do some nasty echo, sleep's to get that to work. NOTE: I do not suggest you use this, but this is how it can work:

sleep 3 # This is to wait for the connection to occur
echo username
sleep 2 # Username will go in Login: prompt, this will wait a while for Passwrod:
echo password
sleep 2 # finish the login, get a prompt
echo "CommandToExecute; exit"
sleep XX # wait for command to complete, then exit.
) | telnet HOST

Now, whilst this will work in simple situations, it doesn't have any accountability with regards to network degredation, or server load. Using Expect takes these into consideration, simply by waiting for expected text.

The above example just takes 'guesses', in other words *IS NOT RELIABLE!*.
One long-haired git at your service...