Python批量备份网络设备配置文件

个人没有系统学习过Python,编程能力也很烂,但是可以通过简单的程序来简化自己的工作量,脚本如下:

import paramiko
import xlrd
import time
import random
import os
#定义连接SSH的过程
def SSH_Connect(ssh_host, ssh_hostname):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=ssh_host, port=22, username='admin', password='admin@admin')
# 使用交互式终端
channel = ssh.invoke_shell()
time.sleep(1)
channel.send('terminal length 0\nshow run' + '\n')
# 这里等待时间为了让返回字符写入缓存中,根据配置量来进行设置
time.sleep(10)
temp = channel.recv(120000).decode()
# 这里调用另外一个过程,将缓存中的配置内容进行处理并保存
Get_Conf(temp, ssh_hostname)
ssh.close()
#本函数的功能,由于通过SSH获取下来的内容存在大量空行,需要进行格式化。
#由于不会在缓存阶段处理,索性先建立临时文件,然后读取临时文件进行格式化
#然后再进行写入相应的文件。
def Get_Conf(context, hostname):
#随机生成1000-200000的数字作为临时文件名称
random_file=random.randrange(1000, 200000)
f_path = r'./result_conf/' + hostname + '.log'
f_path_temp = r'./result_conf/' + random_file
f_temp = open(f_path_temp, 'w+')
f_temp.write(context)
f_temp.close()
f_temp = open(f_path_temp, 'r')
f = open(f_path, 'w+')
for line in f_temp.readlines():
if line == '\n':
line = line.strip('\n')
f.write(line)
f_temp.close()
f.close()
os.remove(f_path_temp)
#初始过程,主要是从EXCEL表格中读取相应的IP地址和主机名
def Get_Info():
workbook = xlrd.open_workbook(r'./conf/conf.xlsx')
sheet = workbook.sheet_by_name('conf')
nrows = sheet.nrows
for i in range(nrows-1):
cell_hostname = sheet.cell(i+1, 0).value
cell_ip = sheet.cell(i+1, 1).value
print('正在执行:\t', cell_hostname, '\t', cell_ip)
SSH_Connect(cell_ip, cell_hostname)
Get_Info()
import paramiko import xlrd import time import random import os #定义连接SSH的过程 def SSH_Connect(ssh_host, ssh_hostname): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=ssh_host, port=22, username='admin', password='admin@admin') # 使用交互式终端 channel = ssh.invoke_shell() time.sleep(1) channel.send('terminal length 0\nshow run' + '\n') # 这里等待时间为了让返回字符写入缓存中,根据配置量来进行设置 time.sleep(10) temp = channel.recv(120000).decode() # 这里调用另外一个过程,将缓存中的配置内容进行处理并保存 Get_Conf(temp, ssh_hostname) ssh.close() #本函数的功能,由于通过SSH获取下来的内容存在大量空行,需要进行格式化。 #由于不会在缓存阶段处理,索性先建立临时文件,然后读取临时文件进行格式化 #然后再进行写入相应的文件。 def Get_Conf(context, hostname): #随机生成1000-200000的数字作为临时文件名称 random_file=random.randrange(1000, 200000) f_path = r'./result_conf/' + hostname + '.log' f_path_temp = r'./result_conf/' + random_file f_temp = open(f_path_temp, 'w+') f_temp.write(context) f_temp.close() f_temp = open(f_path_temp, 'r') f = open(f_path, 'w+') for line in f_temp.readlines(): if line == '\n': line = line.strip('\n') f.write(line) f_temp.close() f.close() os.remove(f_path_temp) #初始过程,主要是从EXCEL表格中读取相应的IP地址和主机名 def Get_Info(): workbook = xlrd.open_workbook(r'./conf/conf.xlsx') sheet = workbook.sheet_by_name('conf') nrows = sheet.nrows for i in range(nrows-1): cell_hostname = sheet.cell(i+1, 0).value cell_ip = sheet.cell(i+1, 1).value print('正在执行:\t', cell_hostname, '\t', cell_ip) SSH_Connect(cell_ip, cell_hostname) Get_Info()
import paramiko
import xlrd
import time
import random
import os
#定义连接SSH的过程
def SSH_Connect(ssh_host, ssh_hostname):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=ssh_host, port=22, username='admin', password='admin@admin')
    # 使用交互式终端
    channel = ssh.invoke_shell()
    time.sleep(1)
    channel.send('terminal length 0\nshow run' + '\n')
    # 这里等待时间为了让返回字符写入缓存中,根据配置量来进行设置
    time.sleep(10)
    temp = channel.recv(120000).decode()
    # 这里调用另外一个过程,将缓存中的配置内容进行处理并保存
    Get_Conf(temp, ssh_hostname)
    ssh.close()
#本函数的功能,由于通过SSH获取下来的内容存在大量空行,需要进行格式化。
#由于不会在缓存阶段处理,索性先建立临时文件,然后读取临时文件进行格式化
#然后再进行写入相应的文件。
def Get_Conf(context, hostname):
    #随机生成1000-200000的数字作为临时文件名称
    random_file=random.randrange(1000, 200000)
    f_path = r'./result_conf/' + hostname + '.log'
    f_path_temp = r'./result_conf/' + random_file
    f_temp = open(f_path_temp, 'w+')
    f_temp.write(context)
    f_temp.close()
    f_temp = open(f_path_temp, 'r')
    f = open(f_path, 'w+')
    for line in f_temp.readlines():
        if line == '\n':
            line = line.strip('\n')
        f.write(line)
    f_temp.close()
    f.close()
    os.remove(f_path_temp)
#初始过程,主要是从EXCEL表格中读取相应的IP地址和主机名
def Get_Info():
    workbook = xlrd.open_workbook(r'./conf/conf.xlsx')
    sheet = workbook.sheet_by_name('conf')
    nrows = sheet.nrows
    for i in range(nrows-1):
        cell_hostname = sheet.cell(i+1, 0).value
        cell_ip = sheet.cell(i+1, 1).value
        print('正在执行:\t', cell_hostname, '\t', cell_ip)
        SSH_Connect(cell_ip, cell_hostname)

Get_Info()

发表评论

您的电子邮箱地址不会被公开。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据