Python通过paramiko模块备份H3C交换机配置

2020-12-12 20:33

阅读:735

标签:value   run   待完善   tail   xlsx   values   保存   info   blog   

1.过程思路
  1. 备份配置前,先保存交换机running config到starup config
  2. 交换机通过tftp备份配置文件
  3. 批量备份交换机配置(通过excel文件保存交换机IP)
name ip
SUZ-SW-101 10.X.X.1
SUZ-SW-102 10.X.X.2
SUZ-SW-103 10.X.X.3
SUZ-SW-104 10.X.X.4

2.python代码

import xlrd
import paramiko
import time

def ssh_SW(name,ip):
    now = time.strftime("%Y%m%d", time.localtime(time.time()))
    trans = paramiko.Transport((ip, 22))
    trans.connect(username=‘admin‘, password=‘passwd‘)
    ssh = paramiko.SSHClient()
    ssh._transport = trans
    stdin, stdout, stderr = ssh.exec_command(‘save‘)
    print(stdout.read().decode())
    stdin, stdout, stderr = ssh.exec_command(‘tftp 10.x.x.100 put startup.cfg ‘ + name + ‘-‘ + now + ‘.cfg‘)
    print(stdout.read().decode())
    trans.close()

def main():
    workbook = xlrd.open_workbook(‘./sw.xlsx‘)
    sheet = workbook.sheet_by_name(‘Sheet1‘)
    count = sheet.nrows
    for i in range(count-1):
        i = i + 1
        rows = sheet.row_values(i)
        name = rows[0]
        ip = rows[1]
        ssh_SW(name,ip)

if __name__=="__main__":
    main()

3.脚本很简单,还有待完善

技术图片

paramiko详解见以下链接

https://blog.csdn.net/appke846/article/details/80514024

Python通过paramiko模块备份H3C交换机配置

标签:value   run   待完善   tail   xlsx   values   保存   info   blog   

原文地址:https://blog.51cto.com/wcc127/2406932


评论


亲,登录后才可以留言!