Python的telnetlib模块使用

telnetlib模块的常用接口

telnetlib.Telnet(host, port, timeout)            # 登录
write()                                            # 输入命令
read_until(match)                                # 读出响应,直到读取到指定字符串
close()                                            # 登出

一个简单的例子

import telnetlib

Host = "a.b.c.d"

# 连接Telnet服务器
tn = telnetlib.Telnet(Host, port=23, timeout=10)
tn.set_debuglevel(0)

# 输入登录用户名
tn.read_until(b'login: ')
tn.write(b"zte" + b'\n')

# 输入登录密码
tn.read_until(b'Password: ')
tn.write(b"Telnet@1234" + b'\n')

tn.read_until(b'#')
tn.write(b"cd /home/sd" + b'\n')

tn.read_until(b'#')
tn.write(b"ls -al" + b'\n')

r = tn.read_until(b'#').decode('ASCII')
r1 = r.split(r"\r\n")
for i in r1:
    print(i)

tn.close()

猜你喜欢

转载自www.cnblogs.com/chusiyong/p/12206361.html