通过ciscolib模块中telnet-23端口批量配置交换机:
下面再讲通过paramiko模块下ssh安全批量配置再下图:
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import ciscolib
def main():
PASSWORD="12345678" #//测试环境
USERNAME="admin"
ENABLE_PWD="admin123"
for ip in open('sw3560.txt').readlines():
ip = ip.strip()
if USERNAME != "":
switch = ciscolib.Device(ip, PASSWORD, USERNAME, ENABLE_PWD)
else:
switch = ciscolib.Device(ip, PASSWORD, enable_password=ENABLE_PWD)
try:
switch.connect()
print("Logged into %s,Successful" % ip)
except ciscolib.AuthenticationError as e:
print("Couldn't connect to %s: %s" % (ip, e.value))
continue
except Exception as e:
print("Couldn't connect to %s: %s" % (ip, str(e)))
continue
switch.enable(ENABLE_PWD)
switch.cmd("enable")
switch.cmd("c#andphp\n")
switch.cmd("copy running ftp:")
switch.cmd("192.168.1.5")
switch.cmd(ip + "config.text") #//自动变化配置文件名称
switch.disconnect()
if __name__ == '__main__':
main()
通过ssh加密方式配置:
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import paramiko
import time
import getpass
username = raw_input("Username:")
password = getpass.getpass("Password:")
f = open("iplist.txt","r")
for line in f.readlines():
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)
print "Sucessfully login to ", ip
command = ssh_client.invoke_shell()
command.send("enable\n")
command.send("c#andphp\n")
command.send("copy running ftp:\n")
command.send("192.168.1.5\n")
command.send (ip + "config.text\n") #//这里不要设置交换机命令exit退出,否则会出现问题,这里掉过一个坑。
time.sleep(1)
output = command.recv(65535)
print output
f.close()
ssh_client.close