Search code examples
pythontelnettelnetlib

Python telnet connection successful


I'm wondering if it is possible to know if my telnet connection is successful?

So, if I'm connected to my switch and if I could write commands

telnet = telnetlib.Telnet(IP)
telnet.read_until(b"User Name:")
telnet.write(b"LOGIN\n")
telnet.read_until(b"Password:")
telnet.write(b"PASSWORD\n")
# Here I want to know if I'm connected

Solution

  • Don't use read_all if you plan on writing something after authentication. It blocks the connection until EOF is reached / connection is closed.

    First check the output telnet server is giving when an authentication is successful using putty or something else. read_untill the string to be matched after authentication.

    telnet = telnetlib.Telnet(IP)
    telnet.read_until(b"User Name:")
    telnet.write(b"LOGIN\n")
    telnet.read_until(b"Password:")
    telnet.write(b"PASSWORD\n")
    
    telnet.read_untill("string to be matched")