Search code examples
pythontelnetpython-3.4

Writing through Telnet with Python


I'm still somewhat new to python and fiddling with telnet in general, but I've got an issue here. For some reason this code I have written doesn't stop to let me actually write anything through the telnet terminal, it just runs to completion without stopping. I can't figure out what I'm doing wrong, can anyone help out?

import telnetlib
import time
HOST = input("IP Address: ")
command = b" "

tn = telnetlib.Telnet(HOST, port = 23, timeout = 20)
time.sleep(10)
tn.write(command + b"\n")
ret1 = tn.read_eager()
time.sleep(10)
print(ret1)
tn.write(b"001 rq version\n")
ret2 = tn.read_until(b"_DNE", timeout = 5)
time.sleep(10)
print(ret2)
tn.write(command + b"\n")

print("Success!")
tn.close()

EDIT: Sorry, I should've specified more clearly.

Basically, after it gets the IP, it connects to the host machine just fine. The problem after that is the line "tn.write(command + b"\n")", according to everything I've looked up this should allow the user to input whatever they want to type in, but the program does not stop to allow the user to type anything at all.


Solution

  • It looks to me like command is basically an empty string, and that doesn't seem to change at any point(except for the '\n'). Each time you do tn.write(command + b"\n"), you are just sending b"\n" down the wire. You're not prompting the user to type anything. Try something like:

    command = input("Enter command: ")
    tn.write(b"{}\n".format(command))
    

    In place of just writing:

    tn.write(command + b"\n")