https://download.csdn.net/download/i12344/89206376
交换机需要提前开启SSH,设置用户名和密码
需要登录的交换机IP地址放在 hosts.txt 每行一个IP地址
需要执行的命令放在commands.txt 每行一条命令,最后一条回车
hosts.txt和commands.txt需要和python文件在同一目录下
"""
需要执行的命令放置到 commands.txt
设备管理地址放到 hosts.txt
by Malijie 111@0t.net.cn
"""
from netmiko import ConnectHandler as ch
from netmiko import NetMikoAuthenticationException
hosts = open('hosts.txt', 'r')
username = 'your_username'
password = 'your_password'
for ip in hosts.readlines():
ip = ip.strip()
try:
host = {
'device_type': 'hp_comware',
'host': ip,
'username': username,
'password': password,
'port': 22,
}
conn = ch(**host)
commands = open('commands.txt','r')
output = conn.send_config_set(commands)
print(output)
print('='*100)
except TimeoutError:
print('!!!连接超时: ' + ip)
print('=' * 100)
except NetMikoAuthenticationException:
print('!!!用户名或密码错误: ' + ip)
print('=' * 100)
except KeyboardInterrupt:
print('用户退出')
break
except:
print('!!!错误:' + ip)
print('=' * 100)
username = 'your_username'
password = 'your_password'
修改为你的设备用户名和密码
执行,需要安装python环境和netmiko模块
pip install netmiko
执行命令
以Windows系统为例,打开cmd,执行 python + 脚本文件的路径
python D:\demo\1.py