python自动抓取网络设备配置_用python脚本自动备份网络设备配置

在linux系统上写了个脚本,然后再写一个crontab定时任务,每天凌晨运行备份网络设备配置。

脚本内容在此备份一下。

#! /usr/bin/env python3

from netmiko import ConnectHandler #导入库

import os

import time

def cisco_ios (ip,username,password,secret): #定义cisco_ios函数

cisco = {

"device_type":"cisco_ios", #设备类型为“cisco_ios”

"ip":ip, #传入ip参数

"username":username, #传入登陆用户名

"password":password, #传入登陆密码

"secret":secret #传入sceret密码

}

command = ["show run"] #执行show run 命令。此列表可以加入多条命令

connect = ConnectHandler(**cisco) #对不同品牌设备的预定义的内容。这个地方建议看一下源码,我这一句话解释不太清楚。

full_config = ""

for cmd in command:

config = connect.send_command(cmd)

full_config += config

return full_config

connect.disconnect()

now = time.strftime("%Y-%m-%d", time.localtime()) #定义时间格式,备份文件名用到

if __name__ == "__main__":

host = ["10.163.4.39","10.163.0.126","10.163.1.253","10.163.1.254","10.163.1.133","10.163.1.134","10.163.1.135","10.163.1.129","10.163.1.130","10.163.1.131","10.16

3.1.132"] #需要备份的ip地址放入列表中

for ip in host: #循环列表,保存配置

conf = cisco_ios(ip, "admin", "admin123", "admin123")

with open(f"/root/backup/beijing/{ip}/cfg_bak_{ip}_{now}.txt", mode="a") as f1:

f1.write(conf) #保存到文件中

time.sleep(30) #每个IP之间等待30秒,给系统点反应时间

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
# $language = "VBScript" # $interface = "1.0" '作者;小怪兽怪瘦 Sub Main Const ForReading = 1, ForWriting = 2, ForAppending = 8 Dim fso,file1,line,str1,params Set fso = CreateObject("Scripting.FileSystemObject") Set file1 = fso.OpenTextFile("D:\backup\list.txt",Forreading, False) crt.window.show 3 crt.Screen.Synchronous = True do while file1.AtEndOfStream True '读取每一行 line = file1.ReadLine '分割IP,用户名,密码 特权密码 型号简写(z m h b) 位置 params = Split (line) ip = params(0) user = params(1) password = params(2) su = params(3) mode = params(4) whereis = params(5) crt.session.LogFileName = "D:\backup\log\"&ip&"_"&whereis&"_20190620.txt" '提供日志,修改后面日期即可 crt.session.Log(true) crt.Session.Connect ( "/SSH2 /L "&user&" /PASSWORD "&password&" "&ip) '建立SSH连接 if mode = "m" then 'm 代表迈普型号 crt.Screen.Send "enable" & chr(13) crt.Screen.WaitForString "assword:" crt.Screen.Send params(3) & chr(13) crt.Screen.WaitForString "#" '关闭more提示 crt.Screen.Send "more off" & chr(13) crt.Screen.WaitForString "#" '执行命令集 Set cmdfile = fso.OpenTextFile("D:\backup\cmd-mp.txt",Forreading, False) do while cmdfile.AtEndOfStream True line2 = cmdfile.ReadLine crt.Screen.Send line2 & Chr(13) crt.Screen.waitForString "#" loop crt.Screen.Send "show running-config" & chr(13) crt.Screen.WaitForString "#" '打开more提示 crt.Screen.Send "more on" & chr(13) crt.Screen.WaitForString "#" crt.Screen.Send "exit" & chr(13) crt.Screen.WaitForString ">" crt.Session.Disconnect end if if mode = "c" then 'c 代表思科型号 crt.Screen.WaitForString ">" crt.Screen.Send "enable" & chr(13) crt.Screen.WaitForString "assword:" crt.Screen.Send params(3) & chr(13) crt.Screen.WaitForString "#" '关闭more提示 crt.Screen.Send "configure terminal" & chr(13) crt.Screen.WaitForString "config)#" crt.Screen.Send "line vty 0 4" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "length 0" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "end" & chr(13) crt.Screen.WaitForString "#" '执行命令集 Set cmdfile = fso.OpenTextFile("D:\backup\cmd-cisco.txt",Forreading, False) do while cmdfile.AtEndOfStream True line2 = cmdfile.ReadLine crt.Screen.Send line2 & Chr(13) crt.Screen.waitForString "#" loop crt.Screen.Send "show running-config" & chr(13) crt.Screen.WaitForString "#" '打开more提示 crt.Screen.Send "configure terminal" & chr(13) crt.Screen.WaitForString "config)#" crt.Screen.Send "line vty 0 4" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "no length" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "end" & chr(13) crt.Screen.WaitForString "#" crt.Session.Disconnect end if if mode = "r" then 'r 代表锐捷型号 crt.Screen.WaitForString ">" crt.Screen.Send "enable" & chr(13) crt.Screen.WaitForString "assword:" crt.Screen.Send params(3) & chr(13) crt.Screen.WaitForString "#" '关闭more提示 crt.Screen.Send "configure terminal" & chr(13) crt.Screen.WaitForString "config)#" crt.Screen.Send "line vty 0 15" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "length 0" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "end" & chr(13) crt.Screen.WaitForString "#" '执行命令集 Set cmdfile = fso.OpenTextFile("D:\backup\cmd-rj.txt",Forreading, False) do while cmdfile.AtEndOfStream True line2 = cmdfile.ReadLine crt.Screen.Send line2 & Chr(13) crt.Screen.waitForString "#" loop crt.Screen.Send "show running-config" & chr(13) crt.Screen.WaitForString "#" '打开more提示 crt.Screen.Send "configure terminal" & chr(13) crt.Screen.WaitForString "config)#" crt.Screen.Send "line vty 0 15" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "no length" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "end" & chr(13) crt.Screen.WaitForString "#" crt.Session.Disconnect end if if mode = "z" then 'z 代表中兴型号 crt.Screen.Send "enable" & chr(13) crt.Screen.WaitForString "assword:" crt.Screen.Send params(3) & chr(13) crt.Screen.WaitForString "#" '执行命令集 Set cmdfile = fso.OpenTextFile("D:\backup\cmd-zx.txt",Forreading, False) do while cmdfile.AtEndOfStream True line2 = cmdfile.ReadLine crt.Screen.Send line2 & Chr(13) if (crt.Screen.WaitForString (" --More--",1)False) then crt.Screen.Send " " & chr(13) else crt.Screen.Send chr(13) end if crt.Screen.waitForString "#" loop 'crt.Screen.Send "show running-config" & chr(13) 'crt.Screen.WaitForString " --More--" 'crt.Screen.WaitForString "#" crt.Session.Disconnect

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值