import paramiko
import datetime
import time
f=open('./hostnames.txt','r')
hostnames=f.readlines()
#hostnames=['1.1.1.1','2.2.2.2','3.3.3.3','192.168.99.10','192.168.99.20']
#连接登录设备
for hostname in hostnames:
hostname=hostname.strip()
ssh_client=paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=hostname,username='python',password='123456')
print('Sucessfully conneted to ',hostname)
#提取网络设备主机名
cmd=ssh_client.invoke_shell()
cmd.send('sys\n')
cmd.send('dis curr | include sysname\n')
time.sleep(1)
result=cmd.recv(66666)
result=result.decode('ascii')
items=result.splitlines()
for item in items:
item=item.strip()
if item.startswith('sysname'):
device_name=item.split(' ')[-1]
#修改网络设备主机名
device_name=device_name+'-35'
cmd.send(f'sys {device_name}\n')
time.sleep(1)
cmd.send('quit\n')
cmd.send('save\n')
time.sleep(1)
cmd.send('y\n')
#print(result)
print('the sysname operation has finshed!!!')
ssh_client.close()
标签:hostname,批量,Python,cmd,send,item,result,网络设备,ssh
From: https://www.cnblogs.com/tang-learning/p/16846409.html