Hi Friends,

I had a issue with telnet conncection in python.

Can you send me a working module to connect to telnet windows from a linux machine using python scripting..

Thanks in Adwance,

Recommended Answers

All 3 Replies

Tested only with linux telnet server using python3:

from telnetlib import Telnet

tn = Telnet(HOST_IP)
telnet_data = tn.read_until(b'\r\r\n\r\n').decode('UTF-8')

HOST_IP -> byte object
In this example I read telnet data until EOL on linux machine.
In python2 you need a string as argument for read_until() method.

Why you cannot get simple example from docs.python.org?
Python2 telnet example

This was done on Python 2.7

This will logon to a host via telnet, run a single command and exit.


import getpass
import sys
import telnetlib

HOST = "<host IP>"
user = "<user id>"
password = "<user password>"

tn = telnetlib.Telnet(HOST)

tn.read_until("login: ")

tn.write(user + "\n")
tn.read_until("Password: ")
tn.write(password + "\n")
tn.read_until("~ $")
tn.write("<comman goes here\n")


hi CimmerianX.
Thanks for your reply.

I am using python2.6.6
Running your script on my linux machine throws me the below error.

tn.read_until("~ $")

File "/usr/lib64/python2.6/telnetlib.py", line 319, in read_until

    return self.read_very_lazy()

File "/usr/lib64/python2.6/telnetlib.py", line 395, in read_very_lazy

    raise EOFError, 'telnet connection closed'

EOFError: telnet connection closed

Please help me.

Thanks in Advance,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.