コード例 #1
0
ファイル: net_main.py プロジェクト: ilhampst/pynet
def detect_config_change():

    net_devices = NetworkDevice.objects.all()

    config_changed = False
    for a_device in net_devices:

        if 'cisco' in a_device.device_class:

            # check if config file exists
            if not a_device.cfg_file:
                print "Initial device backup: {}".format(a_device.device_name)
                backup_config(a_device)
                config_changed = True
                continue

            # Check if the configuration changed
            last_changed = int(snmp_wrapper(a_device, oid=global_params.OID_RUNNING_LAST_CHANGED))
            if last_changed > a_device.cfg_last_changed:
                print ">>>Running configuration changed: {}".format(a_device.device_name)
                config_diffs = backup_config(a_device)
                config_changed = True
                if config_diffs:
                    print "Sending email notification regarding changes\n"
                    subject = "Network Device Changed: {}".format(a_device.device_name)
                    send_mail(global_params.EMAIL_RECIPIENT, subject, config_diffs, global_params.EMAIL_SENDER)
            else:
                # Update last_changed field to handle reboot case
                a_device.cfg_last_changed = last_changed
                a_device.save()

    if config_changed:
        print "Checking configuration changes into git"
        git_handling()
コード例 #2
0
ファイル: net_main.py プロジェクト: ilhampst/pynet
def backup_config(a_device):

    DEBUG = True
    perform_diff = False

    if DEBUG: print "Retrieve device configuration via SSH: {}\n".format(a_device.device_name)
    ssh_connect = SSHConnection(a_device)
    ssh_connect.enable_mode()
    output = ssh_connect.send_command('show run\n')

    file_name = a_device.device_name + '.txt'
    full_path = global_params.CFGS_DIR + file_name
    bup_file = global_params.CFGS_DIR + a_device.device_name + '.old'

    # Check if file already exists
    if os.path.isfile(full_path):
        # Create copy of old file
        cmd_status = subprocess.call(['/bin/mv', full_path, bup_file])
        perform_diff = True

    if DEBUG: print "Writing configuration file to file system\n"
    with open(full_path, 'w') as f:
        f.write(output)

    a_device.cfg_file = file_name
    a_device.cfg_archive_time = timezone.make_aware(datetime.now(), timezone.get_current_timezone())

    # obtain last_changed time (Cisco specific)
    a_device.cfg_last_changed = int(snmp_wrapper(a_device, oid=global_params.OID_RUNNING_LAST_CHANGED))
    a_device.save()

    if perform_diff:
        return find_diff(full_path, bup_file)
    else:
        return None
コード例 #3
0
def detect_config_change():
    '''
    Use SNMP to detect configuration changes.

    If configuration has changed, then:
    1. Perform backup
    2. Email diff
    3. Check change into Git
    '''

    net_devices = NetworkDevice.objects.all()

    config_changed = False
    for a_device in net_devices:

        if 'cisco' in a_device.device_class:

            # check if config file exists
            if not a_device.cfg_file:
                print "Initial device backup: {}".format(a_device.device_name)
                backup_config(a_device)
                config_changed = True
                continue

            # Check if the configuration changed
            last_changed = int(
                snmp_wrapper(a_device,
                             oid=global_params.OID_RUNNING_LAST_CHANGED))
            if last_changed > a_device.cfg_last_changed:
                print ">>>Running configuration changed: {}".format(
                    a_device.device_name)
                config_diffs = backup_config(a_device)
                config_changed = True
                if config_diffs:
                    print "Sending email notification regarding changes\n"
                    subject = "Network Device Changed: {}".format(
                        a_device.device_name)
                    send_mail(global_params.EMAIL_RECIPIENT, subject,
                              config_diffs, global_params.EMAIL_SENDER)
            else:
                # Update last_changed field to handle reboot case
                a_device.cfg_last_changed = last_changed
                a_device.save()

    if config_changed:
        print "Checking configuration changes into git"
        git_handling()
コード例 #4
0
def backup_config(a_device):
    '''
    Retrieve configuration from network device, save to filesystem.
    '''

    DEBUG = True
    perform_diff = False

    if DEBUG:
        print "Retrieve device configuration via SSH: {}\n".format(
            a_device.device_name)
    ssh_connect = SSHConnection(a_device)
    ssh_connect.enable_mode()
    output = ssh_connect.send_command('show run\n')

    file_name = a_device.device_name + '.txt'
    full_path = global_params.CFGS_DIR + file_name
    bup_file = global_params.CFGS_DIR + a_device.device_name + '.old'

    # Check if file already exists
    if os.path.isfile(full_path):
        # Create copy of old file
        cmd_status = subprocess.call(['/bin/mv', full_path, bup_file])
        perform_diff = True

    if DEBUG:
        print "Writing configuration file to file system\n"
    with open(full_path, 'w') as f:
        f.write(output)

    a_device.cfg_file = file_name
    a_device.cfg_archive_time = timezone.make_aware(
        datetime.now(), timezone.get_current_timezone())

    # obtain last_changed time (Cisco specific)
    a_device.cfg_last_changed = int(
        snmp_wrapper(a_device, oid=global_params.OID_RUNNING_LAST_CHANGED))
    a_device.save()

    if perform_diff:
        return find_diff(full_path, bup_file)
    else:
        return None