|
| 1 | +#!/usr/bin/python |
| 2 | +# -------------------------------- |
| 3 | +# This python script will get interface IP address configured on the fabric devices |
| 4 | +# -------------------------------- |
| 5 | +import os, sys, logging, getpass, time |
| 6 | +import argparse |
| 7 | +import paramiko |
| 8 | +from socket import error as SocketError |
| 9 | + |
| 10 | +class CommitConfig(object): |
| 11 | + def __init__(self, address): |
| 12 | + self.host_ip = address |
| 13 | + self.username = 'root' |
| 14 | + self.password = 'Embe1mpls' |
| 15 | + |
| 16 | + def connect_2_device(self): |
| 17 | + print 'Connecting.. '+ self.host_ip |
| 18 | + self.client = paramiko.SSHClient() |
| 19 | + self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 20 | + try: |
| 21 | + self.client.connect(self.host_ip, username=self.username, password=self.password) |
| 22 | + #print self.client |
| 23 | + return self.client |
| 24 | + except SocketError as err: |
| 25 | + print('\nSocket error: ') |
| 26 | + print(err.message) |
| 27 | + return None |
| 28 | + except paramiko.AuthenticationException as err: |
| 29 | + print('\nConnect failed, Auth error : ') |
| 30 | + print(err.message) |
| 31 | + return None |
| 32 | + except Exception as err: |
| 33 | + print('\nConnect failed, unknown error type : ') |
| 34 | + print(err.message) |
| 35 | + return None |
| 36 | + |
| 37 | + def commit_config(self): |
| 38 | + print 'Commit configuration .. '+ self.host_ip |
| 39 | + configure = self.client.invoke_shell() |
| 40 | + configure.send('cli\n') |
| 41 | + configure.send('configure\n') |
| 42 | + configure.send('set system services ssh\n') |
| 43 | + configure.send('set system services netconf ssh\n') |
| 44 | + configure.send('set system services netconf traceoptions file netconf-ops.log\n') |
| 45 | + configure.send('set system services netconf traceoptions file size 3m\n') |
| 46 | + configure.send('set system services netconf traceoptions file files 20\n') |
| 47 | + configure.send('set system services netconf traceoptions file world-readable\n') |
| 48 | + configure.send('set system services netconf traceoptions flag all\n') |
| 49 | + configure.send('commit\n') |
| 50 | + configure.send('quit\n') |
| 51 | + time.sleep(2) |
| 52 | + print 'Commit Done!' |
| 53 | + #op = configure.recv(1048576) |
| 54 | + #print op |
| 55 | + |
| 56 | + def close_connection(self): |
| 57 | + print('Closing connection...' + self.host_ip) |
| 58 | + self.client.close() |
| 59 | + |
| 60 | + |
| 61 | +def parse_options(args): |
| 62 | + parser = argparse.ArgumentParser( |
| 63 | + description='Commit netconf configuration on vQFX fabric devices' |
| 64 | + ) |
| 65 | + parser.add_argument('-a', '--device_ip', required=True, help='Fabric device IP address') |
| 66 | + |
| 67 | + opt = parser.parse_args() |
| 68 | + return opt |
| 69 | + |
| 70 | +def main(argv): |
| 71 | + options = parse_options(argv) |
| 72 | + config = CommitConfig(options.device_ip) |
| 73 | + res = config.connect_2_device() |
| 74 | + if res is not None: |
| 75 | + config.commit_config() |
| 76 | + config.close_connection() |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + main(sys.argv[1:]) |
0 commit comments