码迷,mamicode.com
首页 > 编程语言 > 详细

使用python脚本利用SSH协议通过TFTP备份批量备份山石防火墙配置

时间:2017-07-12 23:21:01      阅读:1249      评论:0      收藏:0      [点我收藏+]

标签:tftp ssh python

    

    #!/usr/bin/python

    #-*- coding: utf-8 -*-

    import re

    import paramiko          #引入ssh模块,该模块需要单独安装。

    import time

    LogTime = time.strftime(‘%Y-%m-%d_%H-%M-%S‘)

    tftp = raw_input(‘Please Enter TFTP Sever IP:‘)

    import Hillstone_icmp

    action = raw_input(‘‘‘Please Select Action :

        1:Config & Backup;

        2:Backup;

    Put Your Choose:‘‘‘)

    for line in open("HS_IP_True.txt"):

        hostname = line.replace(‘\n‘,‘‘)

        temp = open(‘HS_temp.txt‘,‘w‘)

        port = 22

        username = ‘666666‘

        password = ‘666666‘

        if action == ‘2‘:

            try:

                client = paramiko.SSHClient()

                client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

                client.connect(hostname, port, username, password, timeout=5)

                remote_conn = client.invoke_shell()

                remote_conn.send(‘show version \n‘)

                time.sleep(2)

                remote_conn.send(‘\n‘)

                out = remote_conn.recv(temp)

                time.sleep(2)

                DeviceName = (re.findall(‘.*(?=#)‘,out))[0]

                tftp_cli = "export configuration startup to tftp server %s %s-%s..cfg" %(tftp,DeviceName,LogTime)

                remote_conn.send(tftp_cli+‘\n‘)

                time.sleep(2)

                print  hostname,‘Backup Success !!‘

            except :

                print hostname,‘Backup Failed !!‘


本文出自 “阿建” 博客,请务必保留此出处http://hardwork.blog.51cto.com/2529098/1946788

使用python脚本利用SSH协议通过TFTP备份批量备份山石防火墙配置

标签:tftp ssh python

原文地址:http://hardwork.blog.51cto.com/2529098/1946788

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!