python使用telnetlib库文件进行telnet登陆并且进行命令操作设备
import telnetlib
delay_time=1
def telnet_command(tn,command):
tn.write(command + '\n')
time.sleep(delay_time)
msg_list=[]
#i=0
while 1:
#pdb.set_trace()
#if i>=6:
#pdb.set_trace()
#print i
#i=i+1
temp=tn.read_until("--More-- press q to quit display:",5)
if temp.find('More')==-1:
msg_list.append(temp)
break
else:
msg_list.append(temp)
tn.write(' ')
time.sleep(delay_time)
#pdb.set_trace()
return ','.join(msg_list)
def telnet_login(tn,test_ip_address,telnet_username,telnet_password):
result=[]
try:
tn.open(test_ip_address,23)<