import logging
import telnetlib
import time
class TelnetClient():
def __init__(self,):
self.tn = telnetlib.Telnet()
# 此函数实现telnet登录主机
def login_host(self,host_ip,username,password):
try:
# self.tn = telnetlib.Telnet(host_ip,port=23)
self.tn.open(host_ip,port=23)
except:
logging.warning('%s网络连接失败'%host_ip)
return False
# 等待login出现后输入用户名,最多等待10秒
self.tn.read_until(b'login: ',timeout=10)
self.tn.write(username.encode('ascii') + b'\n')
# 等待Password出现后输入用户名,最多等待10秒
self.tn.read_until(b'Password: ',timeout=10)
self.tn.write(password.encode('ascii') + b'\n')
# 延时两秒再收取返回结果,给服务
Python实现Telnet连接
最新推荐文章于 2024-06-16 22:45:19 发布