Telnet automation / scripting

55,790

Solution 1

There's a python library for telnet connections that reads and writes from/to a telnet connection.

Check the link. It has some basic examples of what you are looking for.

Here's an example from the link:

import getpass
import sys
import telnetlib

HOST = "localhost"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until("login: ")
tn.write(user + "\n")
if password:
    tn.read_until("Password: ")
    tn.write(password + "\n")

tn.write("ls\n")
tn.write("exit\n")

print tn.read_all()

It connects to a telnet server. Sends your login credentials and then executes the unix command ls. Then exits the session and prints all output from the telnet server.

Solution 2

You may want to consider Exscript as well. It simplifies some of the easy tasks but for more complicated there is additional level of abstraction (Exscript is a scripting language in itself). Either way - worth checking out.

Solution 3

I've never used it myself, but maybe pexpect is what you need?

"Pexpect can be used for automating interactive applications such as ssh, ftp, passwd, telnet, etc."

Share:
55,790
Ayman
Author by

Ayman

Creator of JSyntaxPane. Working as Consultant and coding for fun.

Updated on July 09, 2022

Comments

  • Ayman
    Ayman almost 2 years

    I have already checked this question but could not find what I'm looking for. I am running Windows (the client), and the server is a legacy mainframe type server.

    Basically I need to write a script, python code or whatever, to send some know commands to the server via telnet, and preferable capture the output. Then return when done.

    What's the best approach?

  • Ayman
    Ayman over 14 years
    PExpect does not run on Windows clients. It gives an error that it needs a UNIX like system.
  • aman.gupta
    aman.gupta over 14 years
    I should read the questions more carefully, sorry! :)
  • Ayman
    Ayman over 14 years
    Just a note that on my server, I needed to add a \r instead of the \n. Took me a while to debug that.