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,
Omkar

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.

#!/usr/bin/python

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")
tn.write("exit\n")

tn.read_all()

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,
Omkar

Be a part of the DaniWeb community

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