新时代网工编程能力变成比敲命令更重要的能力了,大潮流已经无法改变。最近一直在研究python里面的netmiko作为ssh的方法。把第一个脚本共享出来大家研究一下。

方向比较简单,通过python的open方法读取iplist.txt里面的IP地址,这些ip就是所需要下发命令的交换机ip地址。然后读取cmdlist里面需要下发的哪些命令。通过python的netmiko ssh连接到交换机自动下发


h3c_check.py

coding:utf-8

import time
from netmiko import *
import sys
import os
import getpass

#读取iplist.txt 文档里面的ip地址
iplist = open('iplist.txt','r',encoding = 'ascii')
ip_addr = iplist.readlines()
iplist.close()

cmdlist = open('cmdlist.txt','r',encoding = 'ascii')
cmd_line = cmdlist.readlines()
cmdlist.close()

#遍历ip_addr这个列表里面的ip进行连接
for ip in iter(ip_addr):
print(ip)
try:
S5130 = {
'device_type':'hp_comware',
'ip':ip,
'username':'h3c',
'password':'h3c',
}

    net_connect = ConnectHandler(**S5130)

    for cmd in iter(cmd_line):
        cmd_result = net_connect.send_command(cmd)
        print('-----------------------------------------------------------------')
        print(cmd)
        print(cmd_result)
        print('-----------------------------------------------------------------')
        print('')
    net_connect.disconnect()
except (EOFError, NetMikoTimeoutException):
    print('Can not connect to Device')
except (EOFError, NetMikoAuthenticationException):
    print('username/password wrong!')

iplist.txt

192.168.11.112
192.168.11.110


cmdlist.txt

display vlan
display version
display ip inter brief