Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- devices.py
- class NWDevice:
- name = ''
- ipaddr = ''
- login = ''
- password = ''
- command = ''
- file = ''
- repo = ''
- devicelist = []
- def init_devices():
- device = NWDevice()
- device.name = 'SWAC_0801'
- device.ipaddr = '192.168.8.221'
- device.login = 'backupd'
- device.password = 'password'
- device.command = 'show configuration | display set | no-more'
- device.file = 'fullconfig.cfg'
- device.repo = 'ssh://git@bitbucket.loc:10999/ii/swac_0801.git'
- devicelist.append(device)
- device = NWDevice()
- device.name = 'SWAC_0802'
- device.ipaddr = '192.168.8.222'
- device.login = 'backupd'
- device.password = 'password'
- device.command = 'show configuration | display set | no-more'
- device.file = 'fullconfig.cfg'
- device.repo = 'ssh://git@bitbucket.loc:10999/ii/swac_0802.git'
- devicelist.append(device)
- backupconfig.py
- from paramiko import client
- import time
- import git
- import os
- from subprocess import call
- import threading
- from devices import init_devices
- from devices import devicelist
- import logging
- import sys
- class LogPipe(threading.Thread):
- def __init__(self, level):
- threading.Thread.__init__(self)
- self.daemon = False
- self.level = level
- self.fdRead, self.fdWrite = os.pipe()
- self.pipeReader = os.fdopen(self.fdRead)
- self.start()
- def fileno(self):
- return self.fdWrite
- def run(self):
- for line in iter(self.pipeReader.readline, ''):
- logging.log(self.level, line.strip('\n'))
- self.pipeReader.close()
- def close(self):
- os.close(self.fdWrite)
- class ssh:
- client = None
- def __init__(self, address, port, username, password):
- self.client = client.SSHClient()
- self.client.set_missing_host_key_policy(client.AutoAddPolicy())
- self.client.connect(address, port=port, username=username, password=password, look_for_keys=False)
- def sendCommand(self, command):
- if(self.client):
- stdin, stdout, stderr = self.client.exec_command(command)
- while not stdout.channel.exit_status_ready():
- # Print data when available
- if stdout.channel.recv_ready():
- alldata = stdout.channel.recv(1024)
- prevdata = b"1"
- while prevdata:
- prevdata = stdout.channel.recv(1024)
- alldata += prevdata
- return (str(alldata,'utf-8'))
- else:
- return ""
- def backup(device):
- logging.info('Backup '+device.file+' from '+device.name+' ['+device.ipaddr+']')
- #connect to device
- connection = ssh(device.ipaddr,device.login,device.password)
- answer = connection.sendCommand(device.command)
- #get config
- configfile = '/home/python/repos/'+device.name+'/'+device.file
- dir = os.path.dirname(configfile)
- if not os.path.exists(dir):
- os.makedirs(dir)
- f=open(configfile,'w')
- f.write(answer)
- f.close()
- logging.info('Save config to: '+configfile)
- logpipe = LogPipe(logging.INFO)
- #check git repo
- if not os.path.exists(dir+'/.git'):
- call(['git','init'],cwd=dir,stdout=logpipe,stderr=logpipe)
- call(['git','add','--all'],cwd=dir,stdout=logpipe,stderr=logpipe)
- call(['git','remote','add','origin',device.repo],cwd=dir,stdout=logpipe,stderr=logpipe)
- logging.info('Create GIT repo: '+device.repo)
- #commit changes
- message = 'Changes '+time.strftime("%d %b %Y %H:%M")
- call(['git','commit','-a','-m',message],cwd=dir,stdout=logpipe,stderr=logpipe)
- call(['git','push','-u','origin','master'],cwd=dir,stdout=logpipe,stderr=logpipe)
- logpipe.close()
- return 0
- #configure log
- logging.basicConfig(filename='/var/log/backupconfig.log', level=logging.INFO, format='%(asctime)s %(message)s')
- logging.info('RUN Script')
- #load devices configuration
- init_devices()
- for device in devicelist:
- backup(device)
Add Comment
Please, Sign In to add comment