python备份cisco交换机_Python 备份cisco ios 设备 简例

netmik库用来ssh,os库用来管理文件夹,datetime用于生成文件名

.

from netmiko import ConnectHandler

import os

import datetime

#define device type

cisco = {

'device_type':'cisco_ios',

'ip':'',

'username':'admin',

'password':'ccie',

'secret':'ccie',

}

.

#get ip address from a file

ip_addr = open("G:\\PythonProject_netmiko\\ipList.txt")

path ="G:\\PythonProject_netmiko\\"

folder_v = os.listdir() #查看當前文件夾所有文件

while True:

print("input your bak file folder name:")

folder_name = input()

if folder_name in folder_v:

print("there is a same name file already, please input again!")

else:

create_path = path + folder_name

os.mkdir(create_path)

print("Created folder:"+folder_name+" successfully!\n")

print("NOTE:the bakup file will be in: ",create_path)

break

for ip in ip_addr:

cisco['ip'] = ip.strip('\n')

print('Connecting: '+ip.strip("\n")+"...")

net_connect = ConnectHandler(**cisco)

print("Connected successfully !")

net_connect.enable()

run_config = net_connect.send_command('show run')

save_name = ip.strip() + datetime.datetime.now().strftime("-%Y%m%d-%H_%M_%S")

print('Backing up: '+ip.strip("\n")+'...')

f = open(create_path +"\\"+save_name+".txt",'w')

f.write(run_config)

f.close()

print("Backed up successfully!")

f = open(create_path +"\\0_IP-date-time.fileType",'w')#used to instruct

f.close()

ip_addr.close()

.

For minding codes

print("\n\n\nBacked up all devices compeleted ! ")

print("Please check the bakeup file in the follow folder: "+create_path)

.

Over~ Good luck to you!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值