42

Does anyone know of an easy way to create a script that can connect to a telnet server, do some usual telnet stuff, and then log off? 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 automatically execute 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 to 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.

tRuEsAtM
  • 3,517
  • 6
  • 43
  • 83
TrentCoder
  • 758
  • 1
  • 6
  • 11
  • 1
    Use python telnetlib module. An example is here http://stackoverflow.com/questions/1491494/telnet-automation-scripting – Vanuan Jul 21 '11 at 16:57

11 Answers11

41

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 (basically) retrieves the routing table of a Cisco router.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Vatine
  • 20,782
  • 4
  • 54
  • 70
  • What is the purpose of parentheses  here? – Wakan Tanka Jul 17 '13 at 08:05
  • 7
    @WakanTanka Forcing the echos and sleeps to run in a sub-shell, sharing a stdout and actually have the sleep pause the output before the "quit" command is echoed, to allow the full routing table to print, before the connection dies. – Vatine Jul 17 '13 at 10:49
  • Excellent, so I can now use this to test a server with ( echo "^]"; echo "quit" ) | telnet mail.domain.co.uk 25 – Paul Littlefield Feb 17 '23 at 14:04
14

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
  • 268,207
  • 37
  • 334
  • 440
  • 1
    Expect implementations are also available in other languages, like Perl's Expect.pm module, if you don't want to learn TCL. – Josh Kelley Apr 02 '09 at 14:12
  • 1
    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 Apr 02 '09 at 17:09
  • 3
    TCL/TK sucks alot for having tools written with it - simply because the dependencies cannot be fully met on embedded systems ... forget about all the crap and just use netcat = almost no CPU load. – Martin Zeitler Dec 31 '12 at 10:14
  • Expect is Linux/Unix only. Windows is not supported. – YetAnotherRandomUser Jun 29 '17 at 02:07
12

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
  • 14,226
  • 3
  • 42
  • 60
  • Excellent answer, telnet wasn't working but piping it through nc instead worked. – I82Much Aug 03 '10 at 19:37
  • 5
    C'mon people, when you downvote, tell me why (especially years after the fact) – KevinDTimm Dec 18 '12 at 02:42
  • 1
    Your answer could simply have been a comment in Vatine's answer. Or an edit to his. There was no need to post an entirely new answer when you mention "same format as vatine shows". In SO, getting the best answer out is better than having a lot of answers cross referencing each other, or so I believe. BTW, I didn't downvote. – artfuldev Jun 13 '14 at 03:27
8

This vbs script reloads a cisco switch, make sure telnet is installed on windows.

Option explicit
Dim oShell
set oShell= Wscript.CreateObject("WScript.Shell")
oShell.Run "telnet"
WScript.Sleep 1000
oShell.Sendkeys "open 172.25.15.9~"
WScript.Sleep 1000
oShell.Sendkeys "password~"
WScript.Sleep 1000
oShell.Sendkeys "en~"
WScript.Sleep 1000
oShell.Sendkeys "password~"
WScript.Sleep 1000
oShell.Sendkeys "reload~"
WScript.Sleep 1000
oShell.Sendkeys "~"
Wscript.Quit
Arjan Keij
  • 81
  • 1
  • 1
  • Only solution that worked on Windows for me (tried the Linuxy like solutions on Cygwin on Windows, none of them worked for me) – ericcurtin Jan 23 '20 at 15:44
3
import telnetlib

user = "admin"
password = "\r"

def connect(A):
    tnA = telnetlib.Telnet(A)
    tnA.read_until('username: ', 3)
    tnA.write(user + '\n')
    tnA.read_until('password: ', 3)
    tnA.write(password + '\n')
    return tnA
def quit_telnet(tn)
    tn.write("bye\n")
    tn.write("quit\n")
Vishal Pawar
  • 4,324
  • 4
  • 28
  • 54
Harshan Gowda
  • 181
  • 2
  • 10
3

Check for the SendCommand tool.

You can use it as follows:

perl sendcommand.pl -i login.txt -t cisco -c "show ip route"
3

It may not sound a good idea but i used java and used simple TCP/IP socket programming to connect to a telnet server and exchange communication. ANd it works perfectly if you know the protocol implemented. For SSH etc, it might be tough unless you know how to do the handshake etc, but simple telnet works like a treat.

Another way i tried, was using external process in java System.exec() etc, and then let the windows built in telnet do the job for you and you just send and receive data to the local system process.

rizwan
  • 31
  • 1
0

I like the example given by Active State using python. Here is the full link. I added the simple log in part from the link but you can get the gist of what you could do.

import telnetlib

prdLogBox='142.178.1.3'
uid = 'uid'
pwd = 'yourpassword'

tn = telnetlib.Telnet(prdLogBox)
tn.read_until("login: ")
tn.write(uid + "\n")
tn.read_until("Password:")
tn.write(pwd + "\n")
tn.write("exit\n")
tn.close()
ryanskeith
  • 533
  • 4
  • 11
0

Bash shell supports this out-of-box, e.g.

exec {stream}<>/dev/tcp/example.com/80
printf "GET / HTTP/1.1\nHost: example.com\nConnection: close\n\n" >&${stream}
cat <&${stream}

To filter and only show some lines, run: grep Example <&${stream}.

kenorb
  • 155,785
  • 88
  • 678
  • 743
0

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
  • 339
  • 1
  • 6
-2

Write the telnet session inside a BAT Dos file and execute.

  • 2
    You cannot control / script an FTP session from inside a BATch file, you can only begin an FTP session. To script an FTP session you must use the script command `-s:filename.txt` to identify a text file containing your script. – James K Sep 18 '12 at 17:10
  • FTP? I believe the question is about a TELNET session – Sara Apr 27 '13 at 06:56