手把手教网络工程师写python脚本,实现ssh登录交换机修改配置
作者:互联网
Netmiko在window系统下安装
pip3 install Netmiko(linux、mac也可以使用该命令)
没报错,就说明Netmiko安装没问题。
python脚本代码
from netmiko import ConnectHandler
alongsw = {
'device_type': 'huawei',
'ip':'192.168.1.200',
'username':'along',
'password':'along123',
}
connect = ConnectHandler(**alongsw)
print("恭喜阿龙成功登录交换机"+ alongsw['ip'])
config_commands = ['vlan 100 ','name 20210103']
output = connect.send_config_set(config_commands)
print(output)
result = connect.send_command('dis vlan')
print(result)
交换机SSH相关配置
[along]user-interface vty 0 4
[along-ui-vty0-4]authentication-mode aaa
[along-ui-vty0-4]protocol inbound ssh
[along]aaa
[along-aaa]local-user along password irreversible-cipher
[along-aaa] local-user along privilege level 15
[along-aaa]local-user along service-type ssh
[along]ssh user along authentication-type password
[along]ssh user along service-type stelnet
[along]stelnet server enable
测试一下ssh,能不能登录?
代码执行报错,解决办法
报错:
netmiko.ssh_exception.NetmikoTimeoutException: Paramiko: 'No existing session' error: try increasing 'conn_timeout' to 10 seconds or larger.
解决办法:
标签:aaa,python,手把手,报错,ssh,along,type,user 来源: https://blog.51cto.com/u_15127557/2701411