Search code examples
pythontelnettelnetlib

How to get data from telnet using python without authentication


So guys, how can i retrieve data from telnet using host and port but without any authentication, without any password or login. On my terminal when i give command: telnet <myhost> <myport> it spits out bunch of data from the machine that is connected to that.

This is the data:

Trying <MyHost>...
Connected to <MyHost>.
Escape character is '^]'.
2018-08-03T17:40:45.0746|power|ON|mode|MANUAL|execution|READY|Xact|0.00|Yact|0.00|Zact|100.51|Xcom|0.00|Ycom|0.00|Zcom|0.00|path_feedrate|0.00|line|0|Block|0|program|Plate 1_imported_dxf.ORD
2018-08-03T17:40:45.0746|comms|NORMAL||||
2018-08-03T17:40:45.0746|Sspeed|0.00

And this is my python script, i can't get the result. Please help

import telnetlib
import time

print("===============================================")

host = "MyHost"
port = "MyPort"

connect = telnetlib.Telnet(host, port)

connect.write("term leng 0\n")
connect.read_until("term leng 0")

connect.write("sh ip int br\n")
connect.write("sh ver\n")

time.sleep(1)
print(connect.read_very_eager())
print("===============================================")

Solution

  • Use python sockets. Specifically, copy the 'echo client' code from here: https://docs.python.org/3/library/socket.html#example with your host/port.