Пример #1
0
 def _testGetNetworkDevice1(self):
     nc = NetConfig(self.conf_file)
     swh = nc.get('swh-b050f1')
     assert type(swh) == CiscoIOSSwitch, 'could not get a device ' + str(
         type(swh))
     assert swh.connect() == True, 'could not connect to device'
     res = swh.disconnect()
     assert res == None, 'could not disconnect to device ' + str(res)
Пример #2
0
def pipe(hostname, netconfig_conf, mode, commands, options):

    data = []
    device = None
    try:
        netconfig = NetConfig(netconfig_conf)
        device = netconfig.get(hostname, options=options)
        netconfig.connect(device, **options)
        # disable buffering
        device.prompt.terminal_buffer(0)

        if mode == 'enable':
            device.prompt.mode_enable()
        elif mode == 'config':
            device.prompt.mode_config()

        for cmd, d in execute_commands(device, *commands):
            data.append({
                'command': cmd,
                'output': d['output'],
                'error': d['error']
            })

    except Exception, e:
        logging.debug("Err: %s: %s\n%s" % (type(e), e, traceback.format_exc()))
        error = str(e)
Пример #3
0
 def run(self, *args, **kwargs):
     init_loggers(**kwargs)
     self.netconfig = NetConfig(kwargs['config'])
     options = {}
     rename_ap_on_wlc(kwargs['controller'], kwargs['ap_name'],
                      kwargs['ap_mac_address'], self.netconfig, options,
                      kwargs['quiet'], kwargs['apgroup'], kwargs['dry_run'])
Пример #4
0
    def run(self, *args, **kwargs):
        init_loggers(**kwargs)
        nc = NetConfig(kwargs['config'])

        wlc = nc.get(kwargs['device'])
        wlc.connect()

        # 1) get list of all registered ap's
        aps = []
        for i in wlc.tell('show ap summary'):
            m = search(
                '(?P<name>\S+)\s+\d+\s+(?P<model>\S+)\s+(?P<mac>\S+)\s+', i)
            if m:
                d = m.groupdict()
                aps.append(d)
        logging.debug("%s" % (aps, ))

        # 2) for each, determine for each 'slot' it's bssid
        for ap in aps:
            for slot in kwargs['slots']:
                for i in wlc.tell('show ap wlan %s %s' % (slot, ap['name'])):
                    m = search(
                        r'(?P<wlan_id>\d+)\s+(?P<interface>\S+)\s+(?P<bssid>\S+)\s*$',
                        i)
                    if m:
                        d = m.groupdict()
                        ap[slot] = d['bssid']
        logging.debug("%s" % (aps, ))

        # 3) report!
        print "# ap\t%s" % '\t'.join(s for s in kwargs['slots'])
        for ap in aps:
            bssids = "%s" % '\t'.join([ap[s] for s in kwargs['slots']])
            print "%s\t%s" % (ap['name'], bssids)
Пример #5
0
    def run(self, *args, **kwargs):
        init_loggers(**kwargs)
        self.netconfig = NetConfig(kwargs['config'])

        # get lock
        manager = Manager()
        mutex = manager.Lock()

        fw = FirmwareHelper(kwargs['settings'].server,
                            kwargs['settings'].firmware)

        options = {}
        target_args = [
            kwargs['config'], mutex, fw, options, kwargs['quiet'],
            kwargs['apgroup'], kwargs['dry_run']
        ]
        res = self.map(migrate,
                       kwargs['device'],
                       num_workers=kwargs['workers'],
                       target_args=target_args)
        for hostname, status in self.fold(kwargs['device'], res):
            msg = ''
            # if status['error']:
            #     msg = status['error']
            # else:
            msg = status
            print "%s\t%s" % (hostname, msg)
Пример #6
0
def eth_mac(hostname, netconfig_conf, options, quiet):
    """
    Get mac address of the eth interface
    """
    try:
        netconfig = NetConfig(netconfig_conf)
        ap = netconfig.get(hostname, options=options)
        ap.connect()
        ap_mac_address = None
        for interface in ('Gi', 'Fa'):
            for i in ap.tell('show interface | beg %s' % interface):
                m = search(r'\(bia (?P<bia>\S+)\)', i)
                if m:
                    ap_mac_address = slac_utils.net.mac_address(m.group('bia'))
                    break
        ap.disconnect()

        if ap_mac_address == None:
            raise Exception, 'could not determine ap mac address'


#        logging.info('found %s mac address as: %s' % (hostname,ap_mac_address,))

        return ap_mac_address
    except Exception, e:
        logging.error("%s %s" % (type(e), e))
        error = str(e)
Пример #7
0
    def run( self, *args, **kwargs ):
        init_loggers( **kwargs )
        
        options = {}
        for i in ( 'profile', ):
            if i in kwargs and kwargs[i]:
                options[i] = kwargs[i]
        netconfig = NetConfig( kwargs['config'] )        

        # logging.error("EL %s " % (kwargs['device'],))
        for d in kwargs['device']:

            try:

                # get device
                device = netconfig.get( d, options=kwargs )
                netconfig.connect( device, **kwargs )     
                
                # do it
                delete_vlan_if_exists( device, int(kwargs['delete_vlan']), int(kwargs['other_vlan']) )
                
                
            except Exception,e:
                t = traceback.format_exc()
                logging.error("%s: %s\n%s" % (type(e),e,t))
Пример #8
0
def api(hostname, netconfig_conf, mutex, kwargs, options, quiet):
    device = None
    error = None
    res = []
    if not quiet:
        logging.info("%s" % hostname)
    try:
        netconfig = NetConfig(netconfig_conf, mutex=mutex)
        device = netconfig.get(hostname, options=options)
        netconfig.connect(device, **options)

        component = device.get_component(kwargs['component'])
        # logging.info("C: %s" % component)
        # component lists allow __iter__
        # if kwargs['arg']:
        #     logging.info( "%s" %  component( kwargs['arg'] ) )
        # else:
        done = component()
        if isinstance(done, bool):
            done = [True]
        for d in done:
            # logging.info("%s" % (d,))
            # for i in d:
            res.append(d)
    except Exception, e:
        logging.error("Err: %s: %s %s" % (hostname, type(e), e))
        error = str(e)
Пример #9
0
    def run( self, *args, **kwargs ):
        """
        plan:
          1) log onto device to determine current settings
          2) write new config partial to tftp
          3) upload new management config to switch
          4) try to log onto new device
        """
        init_loggers( **kwargs )
        
        options = {}
        for i in ( 'profile', ):
            if i in kwargs and kwargs[i]:
                options[i] = kwargs[i]
        netconfig = NetConfig( kwargs['config'] )        

        # logging.error("EL %s " % (kwargs['device'],))
        for d in kwargs['device']:

            try:
                
                # get device
                device = netconfig.get( d, options=kwargs )
                netconfig.connect( device, **kwargs )     
                # create
                create_vlan( device, **kwargs )
                # add vlans
                add_vlan_to_uplinks( device, **kwargs )

                
            except Exception,e:
                t = traceback.format_exc()
                logging.error("%s: %s\n%s" % (type(e),e,t))
Пример #10
0
    def run( self, *args, **kwargs ):
        """
        plan:
          1) log onto device to determine current settings
          2) write new config partial to tftp
          3) upload new management config to switch
          4) try to log onto new device
        """
        init_loggers( **kwargs )
        
        options = {}
        for i in ( 'profile', ):
            if i in kwargs and kwargs[i]:
                options[i] = kwargs[i]
        netconfig = NetConfig( kwargs['config'] )        

        # logging.error("EL %s " % (kwargs['device'],))
        for d in kwargs['device']:

            try:
                
                # get device
                device = netconfig.get( d, options=kwargs )
                netconfig.connect( device, **kwargs )        

                # create vlan
                if not create_vlan( device, **kwargs ):
                    raise Exception, 'could not create vlan'
                    
                # add to uplinks and add svi
                if not add_vlan_to_uplinks( device, **kwargs ):
                    raise Exception, 'did not add vlan to any new uplinks'
                    
                # prepare the svi
                if not setup_management_ip( device, **kwargs ):
                    raise Exception, 'could not create new svi'
                        
                # switch the old and new svi's
                

                # # change default gateway
                # try:
                #     device.prompt.ask( 'ip default-gateway %s' % (kwargs['new_gateway'],), cursor=device.prompt.cursor('mode','config'), timeout=2 )
                # except Exception, e:
                #     logging.debug("required timeout... %s %s" % (type(e),e))
                
                # we're now kicked out because of change in default gateway, so try to reconnect to new ip      
                # new_device = netconfig.get( kwargs['new_ip'], options=kwargs, profiles=[ 'ios' ], use_cache=True )
                # netconfig.connect( new_device )

                # if 'old_vlan' in kwargs:
                #     new_device.prompt.ask('no interface vlan %s' % (kwargs['old_vlan'],))
                # new_device.system.config.commit()
            
            
            except Exception,e:
                logging.error("%s: %s" % (type(e),e))
Пример #11
0
    def setup(self, *args, **kwargs):

        init_loggers(**kwargs)

        self.fw = FirmwareHelper(kwargs['settings'].server,
                                 kwargs['settings'].firmware)
        self.group = kwargs['group']
        self.release = kwargs['release']

        options = {}
        for i in ('profile', ):
            if i in kwargs and kwargs[i]:
                options[i] = kwargs[i]
        self.netconfig = NetConfig(kwargs['config'])
        self.options = options
Пример #12
0
    def run(self, *args, **kwargs):

        init_loggers(**kwargs)

        options = {}
        for i in ('profile', 'port', 'prime', 'password', 'username',
                  'cache_group'):
            if i in kwargs and kwargs[i]:
                options[i] = kwargs[i]

        netconfig = NetConfig(kwargs['config'])
        device = None
        try:

            device = netconfig.get(kwargs['device'], options=options)
            netconfig.connect(device, **options)

            # init
            if kwargs['no_user_check']:
                try:
                    users = {}
                    t = device.system.users.get()
                    if not t == None:
                        for i in t:
                            if not 'user' in i:
                                i['user'] = '******'
                            if not i['user'] in users:
                                users[i['user']] = []
                            users[i['user']].append(i['line'])
                        for k, v in users.iteritems():
                            print('WARNING: %s is connected on %s' %
                                  (k, ', '.join(v)))
                except:
                    pass

            if device.ports.conf_sync:
                print("NOTICE: device is using conf sync profile %s" %
                      device.ports.conf_sync)

            # set up enable acess
            if kwargs['enable']:
                device.prompt.mode_enable()

            device.prompt.mode_interactive()
        except Exception, e:
            logging.error("%s: %s" % (type(e), e))
Пример #13
0
    def run(self, *args, **kwargs):
        init_loggers(**kwargs)
        options = {}
        for i in ('profile', 'password'):
            if i in kwargs and kwargs[i]:
                options[i] = kwargs[i]
        self.netconfig = NetConfig(kwargs['config'])
        self.options = options

        results = {}

        target_args = [kwargs['config'], options, kwargs['quiet']]
        res = self.map(eth_mac,
                       kwargs['device'],
                       num_workers=kwargs['workers'],
                       target_args=target_args)
        for hostname, mac in self.fold(kwargs['device'], res):
            print "%s\t%s" % (hostname, mac)
Пример #14
0
    def run(self, *args, **kwargs):

        init_loggers(**kwargs)

        # organise by device
        by_device = {}
        for i in parse_file(**kwargs):
            logging.debug(" >= %s" % i)
            if not i['device'] in by_device:
                by_device[i['device']] = []
            by_device[i['device']].append(i)

        options = {}
        for i in ('profile', ):
            if i in kwargs and kwargs[i]:
                options[i] = kwargs[i]
        netconfig = NetConfig(kwargs['config'])

        for d in by_device:

            dev = None
            try:

                dev = netconfig.get(d)
                netconfig.connect(dev)

                # disable buffering
                dev.prompt.terminal_buffer(0)

                for d, port, action, data in set_ports(dev, by_device[d]):
                    if action == True:
                        logging.info(" updated")
                    elif action == None:
                        logging.info(" no changes required")
                    else:
                        raise Exception("%s %s failed: %s" %
                                        (d, port['port'], data))

            except Exception, e:
                t = traceback.format_exc()
                logging.error("%s: %s\n%s" % (type(e), e, t))
            finally:
Пример #15
0
def backup( hostname, netconfig_conf, mutex, storage_path, options, quiet, write_config, commit ):
    # logging.error("hostname: %s, netconfig=%s, storage=%s" % (hostname,netconfig_conf,storage_path))
    res, person, diff, error = None, None, {}, None
    device = None
    config = None
    if not quiet:
        logging.info("%s"%hostname)
    try:
        netconfig = NetConfig( netconfig_conf, mutex=mutex )
        storage = FileStore( path=storage_path )
        device = netconfig.get( hostname, options=options )
        netconfig.connect( device, **options )
        if commit:
            try:
                device.system.config.commit()
            except Exception,e:
                logging.info("%s: %s" % (hostname,e) )
                device.prompt.ask("", fail_okay=True)
        config = device.system.config.get()
        res, person, diff = storage.insert( hostname, config, commit=write_config )
Пример #16
0
def bssid(hostname, netconfig_conf, options, quiet):
    """
    just logs onto the device and grabs out bssids
    """
    found = {}
    error = None
    try:
        netconfig = NetConfig(netconfig_conf)
        device = netconfig.get(hostname, options=options)
        device.connect()
        cmd = 'show int | inc Radio,'
        for i in device.prompt.tell(cmd):
            m = search(
                r'Hardware is (?P<hardware>.*), address is (?P<addr>.*) \(bia (?P<bia>.*)\)$',
                i)
            if m:
                d = m.groupdict()
                found[d['bia']] = d
    except Exception, e:
        logging.error("%s %s" % (type(e), e))
        error = str(e)
Пример #17
0
    def run(self, *args, **kwargs):

        init_loggers(**kwargs)

        options = {}
        for i in ('profile', ):
            if i in kwargs and kwargs[i]:
                options[i] = kwargs[i]
        netconfig = NetConfig(kwargs['config'])

        for d in get_array(kwargs['device']):

            try:
                # get device
                device = netconfig.get(d, options=options)
                netconfig.connect(device, **options)

                # reload device and make sure it comes back!
                msg_per_loop = '.'
                for e in reload_and_monitor(netconfig,
                                            device,
                                            options=options,
                                            wait=int(kwargs['wait']),
                                            delay=int(kwargs['delay']),
                                            message_per_loop=msg_per_loop):
                    if isinstance(e, str):
                        if e == msg_per_loop:
                            print "%s" % (e, ),
                        else:
                            print "%s" % (e, )
                    else:
                        print
                        device = e

                logging.info("%s" % (device.system.get(), ))

            except Exception, e:
                logging.error("%s: %s" % (type(e), e))
Пример #18
0
    def run(self, *args, **kwargs):
        init_loggers(**kwargs)
        options = {}
        for i in ('profile', 'password'):
            if i in kwargs and kwargs[i]:
                options[i] = kwargs[i]
        self.netconfig = NetConfig(kwargs['config'])
        self.options = options

        results = {}

        target_args = [kwargs['config'], options, kwargs['quiet']]
        res = self.map(bssid,
                       kwargs['device'],
                       num_workers=kwargs['workers'],
                       target_args=target_args)
        for hostname, status in self.fold(kwargs['device'], res):
            msg = ''
            if status['error']:
                msg = status['error']
            else:
                msg = '\t'.join(v['bia']
                                for k, v in status['interfaces'].iteritems())
            print "%s\t%s" % (hostname, msg)
Пример #19
0
 def testForceProbe(self):
     nc = NetConfig(self.conf_file)
     swh = nc.get('swh-b050f1', options={'profile': 'auto'})
     assert type(swh) == CiscoIOSSwitch, 'could not get a device ' + str(
         type(swh))
Пример #20
0
def migrate(hostname,
            netconfig_conf,
            mutex,
            firmware_helper,
            options,
            quiet,
            apgroup='default-group',
            dry_run=False):
    """
    just logs onto the device and grabs out bssids
    """
    success = False
    error = None
    ap = None
    switch = None
    wlc = None
    try:
        netconfig = NetConfig(netconfig_conf, mutex=mutex)

        # ap connections
        ap = netconfig.get(hostname, options=options)
        ap.connect()

        # 1) log onto ap and determine mac address
        ap_mac_address = None
        for interface in ('Gi', 'Fa'):
            for i in ap.tell('show interface | beg %s' % interface):
                m = search(r'\(bia (?P<bia>\S+)\)', i)
                if m:
                    ap_mac_address = slac_utils.net.mac_address(m.group('bia'))
                    break
        if ap_mac_address == None:
            raise Exception, 'could not determine ap mac address'
        logging.info('found %s mac address as: %s' % (
            hostname,
            ap_mac_address,
        ))

        # 2) see where it's connected to via cdp
        peer = {}
        for ap_port, d in ap.layer1:
            # assume only one connection
            peer = {
                'name': d['peer_device'].split('.')[0],
                'port': d['peer_physical_port']
            }
        if peer == {}:
            raise Exception, 'could not determine where access point is connected to'

        # 3) upload new firmware to ap
        try:
            # don't do image only as it will deactivate wireless interfaces
            upgrade_firmware(netconfig,
                             ap,
                             'wlc',
                             None,
                             firmware_helper,
                             options={},
                             image_only=False,
                             dry_run=dry_run)
        except Exception, e:
            raise Exception, 'could not upgrade firmware: %s' % e

        # 4) clear the configs on the ap
        if not dry_run:
            try:
                logging.info('clearing configs')
                # do an write erase to clear config and delete the bvi cache to enable dhcp booting (faster)
                for c in (
                        'erase /all nvram:',
                        'del env_vars',
                        'del private-config',
                        'del config.txt',
                        'wr erase',
                ):
                    ap.prompt.ask(c,
                                  interact={
                                      'confirm': '\n',
                                      'question': '\n'
                                  })
            except Exception, e:
                logging.error("could not clear configuration: %s" % (e, ))
                raise e

            try:
                #sleep(20)
                ap.system.reload(force=True, commit=False)
            except Exception, e:
                logging.error("could not reload: %s" % (e, ))
                raise e
Пример #21
0
def port_baseline(hostname,
                  netconfig_conf,
                  mutex,
                  interfaces,
                  baselines,
                  exceptions,
                  options,
                  fix,
                  fix_only=None):
    """
    if len(fix_only) == 0, assume fix all
    """

    device = None
    deltas = []

    if fix_only == None:
        fix_only = []

    try:

        logging.info('processing %s' % (hostname, ))
        netconfig = NetConfig(netconfig_conf, mutex=mutex)
        device = netconfig.get(hostname, options=options)
        netconfig.connect(device, **options)
        device.prompt.terminal_buffer()

        # find all the ports
        ports = []
        if interfaces == None:
            ports = device.ports.filter()
        else:
            for i in interfaces:
                ports.append(device.ports.get(i))

        # do something with each port
        for p in ports:

            profile = None
            delta = {}
            multiple = False
            try:
                logging.info('analysing %s %s' % (hostname, p['port']))
                profile, delta, multiple = compare_to_baselines(
                    device, p, baselines, exceptions)
            except Exception, e:
                logging.warn("%s\t%s: %s" % (hostname, p['port'], e))

            # logging.info("%-16s\t%-9s\t%26s%s\tdelta: %s" % (hostname, p['port'], profile, "*" if multiple else '', report_baseline_delta(delta),))

            fixed = False if len(delta.keys()) else None
            fix_this = True if len(
                fix_only) == 0 or profile in fix_only else False
            logging.debug(" fix: %s / %s" % (fix_this, fix_only))
            if profile and len(delta.keys()) and fix and fix_this:
                try:
                    logging.info('fixing %s %s with %s' %
                                 (hostname, p['port'], profile))
                    baseline = {}
                    for k, d in delta.iteritems():
                        if not 'baseline' in d:
                            raise Exception, 'profile %s does not contain a baseline stanza' % (
                                profile, )
                        baseline[k] = d['baseline']
                    # always add the type to force cleaning of the configs
                    baseline['type'] = p['type']
                    baseline['port'] = p['port']
                    fixed, _tmp = device.ports.set(port=p,
                                                   port_object=baseline,
                                                   initial_check=False)
                except Exception, e:
                    logging.error(
                        "Err: %s\n%s" %
                        ('could not fix %s' % p, traceback.format_exc()))

            report = True
            if len(fix_only) and not profile in fix_only:
                report = False

            if report:
                deltas.append({
                    'physical_port': p['port'],
                    'profile': profile,
                    'delta': delta,
                    'fixed': fixed
                })
Пример #22
0
        '--enable'  : 'enter_enable',
        '--profile' : 'profile=',
        '-f'        : 'profile=',
        '-v'        : 'verbose',
        '--verbose' : 'verbose'
    }
    
    args, options = util.parseArgs( args )
    host = args.pop(0)

    if options.has_key( 'verbose' ) and options['verbose'] == 1:
        logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(module)-25s %(funcName)-20s %(lineno)-5d %(message)s', datefmt='%a, %d %b %Y %H:%M:%S')
    else:
        logging.basicConfig(level=logging.INFO)

        

    if options.has_key( 'help' ) and options['help'] == 1:
        usage()
        sys.exit()
        
    # create object
    netconfig = NetConfig( config_file )
    device = netconfig.get( host, options=options )
    
    netconfig.connect( device, options )
    
    for d in device.ports.filter():
        print( "%s" % d )
        
    device.disconnect()
Пример #23
0
#!/bin/env python
import logging
from netconfig import NewNetConfig as NetConfig

logging.basicConfig(level=logging.DEBUG)

nc = NetConfig('/u/sf/ytl/net/projects/net-config/conf/net-config.yaml')
ap = nc.get('AP-280R112')
ap.connect()

print ap.prompt.request('del config.txt ',
                        interact={
                            'confirm': '\n',
                            'question': '\n'
                        })
Пример #24
0
#!/usr/bin/python

###
# quick test of netconf via ssh
###

from netconfig import NewNetConfig as NetConfig

if __name__ == '__main__':

    nc = NetConfig(
        '/afs/slac/g/scs/net/projects/net-config/conf/net-config.yaml')
    device = nc.get('rtr-farm04')

    if device.connect():

        xml = device.rpc('show environment fan')
        for i in xml.xpath(
                "//*[local-name()='ROW_faninfo']/*[local-name()='fanname']/text()"
        ):
            # for i in xml.xpath("//*:fanname/text()"): # not xpath2 compat!
            print "%s" % i
Пример #25
0
 def testInit(self):
     nc = NetConfig(self.conf_file)
     assert type(nc) == NetConfig, 'initiation failed'