Python批量配置Cisco交换机

前段时间也不知道在瞎忙什么,也没见什么成果加上11月份也没有发博文了。将之前Python写的批量管理配置交换机贴上来,供需要的网友提供一点借鉴,使用的是paramiko库:

[root@zabbix609 python]# cat py-ssh2960-arg.py
#!/usr/bin/env python
import paramiko
import time
import getpass
import sys
import socket

username = raw_input("Username:")
password = getpass.getpass("Password:")
ip_file = sys.argv[1]
cmd_file = sys.argv[2]

switch_with_authentication_issue = []
switch_not_reachable = []

f = open(ip_file,'r')
for line in f.readlines():
   try:
        ip = line.strip()
        ssh_client = paramiko.SSHClient()
        ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh_client.connect(hostname=ip,username=username,password=password,look_for_keys=False)
        print "You have sucessfully connect to ", ip
        command = ssh_client.invoke_shell()
        cmdlist = open(cmd_file,'r')
        cmdlist.seek(0)
        for line in cmdlist.readlines():
                command.send(line + "\n")
        time.sleep(2)
        cmdlist.close()
        output = command.recv(65535)
        print output
   except paramiko.ssh_exception.AuthenticationException:
        print "user authentication failed for" + ip + "."
        switch_with_authentication_issue.append(ip)
   except socket.error:
        print ip + "is not reachable,pls check network."
        switch_not_reachable.append(ip)
f.close()
ssh_client.close

print '\n User authentication failed for below switches:'
for i in switch_with_authentication_issue:
        print i
print '\n Below swithces are not reachable:'
for i in switch_not_reachable:
        print i

定义你要管理的交换机IP地址文件:

定义要配置交换机的命令:我这里测试只更新交换的时间

调用使用方法:

Python批量配置Cisco交换机_第1张图片

你可能感兴趣的:(Python)