[英]Python telnet connection successful
I'm wondering if it is possible to know if my telnet connection is successful?我想知道是否可以知道我的 telnet 连接是否成功?
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
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.
在这种情况下,测试答案变量中是否存在字符串或缺少字符串。
Don't use read_all
if you plan on writing something after authentication.如果您打算在身份验证后编写一些内容,请不要使用
read_all
。 It blocks the connection until EOF
is reached / connection is closed.它会阻止连接,直到达到
EOF
/连接关闭。
First check the output telnet server is giving when an authentication is successful using putty or something else.首先检查输出 telnet 服务器在使用腻子或其他东西进行身份验证成功时给出的输出。
read_untill
the string to be matched after authentication. read_untill
认证后要匹配的字符串。
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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.