Reputation: 53
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
Upvotes: 1
Views: 11933
Reputation: 1650
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")
Upvotes: 1
Reputation: 487
You could go this way:
def is_connected(telnet_obj ):
answer = telnet_obj.read_all()
if "connected" in answer: #this test condition is not real is an example
return True
else:
return False
If you observe what yout router/switch returns you can test for that condition. In this case testing for the presence of a string or lack in the answer variable.
Upvotes: 1