交換機用python定時備份

  • 用python的netmiko模塊,可以很好的實現對交換機的管理

  • 下面以H3C交換機爲例,採用tftp方式,對交換機每天凌晨3點進行備份

#!/usr/bin/env python
# encoding: utf-8


from netmiko import ConnectHandler

class switch(object):
    def __init__(self):
        self.switch_list = [
        '1.1.1.1',
        '2.2.2.2',
        ]



    def con_switch(self):
        for i in self.switch_list:
            huawei = {
            'device_type': 'huawei',
            'ip': i,
            'username': 'xxxx',
            'password': 'xxxx',
            'port' : 22,          
            'verbose': False,       # optional, defaults to False         
            }

            try:
                net_connect = ConnectHandler(**huawei)
            except expression as identifier:
                print i
                exit
            output = net_connect.send_command('tftp 3.3.3.3 put startup.cfg %s_startup.cfg' % i)
            print i + ':' + output




if __name__ == "__main__":
    run = switch()
    run.con_switch()
  • 其中1.1.1.1,2.2.2.2爲交換機地址,3.3.3.3爲tftp服務器地址。

  • tftp服務器搭建

  • 腳本寫完,在linux機器上跑定時就可以了

yum install xinetd tftp-server tftp -y
mkdir /data/network


  •  修改配置文件

cat /etc/xinetd.d/tftp    

# default: off
# description: The tftp server serves files using the trivial file transfer \
#       protocol.  The tftp protocol is often used to boot diskless \
#       workstations, download configuration files to network-aware printers, \
#       and to start the installation process for some operating systems.
service tftp
{
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -s /data/network/ -c
        disable                 = no
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}
  • 啓動服務

systemctl start xinetd.service
systemctl enable tftp.service



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章