背景
- 公司一共近100台交换机,例如想改名字,备份之类,每台都要连接非常麻烦,使用Python编写脚本,达到批量操作的目的。
相关代码
class telnet_client:
def __init__(self,ip):
self.host_ip=ip
self.tn=telnetlib.Telnet(self.host_ip,port=23,timeout=50)
self.tn.write('password'.encode(encoding='utf-8') + b'\n')
self.tn.write(b'enable' + b'\n')
self.tn.write('password'.encode(encoding='utf-8') + b'\n')
self.tn.write(b'terminal length 0' + b'\n')
def command(self,command):
self.command=command
self.tn.write(self.command.encode(encoding='utf-8') + b'\n')
#设定时间延迟,让命令有足够时间生效
time.sleep(1)
self.command_result=self.tn.read_very_eager().decode('utf-8')