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))
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)
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))
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)
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)
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)
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))
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))
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:
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 )
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)
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))
#!/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
#!/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' })
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 })
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