Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Python Telnet Client

Status
Not open for further replies.

HoustonGuitarPro

Technical User
Feb 2, 2009
8
US
Does anyone know of Python Telnet script that is already available that will allow you to telnet to a machine and input commands?

So I am guessing bidirectional client?

I want to see how it is done for a programming project I am thinking of.
 
I have been able to make little progress. I can now make a connection, but I want to be able to read what is being transmitted to the client and display it on the python screen. Of course send input responses via the python script as well.

any clue how to do this? I would like it to be able to interpret not only ASCII but ANSI and IBM graphics if possible.

Python:
import os
import telnetlib

os.system("cls")
HOST = input("Enter Hostname: ")
PORT = input ("Port #: ")
tn = telnetlib.Telnet(HOST,PORT)

tn.read_all()
 

An example using telnetlib



Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind these jesus character, stars had to die for me to live.
 
i have come across this one and even though it shows how to make the connection, it shows to read the lines until it meets a certain criteria.

What I want to do is read the whole line and/or the EOF and display it as well as accept input if needed.

So..

If I connect and the first line is "Connected to XYZ" or whatever it is. I want to display that line as it comes in. Then go to the next line and start over. If there is no carriage return then it should be set to take input from the client.

What i really want to do is code a python based telnet client if possible so it will only want input when input is really required.

Make sense?
 

I want to display that line as it comes in. Then go to the next line and start over

Use one of the .read_* objects to get the message(s), Then use sys.stdout.write to 'print' the messages 'in-line' (concatenated)

Python:
sys.stdout.write(message)
sys.stdout.flush()

OR
Python:
 print(message)
To output each one on a separate line.

then loop around the read->print construct until the .read_* raises a EOFError

Or if you use read_until() stop when the required match is reached.




Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind these jesus character, stars had to die for me to live.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top