栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

python------备份设备配置脚本(二)

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python------备份设备配置脚本(二)

 

字典型

sw1上

[SW1]user-interface vty 0 4

[SW1-ui-vty0-4]screen-length 0

sw2上

[SW2]user-interface vty 0 4

[SW2-ui-vty0-4]screen-length 0

SW1_info = {"ip_addr":"192.168.100.1",
           "username":"dyx",
           "passwd":"Huawei@123",
           "device_name":"SW1",
           "device_type":"交换机"}

SW2_info = {"ip_addr":"192.168.100.2",
           "username":"dyx",
           "passwd":"Huawei@123",
           "device_name":"SW2",
           "device_type":"交换机"}

database = [SW1_info, SW2_info]

#display current-configuration 查看当前的配置
#display saved configueation
#display current-configuration | include ip route 命令筛选

import paramiko
import time
from datetime import datetime
from device import *
for i in range(len(database)):

    #创建一个SSH客户端
    ssh_client = paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh_client.connect(hostname=database[i]["ip_addr"],username=database[i]["username"],password=database[i]["passwd"])

    command = ssh_client.invoke_shell()
        #给设备发送“查看配置”命令
    command.send("system-viewn"
             "user-interface vty 0 4 n"
             "screen-length 0 n".encode())
    command.send("dis cun".encode())
    time.sleep(3)
    recv = command.recv(65535).decode()
    #print(recv)
    #recv 就是设备给我们返回的结果,但是,如果直接将这个结果写入文件
    #文件呈现的效果是,一行结束,会多一个回车
    #处理方法:recv这个大文件,通过.splitlines(),将它的每一行进行切割,组成一个列表
    #遍历列表,即大文件的每一行,将每一行再写入配置文件即可
    resut = recv.splitlines()
    #print(resut)
    back_time = str(datetime.now())
    now_time = back_time[0:-7].replace(":",":")
    with open(f"{database[i]['device_name']}-{now_time}.txt",'w')as file_backup:
        for item in resut:
            print(item)
    #将回显内容保存到本地
            file_backup.write(item+"n")
ssh_client.close()

运行后:

查看带时间

在X-shell上

[SW3]user-interface vty 0 4

[SW3-ui-vty0-4]screen-length 0

import paramiko
import time
from datetime import datetime
from datetime import time as time1

ip_addr = "192.168.100.3"
username = "dyx"
passwd = "Huawei@123"
device = "SW3"

#创建一个SSH客户端
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_addr,username=username,password=passwd)

command = ssh_client.invoke_shell()
    #给设备发送“查看配置”命令
command.send("system-viewn"
         "user-interface vty 0 4 n"
         "screen-length 0 n".encode())
command.send("dis cun".encode())
time.sleep(3)
recv = command.recv(65535).decode()
resut = recv.splitlines()
#print(resut)
back_time = str(datetime.now())
now_time = back_time[0:-7].replace(":",":")
with open(f"{device}-{now_time}.txt",'w')as file_backup:
    for item in resut:
        print(item)
#将回显内容保存到本地
        file_backup.write(item+"n")
ssh_client.close()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/853747.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号