Esempio n. 1
0
    def run(self):
        self.logfile = open(os.getcwd() + '/' + 'linkfailure_log', 'a')
        while True:
            while self.failed() == False:
                time.sleep(1)
            # select a router
            r_number = random.randint(1,len(self.rtr)) - 1
            # select a random interface 
            i_number = random.randint(1, len(self.rtr[r_number]['interfaces'])) - 1

            #set interfaces ge-1/0/3 disable 

            cmd = 'set interfaces ' + self.rtr[r_number]['interfaces'][i_number]['name'] + ' disable'

            dev = Device(host=self.rtr[r_number]['ip'], user='******', password='******')
            dev.open()
            dev.timeout = 60
            cu = Config(dev)
            cu.lock()
            cu.load(cmd, format='set', merge=True)
            cu.commit()
            cu.unlock()
            dev.close()
            link__data = { 'status': 'failed', 'timestamp': datetime.datetime.fromtimestamp(time.time()).strftime('%a:%H:%M:%S'), 
                                'router_id': self.rtr[r_number]['router_id'], 'router_name': self.rtr[r_number]['name'],
                                'interface_address': self.rtr[r_number]['interfaces'][i_number]['address'],
                                'interface_name': self.rtr[r_number]['interfaces'][i_number]['name']
                         }
            jd = json.dumps(link__data)
            self.redis.publish('link_event', jd)
            self.logfile.write("Link failed: " + datetime.datetime.fromtimestamp(time.time()).strftime('%a:%H:%M:%S') + " " +
                               self.rtr[r_number]['name'] + " " +  self.rtr[r_number]['ip'] + " " + self.rtr[r_number]['interfaces'][i_number]['name'] + "\n")
            self.logfile.flush()
            # now repair the link
            while self.repaired() == False:
                time.sleep(1)

            cmd = 'delete interfaces ' + self.rtr[r_number]['interfaces'][i_number]['name'] + ' disable'
            dev = Device(host=self.rtr[r_number]['ip'], user='******', password='******')
            dev.open()
            dev.timeout = 60
            cu = Config(dev)
            cu.lock()
            cu.load(cmd, format='set', merge=True)
            cu.commit()
            cu.unlock()
            dev.close()
            link__data = { 'status': 'healed', 'timestamp': datetime.datetime.fromtimestamp(time.time()).strftime('%a:%H:%M:%S'), 
                                'router_id': self.rtr[r_number]['router_id'], 'router_name': self.rtr[r_number]['name'],
                                'interface_address': self.rtr[r_number]['interfaces'][i_number]['address'],
                                'interface_name': self.rtr[r_number]['interfaces'][i_number]['name']
                         }
            jd = json.dumps(link__data)
            self.redis.publish('link_event', jd)
            self.logfile.write("Link healed: " + datetime.datetime.fromtimestamp(time.time()).strftime('%a:%H:%M:%S') + " " +
                               self.rtr[r_number]['name'] + " " +  self.rtr[r_number]['ip'] + " " + self.rtr[r_number]['interfaces'][i_number]['name'] + "\n")
            self.logfile.flush()
Esempio n. 2
0
def device(connect_string):
    """Return a connected device.

    This should be run as a context. Devices will be kept open and
    reused. If a device is already in use, entering the context will
    block until the device is ready.

    """
    with device_lock:
        device, lock = device_list.get(connect_string, (None, None))
        if device is None:
            # Parse the connect string
            mo = connect_re.match(connect_string)
            if not mo:
                raise ValueError("unparsable host string")
            args = {k: v for k, v in mo.groupdict().items()
                    if v is not None}
            device = Device(**args)
            lock = threading.Lock()
            device_list[connect_string] = device, lock
    with lock:
        if not device.connected:
            device.open(gather_facts=False, attempts=3)
            device.timeout = 60
        yield device
Esempio n. 3
0
def main():
	#iterate over csv
	for row in devList:

        	#Make Device Connection
        	dev = Device(host=row[1], user=userName, password=userPassword)
		try:
       			#Print Opening Header for Status
			now1 = datetime.datetime.now()
			pprint("Work starts on " + row[0] + " | " + row[1] + " at " + now1.strftime("%Y-%m-%d %H:%M"))

			#Open Device with Custom Timer
			dev.open()
			dev.timeout = 300

        	except Exception as err:                               
                	sys.stderr.write('Cannot connect to device: {0}\n'.format(err))

		#Do RPC/Work
		try:
			#Start Work here




			#Write Element Seperator	
			print("+++++++++++++++++++++++++++++++++++++++++++++++++")
		
			#Close Device
			dev.close()

        	except Exception as err:
                	sys.stderr.write('Cannot perform RPC on device: ' + row[1] + '\n'.format(err))
                	print("+++++++++++++++++++++++++++++++++++++++++++++++++")
Esempio n. 4
0
def deployConfig(my_device_list_dict, my_username, my_password, my_config_template_file):
	my_hostname=""
	try:
		my_hostname=my_device_list_dict["mgmt_ip"]
		printProgress("INFO",my_hostname,"Connecting to device through netconf.")
		dev=Device(my_hostname,user=my_username,password=my_password)
		dev.open()
		dev.timeout=3*60
		cu = Config(dev)
		printProgress("INFO",my_hostname,"Going to load template the config now.")
		
		# Determine if template file is in "set" or "bracketed" format
		if isSet(my_config_template_file):
			rsp=cu.load(template_path=my_config_template_file,format='set',template_vars=my_device_list_dict)
		else:
			rsp=cu.load(template_path=my_config_template_file,template_vars=my_device_list_dict)
		
		printProgress("INFO",my_hostname,"Performing diff between active and candidate config.")
		cu.pdiff()
		printProgress("INFO",my_hostname,"Performing commit check")
		if cu.commit_check():
			printProgress("INFO",my_hostname,"commit check was successfull.")
			printProgress("INFO",my_hostname,"performing commit now.")
			commit_status=cu.commit()
			printProgress("INFO",my_hostname,"disconnecting from device.")
			dev.close()
			return commit_status
		else:
			return False
	except Exception,err:
		printProgress("ERROR",my_hostname,"Encountered exception while deploying config")
		printProgress("ERROR",my_hostname,str(err))
		return False
Esempio n. 5
0
def connect(module):
    host = get_param(module, 'host')

    kwargs = {
        'port': get_param(module, 'port') or 830,
        'user': get_param(module, 'username')
    }

    if get_param(module, 'password'):
        kwargs['passwd'] = get_param(module, 'password')

    if get_param(module, 'ssh_keyfile'):
        kwargs['ssh_private_key_file'] = get_param(module, 'ssh_keyfile')

    kwargs['gather_facts'] = False

    try:
        device = Device(host, **kwargs)
        device.open()
        device.timeout = get_param(module, 'timeout') or 10
    except ConnectError:
        exc = get_exception()
        module.fail_json('unable to connect to %s: %s' % (host, str(exc)))

    return device
Esempio n. 6
0
def main():

    parser = argparse.ArgumentParser(description='Displays configuration elements with select hashtag. Hashtags are configured at any level using "set apply-macro ht <hashtag-name>" command.')
    
    for key in arguments:
        parser.add_argument(('-' + key), required=True, help=arguments[key])
    args = parser.parse_args()

    dev = Device()
    dev.open()
    dev.timeout = 300
    xml_config = dev.rpc.get_config(options={'database':'committed','inherit':'inherit'})    
    paths = []
    #select all elements matching pattern
    for ht_element in xml_config.findall( './/apply-macro/data/[name=\'{}\']/../'.format( args.hashtag)):
        #filter by node id just in case        
        if ht_element.text == 'ht':
            s = build_xml_filter( xml_config, ht_element.getparent().getparent())
            paths.append( s)
    if paths:
        path = '<configuration>'+"".join( paths)+'</configuration>'
        #17.x versions of PyEz expect filter_xml to be an etree instance
        xml_config = dev.rpc.get_config( filter_xml= etree.XML( path),options={'database':'committed','inherit':'inherit', 'format':'text'})
        print xml_config.text
    else:
        print 'hashtag \'{}\' is not found'.format( args.hashtag)
    dev.close()
 def get_connected_device(self, requester_name, device):
     LOGGER.debug('get_connected_device, request by [%s]', requester_name)
     time1 = time.time()
     dev_name = device.get('ip_address')
     password_encoded = self.config.get('password_encoded', False)
     if password_encoded:
         pwd = utils.decrypt_passwd(device.get('password'))
     else:
         pwd = device.get('password')
     try:
         dev = Device(host=dev_name, user=device.get('user'),
                  password=pwd, port=device.get('port', 22), gather_facts=False, auto_probe=5)
         rpc_timeout = self.config.get('rpc_timeout', 1) * 60
         dev.open()
         if dev.connected:
             dev.timeout = rpc_timeout
             return dev
     except Exception as e:
         LOGGER.error(
                 'connect, for device [%s, %s] failed',
                 dev_name,
                 e)
     finally:
         LOGGER.debug(
                 'connect, for device %s %d(s)',
                 dev_name,
                 (time.time() - time1))
     return None
def main(device_ip, peer_ip):
    config = ConfigDictionary()

    username = config.username()
    password = config.password()

    print (device_ip + " logging in as " + username)

    jdev = Device(user=username, host=device_ip, password=password)
    jdev.open(gather_facts=False)
    jdev.timeout=6000

    try:
        resultxml = jdev.rpc.get_route_information(table='inet.0',protocol='bgp',peer=peer_ip,extensive=True)

    except Exception as err:
        print "CMD:"   
        etree.dump(err.cmd)   
        print "RSP:"   
        etree.dump(err.rsp)

    for routexml in resultxml.findall('.//rt'):
        route = RouteData(routexml)
        print "destination: " + route.prefix() + "as-path: " + route.aspath()

    jdev.close()
Esempio n. 9
0
def main(args):
    '''Acquire necessary input options, interact with SRX device as specified per CLI args.'''
    parser = argparse.ArgumentParser(
        description='Interact with specified SRX device as specified')
    parser.add_argument('--version', action='version', version=__version__)
    parser.add_argument('-d', '--datafile', help='specify YAML file to read router info from',
                        default=SRX_FILE)
    parser.add_argument('--prompt', action='store_true',
                        help='prompt for router info (do not try to read in from file)',
                        default=False)
    parser.add_argument('-v', '--verbose', action='store_true', help='display verbose output',
                        default=False)
    args = parser.parse_args()
    # Debugging
    #if args.verbose:
    #    print 'Args = {}'.format(args)

    # Initialize data structures
    mysrx = {}
    if not args.prompt:
        mysrx = yaml_input(args.datafile, args.verbose)
        # Debugging
        #if args.verbose:
        #    print 'mysrx = {}'.format(mysrx)
    else:
        if args.verbose:
            print 'Prompting specified - asking user for all connection details'
    check_input(mysrx, args.verbose)

    mysrx_conn = Device(host=mysrx['HOST'], user=mysrx['USER'], password=mysrx['PASSWORD'])
    if args.verbose:
        print 'Opening NETCONF connection to {}...'.format(mysrx['HOST'])
    mysrx_conn.open()
    # Set timeout - default of 30 seconds can be problematic, must set after open()
    mysrx_conn.timeout = TIMEOUT

    mysrx_cfg = Config(mysrx_conn)
    # Change 1
    if args.verbose:
        print '\nChange #1'
    srx_conf(mysrx, mysrx_cfg, SRX_CONF_CHG1, 'set', True, False, verbose=args.verbose)

    # Change 2
    if args.verbose:
        print '\nChange #2'
    srx_conf(mysrx, mysrx_cfg, SRX_CONF_FILE, 'text', True, True,
             'Temporarily changing hostname.', args.verbose)

    # Change 3
    if args.verbose:
        print '\nChange #3'
    srx_conf(mysrx, mysrx_cfg, SRX_XML_FILE, 'xml', True, True,
             'Restoring original hostname.', args.verbose)

    # Cleanup
    mysrx_conn.close()
Esempio n. 10
0
    def _connect(self):
        """Connect to a device.

        :returns: a connection to a Juniper Networks device.
        :rtype: ``Device``
        """
        dev = Device(self.hostname, user=self.user, password=self.password)
        dev.open()
        dev.timeout = self.timeout
        return dev
def juniper_connection_setup():
    django.setup()

    device = NetworkDevice.objects.get(device_name='juniper-srx')
    print "Connecting to device: %s" % device.device_name
    jnp_dev = Device(host=device.ip_address, user=device.credentials.username, password=device.credentials.password)
    jnp_dev.open()
    jnp_dev.timeout = 120
    if jnp_dev.facts:
        return jnp_dev
    else:
        return None
Esempio n. 12
0
def main(args):
    '''Acquire necessary input options, interact with SRX device as specified per CLI args.'''
    parser = argparse.ArgumentParser(
        description='Interact with specified SRX device as specified')
    parser.add_argument('--version', action='version', version=__version__)
    parser.add_argument('-d', '--datafile', help='specify YAML file to read router info from',
                        default=SRX_FILE)
    parser.add_argument('--prompt', action='store_true',
                        help='prompt for router info (do not try to read in from file)',
                        default=False)
    parser.add_argument('-v', '--verbose', action='store_true', help='display verbose output',
                        default=False)
    args = parser.parse_args()
    # Debugging
    #if args.verbose:
    #    print 'Args = {}'.format(args)

    # Initialize data structures
    mysrx = {}
    if not args.prompt:
        mysrx = yaml_input(args.datafile, args.verbose)
        # Debugging
        #if args.verbose:
        #    print 'mysrx = {}'.format(mysrx)
    else:
        if args.verbose:
            print 'Prompting specified - asking user for all connection details'
    check_input(mysrx, args.verbose)

    mysrx_conn = Device(host=mysrx['HOST'], user=mysrx['USER'], password=mysrx['PASSWORD'])
    if args.verbose:
        print 'Opening NETCONF connection to {}...'.format(mysrx['HOST'])
    mysrx_conn.open()
    # Set timeout - default of 30 seconds can be problematic, must set after open()
    mysrx_conn.timeout = TIMEOUT

    ethports = EthPortTable(mysrx_conn)
    if args.verbose:
        print 'Gathering EthPortTable info from {}...'.format(mysrx['HOST'])
    ethports.get()

    print 'Device Ethernet Port information for {}:'.format(mysrx['HOST'])
    for int_key in ethports.keys():
        print '{}:'.format(int_key)
        for int_subkey in ethports[int_key].keys():
            if int_subkey == 'oper':
                print '  \\Operational Status:   {}'.format(ethports[int_key][int_subkey])
            elif int_subkey == 'rx_packets':
                print '  \\Received Packets:     {}'.format(ethports[int_key][int_subkey])
            elif int_subkey == 'tx_packets':
                print '  \\Transmitted Packets:  {}'.format(ethports[int_key][int_subkey])
    mysrx_conn.close()
Esempio n. 13
0
def main():

	#iterate over csv
	for row in devList:

        	#Make Device Connection
        	dev = Device(host=row[1], user=userName, password=userPassword)
		try:
       			#Print Opening Header for Status
			now1 = datetime.datetime.now()
			pprint("Work starts on " + row[0] + " | " + row[1] + " at " + now1.strftime("%Y-%m-%d %H:%M"))

			#Open Device with Custom Timer
			dev.open()
			dev.timeout = 900

        	except Exception as err:                               
                	sys.stderr.write('Cannot connect to device: {0}\n'.format(err))

		#Do RPC/Work
		try:
			#Start Work here

			#Create an instance of SW
			sw = SW(dev)

			try:
				ok = sw.install(package=package, remote_path=remote_path, progress=myprogress, validate=validate, no_copy=noCopy, timeout=1800)
		
			except Exception as err:
				msg = 'Unable to install software, {0}'.format(err)
				print(msg)
				ok = False
		
			if ok is True:
				print('Software installation complete. Rebooting')
				rsp = sw.reboot()
				print('Upgrade pending reboot cycle, please be patient.')
				print(rsp)
			else:
				msg = 'Unable to install software, {0}'.format(ok)
				print(msg)
	
			#Write Element Seperator	
			print("+++++++++++++++++++++++++++++++++++++++++++++++++")
		
			#Close Device
			dev.close()

        	except Exception as err:
                	sys.stderr.write('Cannot perform RPC on device: ' + row[1] + '\n'.format(err))
                	print("+++++++++++++++++++++++++++++++++++++++++++++++++")
Esempio n. 14
0
def main():
	print("\nWelcome to Junos Upgrade Tool \n")
	# Request which code to upgrade with
	fileList = getFileList(code_path)
	package = getOptionAnswer("Choose a junos package", fileList)
	package = code_path + package
	# Request IP for the system to upgrade
	host = getInputAnswer("IP Address of the host")
	# Get username and password parameters
	username=getInputAnswer("\nEnter your device username")
	password=getpass(prompt="\nEnter your device password: ")	
    # initialize logging
	logging.basicConfig(filename=logfile, level=logging.INFO, format='%(asctime)s:%(name)s: %(message)s')
	logging.getLogger().name = host
	sys.stdout.write('Information logged in {0}\n'.format(logfile))

    # verify package exists
	if (os.path.isfile(package)):
		found = True
	else:
		msg = 'Software package does not exist: {0}. '.format(package)
		sys.exit(msg + '\nExiting program')


	dev = Device(host,user=username,password=password)
	try:
		dev.open()
	except Exception as err:
		sys.stderr.write('Cannot connect to device: {0}\n'.format(err))
		return
    
    # Increase the default RPC timeout to accommodate install operations
	dev.timeout = 300

    # Create an instance of SW
	sw = SW(dev)

	try:
		do_log('Starting the software upgrade process: {0}'.format(package))
		ok = sw.install(package=package, remote_path=remote_path, progress=update_progress, validate=validate)
	except Exception as err:
		msg = 'Unable to install software, {0}'.format(err) 
		do_log(msg, level='error')
	else:
		if ok is True:
			do_log('Software installation complete. Rebooting')
			rsp = sw.reboot() 
			do_log('Upgrade pending reboot cycle, please be patient.')
			do_log(rsp)   

    # End the NETCONF session and close the connection  
	dev.close()
Esempio n. 15
0
def main():

    # iterate over csv
    for row in devList:

        # Make Device Connection
        dev = Device(host=row[1], user=userName, password=userPassword)
        try:
            # Print Opening Header for Status
            now1 = datetime.datetime.now()
            pprint("Work starts on " + row[0] + " | " + row[1] + " at " + now1.strftime("%Y-%m-%d %H:%M"))

            # Open Device with Custom Timer
            dev.open()
            dev.timeout = 900

        except Exception as err:
            sys.stderr.write("Cannot connect to device: {0}\n".format(err))

        # Do RPC/Work
        try:
            # Start Work here

            # Create an instance of SW
            sw = SW(dev)

            try:
                ok = sw.safe_copy(
                    package=package, remote_path=remote_path, progress=myprogress, clean_fs=freeSpace, timeout=1800
                )

            except Exception as err:
                msg = "Unable to copy software, {0}".format(err)
                print (msg)
                ok = False

            if ok is True:
                print ("Software copy complete.")
            else:
                msg = "Unable to copy software, {0}".format(ok)
                print (msg)

                # Write Element Seperator
            print ("+++++++++++++++++++++++++++++++++++++++++++++++++")

            # Close Device
            dev.close()

        except Exception as err:
            sys.stderr.write("Cannot perform RPC on device: " + row[1] + "\n".format(err))
            print ("+++++++++++++++++++++++++++++++++++++++++++++++++")
def main(peer_asn):
    list_peering_ips_of_target_asn = []

    config = ConfigDictionary("/home/andy/etc/pypeer.ini")
    username = config.username()
    password = config.password()

    for router in config.get_list_of_router_names():
        jdev = Device(user=username, host=config.get_router_ip(router),
                      password=password)
        jdev.open(gather_facts=False)
        jdev.timeout = 600

        try:
            resultxml = jdev.rpc.get_bgp_summary_information()
        except Exception as err:
            print "CMD:"
            etree.dump(err.cmd)
            print "RSP:"
            etree.dump(err.rsp)

        bgpsum = BgpData(resultxml)
        for thispeeringip in bgpsum.get_list_ipaddr_from_asn(peer_asn):
            list_peering_ips_of_target_asn.append(thispeeringip)

    exchange = Exchange()
    list_sessions_configured_peeringdbid_exchanges_of_target_asn = []
    for peering_ip in list_peering_ips_of_target_asn:
        if not is_valid_ipv4_address(peering_ip):
            next
        else:
            peeringdb_id = exchange.get_exchange_from_peerip(peering_ip)['peeringdbid']
            list_sessions_configured_peeringdbid_exchanges_of_target_asn.append(peeringdb_id)

    # todo: pull this from config file
    list_my_exchanges = [26, 806, 87, 59, 33, 31, 804, 1, 255, 5, 359, 48, 387,
                         435, 583, 745, 18, 777, 53, 297, 35, 70, 64, 60, 325,
                         587]

    peeringdb = PeeringDBClient()
    list_their_exchanges = peeringdb.get_list_connected_ixp(peer_asn)

    mutual_exchanges = set(list_my_exchanges).intersection(list_their_exchanges)
    missing_exchanges = set(mutual_exchanges).difference(list_sessions_configured_peeringdbid_exchanges_of_target_asn)

    print "Missing exchanges are:" + str(missing_exchanges)

    for pdbid in missing_exchanges:
        print str(pdbid) + ": " + peeringdb.get_name_of_ixp_from_pdbid(pdbid)
Esempio n. 17
0
def main():

	#Setup for output
	now = datetime.datetime.now()
	txt = os.path.join(save_path, filePrefix + "-" + now.strftime("%Y%m%d-%H%M") + ".txt")
	text_file =  open(txt, "w")
	#Write Opening Seperator
	text_file.write("+++++++++++++++++++++++++++++++++++++++++++++++++" + '\n')

	#iterate over csv
	for row in devList:

        	#Make Device Connection
        	dev = Device(host=row[1], user=userName, password=userPassword)
		try:
       			#Print Opening Header for Status
			now1 = datetime.datetime.now()
			pprint("Work starts on " + row[0] + " | " + row[1] + " at " + now1.strftime("%Y-%m-%d %H:%M"))

			#Open Device with Custom Timer
			dev.open()
			dev.timeout = 300

        	except Exception as err:                               
                	sys.stderr.write('Cannot connect to device: {0}\n'.format(err))
                	text_file.write('Cannot connect to device: {0}\n'.format(err) + '\n')

		#Do RPC/Work
		try:
			#Start Work here




			#Write Element Seperator	
			print("+++++++++++++++++++++++++++++++++++++++++++++++++")
			text_file.write("+++++++++++++++++++++++++++++++++++++++++++++++++" '\n')
		
			#Close Device
			dev.close()

        	except Exception as err:
                	sys.stderr.write('Cannot perform RPC on device: ' + row[1] + '\n'.format(err))
                	print("+++++++++++++++++++++++++++++++++++++++++++++++++")
                	text_file.write('Cannot perform RPC on device: ' + row[1] + '\n'.format(err) + '\n')
                	text_file.write("+++++++++++++++++++++++++++++++++++++++++++++++++" + '\n')
    def get_connected_device(self, requester_name, device):
        LOGGER.debug('get_connected_device, request by [%s]', requester_name)
        time1 = time.time()
        dev_name = device.get('ip_address')
        password_encoded = self.config.get('password_encoded', False)
        if password_encoded:
            pwd = utils.decrypt_passwd(device.get('password'))
        else:
            pwd = device.get('password')
        dev = Device(host=dev_name, user=device.get('user'),
                     password=pwd, port=device.get('port', 22), gather_facts=False, auto_probe=5)
        event = threading.Event()

        def dev_open():
            try:
                dev.open()
            except Exception as e:
                LOGGER.error(
                    'connect, for device [%s, %s] failed',
                    dev_name,
                    e)
            finally:
                LOGGER.debug(
                    'connect, for device %s %d(s)',
                    dev_name,
                    (time.time() - time1))
                event.set()

        thread = threading.Thread(
            target=dev_open,
            name='DevThread-' +
                 dev_name)
        thread.start()
        time_out = self.config.get('connect_timeout', 1) * 60
        rpc_timeout = self.config.get('rpc_timeout', 1) * 60
        event.wait(time_out)
        if not event.isSet():
            LOGGER.critical(
                'connect, not returned, close forcefully [%s] ',
                dev_name)
            self.close_connected_device('self', dev)
            return None
        if dev.connected:
            dev.timeout = rpc_timeout
            return dev
        return None
def main(peer_asn):
    list_peering_ips_of_target_asn = []

    config = ConfigDictionary("etc/pypeer.ini")
    username = config.username()
    password = config.password()

    for router in config.get_list_of_router_names():
        jdev = Device(user=username, host=config.get_router_ip(router),
                      password=password)
        jdev.open(gather_facts=False)
        jdev.timeout = 600

        try:
            resultxml = jdev.rpc.get_bgp_summary_information()
        except Exception as err:
            print "CMD:"
            etree.dump(err.cmd)
            print "RSP:"
            etree.dump(err.rsp)

        bgpsum = BgpData(resultxml)
        for thispeeringip in bgpsum.get_list_ipaddr_from_asn(peer_asn):
            list_peering_ips_of_target_asn.append(thispeeringip)

    exchange = Exchange()
    list_sessions_configured_peeringdbid_exchanges_of_target_asn = []
    for peering_ip in list_peering_ips_of_target_asn:
        if not is_valid_ipv4_address(peering_ip):
            next
        else:
            peeringdb_id = exchange.get_exchange_from_peerip(peering_ip)['peeringdbid']
            list_sessions_configured_peeringdbid_exchanges_of_target_asn.append(peeringdb_id)

    peeringdb = PeeringDBClient()
    peeringjson = PeeringDBParser(peeringdb.asn(peer_asn))
    list_their_exchanges = peeringjson.get_list_of_asn_exchanges()

    mutual_exchanges = set(config.get_list_of_connected_exchanges()).intersection(list_their_exchanges)
    missing_exchanges = set(mutual_exchanges).difference(list_sessions_configured_peeringdbid_exchanges_of_target_asn)

    print "Missing exchanges are:" + str(missing_exchanges)

    for pdbid in missing_exchanges:
        ixpparser    = PeeringDBParser(peeringdb.ixlan(pdbid))
        print str(pdbid) + ": " + ixpparser.get_name_of_ixorg_from_ixlan()
def main(peer_asn, output):
    config = ConfigDictionary()
    username = config.username()
    password = config.password()

    local_preferences = config.get_list_of_localpref()

    for router in config.get_list_of_router_names():
        jdev = Device(user=username, host=config.get_router_ip(router), password=password)
        jdev.open(gather_facts=False)
        jdev.timeout = 600
        regex_asn = ".* " + peer_asn

        try:
            resultxml = jdev.rpc.get_route_information(table='inet.0',
                                                       aspath_regex=regex_asn,
                                                       extensive=True)

        except Exception as err:
            print "CMD:"
            etree.dump(err.cmd)
            print "RSP:"
            etree.dump(err.rsp)

        jdev.close()

        sorted_routes = {}
        sorted_routes["peer"] = []
        sorted_routes["peer-indirect"] = []
        sorted_routes["peer-routeserver"] = []
        sorted_routes["transit"] = []
        sorted_routes["customer"] = []
        sorted_routes["outofrange"] = []

        for routexml in resultxml.findall('.//rt'):
            route = RouteData(routexml)
            full_prefix = route.prefix()
            session_type = route.get_adjacency_type(local_preferences)
            sorted_routes[session_type].append(full_prefix)

        if output == 'machine':
            print json.dumps(sorted_routes)
        else:
            print "For " + router + " in " + config.get_router_location(router) + " Routes: " + str(len(sorted_routes["peer"])) + " via bilateral peering, " + str(len(sorted_routes["peer-routeserver"])) + " via peering at mlp, " + str(len(sorted_routes["peer-indirect"])) + " via peering in other cities " + str(len(sorted_routes["customer"])) + " via customer, and " + str(len(sorted_routes["transit"])) + " via transit."
Esempio n. 21
0
def main(args):
    '''Acquire necessary input options, interact with SRX device as specified per CLI args.'''
    parser = argparse.ArgumentParser(
        description='Interact with specified SRX device as specified')
    parser.add_argument('--version', action='version', version=__version__)
    parser.add_argument('-d', '--datafile', help='specify YAML file to read router info from',
                        default=SRX_FILE)
    parser.add_argument('--prompt', action='store_true',
                        help='prompt for router info (do not try to read in from file)',
                        default=False)
    parser.add_argument('-v', '--verbose', action='store_true', help='display verbose output',
                        default=False)
    args = parser.parse_args()
    # Debugging
    #if args.verbose:
    #    print 'Args = {}'.format(args)

    # Initialize data structures
    mysrx = {}
    if not args.prompt:
        mysrx = yaml_input(args.datafile, args.verbose)
        # Debugging
        #if args.verbose:
        #    print 'mysrx = {}'.format(mysrx)
    else:
        if args.verbose:
            print 'Prompting specified - asking user for all connection details'
    check_input(mysrx, args.verbose)

    mysrx_conn = Device(host=mysrx['HOST'], user=mysrx['USER'], password=mysrx['PASSWORD'])
    if args.verbose:
        print 'Opening NETCONF connection to {}...'.format(mysrx['HOST'])
    mysrx_conn.open()
    # Set timeout - default of 30 seconds can be problematic, must set after open()
    mysrx_conn.timeout = TIMEOUT

    print 'Device facts for {}:'.format(mysrx['HOST'])
    pprint(mysrx_conn.facts)
    mysrx_conn.close()
Esempio n. 22
0
def main(device_ip, peer_asn, output):
    config = ConfigDictionary()
    username = config.username()
    password = config.password()

    jdev = Device(user=username, host=device_ip, password=password)
    jdev.open(gather_facts=False)
    jdev.timeout = 600
    regex_asn = ".* " + peer_asn

    try:
        resultxml = jdev.rpc.get_route_information(table='inet.0',
                                                   aspath_regex=regex_asn,
                                                   extensive=True)

    except Exception as err:
        print "CMD:"
        etree.dump(err.cmd)
        print "RSP:"
        etree.dump(err.rsp)

    jdev.close()

    sorted_routes = {}
    sorted_routes["peer"] = []
    sorted_routes["transit"] = []
    sorted_routes["customer"] = []
    sorted_routes["outofrange"] = []

    for routexml in resultxml.findall('.//rt'):
        route = RouteData(routexml)
        full_prefix = route.prefix()
        session_type = config.get_type_from_localpref(route.activelocalpref())
        sorted_routes[session_type].append(full_prefix)

    if output == 'machine':
        print json.dumps(sorted_routes)
    else:
        print "Number of routes: " + str(len(sorted_routes["peer"])) + " via peering, " + str(len(sorted_routes["customer"])) + " via customer, and " + str(len(sorted_routes["transit"])) + " via transit. (use -m for full output and breakdown)"
def main():
    list_peeringdbid_of_connected_exchanges = []

    config = ConfigDictionary("/home/andy/etc/pypeer.ini")
    username = config.username()
    password = config.password()
    exchange = Exchange()
    peeringdb = PeeringDBClient()

    for router in config.get_list_of_router_names():
        jdev = Device(user=username, host=config.get_router_ip(router),
                      password=password)
        jdev.open(gather_facts=False)
        jdev.timeout = 600

        try:
            resultxml = jdev.rpc.get_bgp_summary_information()
        except Exception as err:
            print "CMD:"
            etree.dump(err.cmd)
            print "RSP:"
            etree.dump(err.rsp)

        bgpsum = BgpData(resultxml)
        for thispeeringip in bgpsum.get_list_peering_ips():
            if not is_valid_ipv4_address(thispeeringip):
                next
            else:
                peeringdb_id = exchange.get_exchange_from_peerip(thispeeringip)['peeringdbid']
                if peeringdb_id==0:
                    next 
                elif peeringdb_id in list_peeringdbid_of_connected_exchanges:
                    next
                else:
                    list_peeringdbid_of_connected_exchanges.append(peeringdb_id)
                    ixpjson      = peeringdb.ixlan(peeringdb_id)
                    ixpparser    = PeeringDBParser(ixpjson)
                    print "%s: %s   # %s" % (peeringdb_id, router, ixpparser.get_name_of_ixorg_from_ixlan())
Esempio n. 24
0
def main():
	#iterate over csv
	for row in devList:

        	#Make Device Connection
        	dev = Device(host=row[1], user=userName, password=userPassword)
		try:
       			#Print Opening Header for Status
			now1 = datetime.datetime.now()
			pprint("Work starts on " + row[0] + " | " + row[1] + " at " + now1.strftime("%Y-%m-%d %H:%M"))

			#Open Device with Custom Timer
			dev.open()
			dev.timeout = 300

        	except Exception as err:                               
                	sys.stderr.write('Cannot connect to device: {0}\n'.format(err))

		#Do RPC/Work
		try:
			#Start Work here

			rsp = dev.rpc.file_archive(destination='/var/tmp/logs', source='/var/log/*', compress=True)

                	with SCP(dev, progress=True) as scp1:
				scp1.get("/var/tmp/logs.tgz", local_path=output+"/"+row[0]+"-"+now1.strftime("%Y-%m-%d-%H%M")+"-logs.tgz")

			#Write Element Seperator	
			print("+++++++++++++++++++++++++++++++++++++++++++++++++")
		
			#Close Device
			dev.close()

        	except Exception as err:
                	sys.stderr.write('Cannot perform RPC on device: ' + row[1] + '\n'.format(err))
                	print("+++++++++++++++++++++++++++++++++++++++++++++++++")
Esempio n. 25
0
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
from getpass import getpass

a_device = Device(host="srx2.lasthop.io", user="******", password=getpass())
a_device.open()
a_device.timeout = 60

cfg = Config(a_device)
cfg.lock()

cfg.load(path="test_config.xml", format="xml", merge=True)
# overwrite=True - full configuration replace so be careful
# cfg.commit(confirm=1)
# cfg.commit()
# cfg.unlock()

Esempio n. 26
0
                logger.info('[%s]: timestamp_tracking - CLI collection %s', host, timestamp_tracking['collector_ssh_ends']-timestamp_tracking['collector_ssh_start'])

        else: # By default it's a junos device
            # We need to CATCH errors then print then but we need to continue with next host...
            connected = False
            logger.info('Connecting to host: %s', host)
            target_commands = get_target_commands(host)
            timestamp_tracking={}
            timestamp_tracking['collector_start'] = int(datetime.today().strftime('%s'))
            # Establish connection to hosts
            user, passwd = get_credentials(host)
            jdev = Device(user=user, host=host, password=passwd, gather_facts=False, auto_probe=True, port=22)
            for i in range(1, max_connection_retries+1):
                try:
                    jdev.open()
                    jdev.timeout = default_junos_rpc_timeout
                    connected = True
                    break
                except Exception, e:
                    if i < max_connection_retries:
                        logger.error('[%s]: Connection failed %s time(s), retrying....', host, i)
                        time.sleep(1)
                        continue
                    else:
                        logging.exception(e)
                        connected = False  # Notify about the specific problem with the host BUT we need to continue with our list
            # First collect all kpi in datapoints {} then at the end we insert them into DB (performance improvement)
            if connected:
                datapoints = []
                # By default execute show version in order to get version and platform as default tags for all kpi related to this host
                kpi_tags = {}
Esempio n. 27
0
#!/usr/bin/python

from jnpr.junos import Device
from jnpr.junos.utils.config import Config
from jnpr.junos.factory import loadyaml

from getpass import getpass
host = raw_input('hostname or address: ')
username = raw_input('username: '******'password: '******'showinterface.yml'))

dev = Device(host, user=username, password=passwd)
#dev = Device(hostname, user=username, password=passwd)

dev.open()
dev.timeout = 60

interface_table = ShowInterface(dev)
interface_table.get()

for i in interface_table:
  if "ge" in i.name or "fe" in i.name or "vlan" in i.name:
    if i.addrfamily == 'inet' and i.status == 'up':
      print str(i.logicalname) +  " " + str(i.logicalinetaddr)
#      print i.name + " " + str(i.description) + " " + str(i.linkmode) + " " + str(i.linkspeed) + " " + str(i.logicalname) + " " + str(i.logicalinetaddr)

dev.close()
Esempio n. 28
0
    def reboot_device(self, ip, hostname):
        # Reboots a device
        # Status dictionary for post-upgrade reporting
        statusDict = {}
        if Menu.upgrade_list == '':
            statusDict['Upgrade_List'] = 'Juniper-Upgrade_' + Menu.username
        else:
            statusDict['Upgrade_List'] = Menu.upgrade_list
        statusDict['Upgrade_Start'] = ''
        statusDict['Upgrade_Finish'] = '-'
        statusDict['IP'] = ip
        statusDict['Connected'] = 'N'
        statusDict['OS_installed'] = '-'
        statusDict['Rebooted'] = 'N'
        statusDict['IST_Confirm_Loaded'] = '-'
        statusDict['IST_Confirm_Rebooted'] = ''
        statusDict['Comments'] = ''

        # Start the logging
        now = datetime.datetime.now()
        date_time = now.strftime("%Y-%m-%d-%H%M")
        reboot_log = Menu.log_dir + "juniper-reboot-LOG_" + date_time + "_" + Menu.username + ".log"
        logging.basicConfig(filename=reboot_log,
                            level=logging.INFO,
                            format='%(asctime)s:%(name)s: %(message)s')
        logging.getLogger().name = ip
        print('Information logged in {0}'.format(reboot_log))

        # Display basic information
        self.do_log("Device: {0} ({1})".format(hostname, ip))
        now = datetime.datetime.now()
        formattime = now.strftime("%Y-%m-%d %H:%M")
        self.do_log("Timestamp: {0}".format(formattime))

        # Verify package exists before starting upgrade process
        dev = Device(ip, user=Menu.username, password=Menu.password)
        # Try to open a connection to the device
        try:
            self.do_log('\n')
            self.do_log(
                '------------------------- Opening connection to: {0} -------------------------\n'
                .format(ip))
            self.do_log('User: {0}'.format(Menu.username))
            dev.open()
        # If there is an error when opening the connection, display error and exit upgrade process
        except Exception as err:
            sys.stderr.write('Cannot connect to device: {0}\n'.format(err))
        else:
            # Record connection achieved
            statusDict['Connected'] = 'Y'

            # Increase the default RPC timeout to accommodate install operations
            dev.timeout = 600
            # Create an instance of SW
            sw = SW(dev)

            # Logging
            now = datetime.datetime.now()
            statusDict['Upgrade_Start'] = now.strftime("%Y-%m-%d %H:%M")
            self.do_log('Timestamp: {0}'.format(statusDict['Upgrade_Start']))
            self.do_log('Beginning reboot cycle, please be patient.')

            # Attempt to reboot
            try:
                rsp = sw.reboot()
                self.do_log(rsp)
            except Exception as err:
                msg = 'Unable to reboot system, {0}'.format(err)
                self.do_log(msg, level='error')
            else:
                # Record reboot
                statusDict['Rebooted'] = 'Y'

            # End the NETCONF session and close the connection
            dev.close()
            self.do_log('\n')
            self.do_log(
                '------------------------- Closed connection to: {0} -------------------------\n'
                .format(ip))

        return statusDict
Esempio n. 29
0
if __name__ == '__main__':

    # Create instance of device object
    device_conn = Device(**srx2)
    # Establish the NETCONF connection
    device_conn.open()
    # Call the gather_routes function to get the routing tale of the device
    old_routing_table = gather_routes()
    # Print the routing table
    print("Routing table before the change")
    pprint(old_routing_table)
    print('-' * 40)

    # Exercise 4b
    # Increase the timeout
    device_conn.timeout = 60
    # Pass NETCONRF connection into Config Object
    cnfg = Config(device_conn)
    # Lock configuration exclusively to us
    cnfg.lock()
    # Load configuration from external file in curly brace notation
    cnfg.load(path='routing.conf', format='text', merge=True)
    # Commit the configuration
    cnfg.commit()

    # Exercise 4c
    new_routing_table = gather_routes()
    # Print the routing table after the change so we can compare
    print("Routing table after the change")
    pprint(new_routing_table)
Esempio n. 30
0
def getInterfaces(ipAddress, junosUsername, junosPassword):
    #Creates a view for PyEZ
    yml = '''
---
EthPortTable:
  rpc: get-interface-information
  args:
    extensive: True
    interface_name: '[agxe][et]*'
  args_key: interface_name
  item: physical-interface
  view: EthPortView

EthPortView:
  fields:
    interface: name
    description: description
    admin_status: admin-status
    link_status: oper-status
    bpdu_error: bpdu-error
    mtu: mtu
    mode: logical-interface/address-family/address-family-flags/ifff-port-mode-trunk
    inet: logical-interface/address-family/interface-address
    lacp: logical-interface/address-family/ae-bundle-name
'''
    globals().update(FactoryLoader().load(yaml.load(yml, Loader=yaml.FullLoader))) #loads the yaml file
    dev = Device(host=ipAddress, user=junosUsername, password=junosPassword)
    try:
        dev.open(auto_probe=5)
    except:
        return {"Error": "Connection refused"}
    dev.timeout=120
    interfaceTable = EthPortTable(dev).get()
    if len(interfaceTable)<1:
        dev.close()
        return {"Error":"Failed to find interface"}
    interfaceList=[]
    for i in interfaceTable:
        scripts=[{"script":"setInterfaceDescription","name":"Set Description"}, {"script":"getInterfaceConfig","name":"Get Configuration"}]
        if i.mtu=="9216":
            jumbo="enabled"
        else:
            jumbo="disabled"
        try:
            if len(i.mode)>0:
                mode="trunk"
        except:
            try:
                if len(i.inet)>0:
                    mode="layer 3"
            except:
                try:
                    if len(i.lacp)>0:
                        mode="lacp-"+i.lacp.split(".")[0]
                except:
                    mode="access"
        if mode=="trunk":
            scripts.append({"script":"trunkVLANdelete","name":"Delete Trunk VLAN"})
            scripts.append({"script":"changeAccessVLAN","name":"Change Access VLAN"})
            scripts.append({"script":"trunkVLANadd","name":"Add Trunk VLAN"})
        if mode=="access":
            scripts.append({"script":"changeAccessVLAN","name":"Change Access VLAN"})
            scripts.append({"script":"trunkVLANadd","name":"Add Trunk VLAN"})
        if i.link_status=="up" and (mode=="trunk" or mode=="access"):
            scripts.append({"script":"getMACtable","name":"Get MAC Table"})
        interfaceList.append({
            'name':i.name,
            'description' : i.description,
            'admin_status' : i.admin_status,
            'link_status' : i.link_status,
            'jumbo_frames':jumbo,
            'mode':mode,
            'scripts':scripts
        })
    dev.close()
    upCount=0
    #counts up interfaces
    for i in interfaceList:
        if i['link_status']=='up':
            upCount+=1

    try:
        alarmCount=getAlarms(ipAddress=ipAddress)[1]['alarm-summary']['active-alarm-count']
    except:
        alarmCount=0
    updateSwitchInterfaceList(ipAddress=ipAddress, intNum=len(interfaceList), intUp=upCount, alarm_count=int(alarmCount))

    return interfaceList
Esempio n. 31
0
def collector(**kwargs):

    for host in kwargs["host_list"]:
        kpi_tags={}
        latest_datapoints={}
#        if ((db_schema == 1) and (not(use_hostname))):
        if (not(use_hostname)):
            latest_datapoints = get_latest_datapoints(host=host)
            logger.debug("Latest Datapoints are:")
            logger.debug(pformat(latest_datapoints))

        #    kpi_tags = get_host_base_tags(host=host)
        # Check host tag to identify what kind of connections will be used (ej junos / others  / etc)
        if "non_junos_devices" in hosts[host].split():
            pass
            # Reserved for future purposes
        else: # By default it's a junos device
            # We need to CATCH errors then print then but we need to continue with next host...
            connected = False
            logger.info('Connecting to host: %s', host)
            target_commands = get_target_commands(host)
            timestamp_tracking={}
            timestamp_tracking['collector_start'] = int(datetime.today().strftime('%s'))
            # Establish connection to hosts
            user, passwd, authMethod,authKey_file = get_credentials(host)
            if dynamic_args['test']:
                #Open an emulated Junos device instead of connecting to the real one
                _rpc_reply_dict = rpc_reply_dict()
                _rpc_reply_dict['dir'] = BASE_DIR_INPUT

                jdev = mocked_device(_rpc_reply_dict)
                # First collect all kpi in datapoints {} then at the end we insert them into DB (performance improvement)
                connected = True
            else:
                if authMethod in "key":
                    jdev = Device(user=user, host=host, ssh_private_key_file=authKey_file, gather_facts=False, auto_probe=True, port=22)
                elif authMethod in "enc_key":
                    jdev = Device(user=user, host=host, ssh_private_key_file=authKey_file, password=passwd, gather_facts=False, auto_probe=True, port=22)
                else: # Default is 
                    jdev = Device(user=user, host=host, password=passwd, gather_facts=False, auto_probe=True, port=22)
                for i in range(1, max_connection_retries+1):
                    try:
                        jdev.open()
                        jdev.timeout = default_junos_rpc_timeout
                        connected = True
                        break
                    except Exception, e:
                        if i < max_connection_retries:
                            logger.error('[%s]: Connection failed %s time(s), retrying....', host, i)
                            time.sleep(1)
                            continue
                        else:
                            logging.exception(e)
                            connected = False  # Notify about the specific problem with the host BUT we need to continue with our list
            # First collect all kpi in datapoints {} then at the end we insert them into DB (performance improvement)
            if connected:
                datapoints = []
                # By default execute show version in order to get version and platform as default tags for all kpi related to this host
                kpi_tags = {}
                target_command = 'show version | display xml'
                version_xpath = "//package-information/comment"
                product_model_xpath = "//product-model"
                logger.info('[%s]: Executing command: %s', host, target_command)
                result = execute_command(jdev,target_command)
                if result:
                    logger.debug('[%s]: Parsing command: %s', host, target_command)
                    xml_data = etree.fromstring(result)
                    value_tmp = xml_data.xpath(version_xpath)[0].text.strip()
                    version = re.search('\[(.*?)\]$', value_tmp)
                    if version:
                        kpi_tags['version'] = version.group(1)
                    else:
                        kpi_tags['version'] = 'unknown'
                    value_tmp = xml_data.xpath(product_model_xpath)[0].text.strip()
                    kpi_tags['product-model'] = convert_variable_type(value_tmp)

                    ## Based on parameter defined in config file

                    if use_hostname:
                        hostname_xpth = "//host-name"
                        hostname_tmp = xml_data.xpath(hostname_xpth)[0].text.strip()
                        hostname = convert_variable_type(hostname_tmp)
                        logger.info('[%s]: Host will now be referenced as : %s', host, hostname)
                        host = hostname
#                        if (db_schema == 1):
#                            latest_datapoints = get_latest_datapoints(host=host)
#                            logger.info("Latest Datapoints are:")
#                            logger.info(pformat(latest_datapoints))
                        latest_datapoints = get_latest_datapoints(host=host)
                        logger.debug("Latest Datapoints are:")
                        logger.debug(pformat(latest_datapoints))
                    else:
                        logger.info('[%s]: Host will be referenced as : %s', host, host)


                    kpi_tags['device']=host
                    kpi_tags['kpi']="base-info"
                    match={}
                    match["variable-name"]="base-info"
                    # We'll add a dummy kpi in oder to have at least one fixed kpi with version/platform data.
                    get_metadata_and_add_datapoint(datapoints=datapoints,match=match,value_tmp=value_tmp,latest_datapoints=latest_datapoints,host=host,kpi_tags=kpi_tags)

                # Now we have all hosts tags that all host kpis will inherit
                # For each target_command execute it, parse it, and insert values into DB
                timestamp_tracking['collector_cli_start'] = int(datetime.today().strftime('%s'))
                for target_command in target_commands:
                    logger.info('[%s]: Executing command: %s', host, target_command)
                    # Execute rpc/command on host and get result
                    result = execute_command(jdev,target_command)
                    if result:
                        logger.debug('[%s]: Parsing command: %s', host, target_command)
                        parse_result(host,target_command,result,datapoints,latest_datapoints,kpi_tags)
                        time.sleep(delay_between_commands)

                try:
                    jdev.close()
                    time.sleep(0.5)
                except Exception, e:
                    print "ERROR: Something wrong happens when closing the connection with the device"
                    logging.exception(e)

                timestamp_tracking['collector_cli_ends'] = int(datetime.today().strftime('%s'))
                logger.info('[%s]: timestamp_tracking - CLI collection %s', host, timestamp_tracking['collector_cli_ends']-timestamp_tracking['collector_cli_start'])
                timestamp_tracking['collector_ends'] = int(datetime.today().strftime('%s'))

                # Add open-nti internal kpi 
                collection_time = timestamp_tracking['collector_ends']-timestamp_tracking['collector_start']
                #kpi_tags['device']=host
                kpi_tags['stats']="collection-time"
                match={}
                match["variable-name"]="open-nti-stats"
                value_tmp =collection_time
                # We'll add a dummy kpi in oder to have at least one fixed kpi with version/platform data.
                get_metadata_and_add_datapoint(datapoints=datapoints,match=match,value_tmp=value_tmp,latest_datapoints=latest_datapoints,host=host,kpi_tags=kpi_tags)

                #kpi_tags['device']=host
                kpi_tags['stats']="collection-successful"
                match={}
                match["variable-name"]="open-nti-stats"
                value_tmp = 1
                # We'll add a dummy kpi in oder to have at least one fixed kpi with version/platform data.
                get_metadata_and_add_datapoint(datapoints=datapoints,match=match,value_tmp=value_tmp,latest_datapoints=latest_datapoints,host=host,kpi_tags=kpi_tags)



                if datapoints:   # Only insert datapoints if there is any :)
                    insert_datapoints(datapoints)

                
                logger.info('[%s]: timestamp_tracking - total collection %s', host, collection_time)
            else:
                logger.error('[%s]: Skipping host due connectivity issue', host)

                datapoints = []
                latest_datapoints = get_latest_datapoints(host=host)
                # By default execute show version in order to get version and platform as default tags for all kpi related to this host
                kpi_tags['device']=host
                kpi_tags['stats']="collection-failure"
                match={}
                match["variable-name"]="open-nti-stats"
                value_tmp = 1
                # We'll add a dummy kpi in oder to have at least one fixed kpi with version/platform data.
                get_metadata_and_add_datapoint(datapoints=datapoints,match=match,value_tmp=value_tmp,latest_datapoints=latest_datapoints,host=host,kpi_tags=kpi_tags)

                if datapoints:   # Only insert datapoints if there is any :)
                    insert_datapoints(datapoints)
Esempio n. 32
0
Add code to attempt to lock the configuration again.
Gracefully handle the "LockError" exception (meaning the configuration is already locked).

"""

from jnpr_devices import srx2 # SRX device dictionary
from jnpr.junos import Device # Device class
from jnpr.junos.utils.config import Config # Config class
from jnpr.junos.exception import LockError # For gracefully handling lock error
from getpass import getpass

# Create device object
device_conn = Device(**srx2)
device_conn.open() # Establish NETCONF connection
device_conn.timeout = 60 # Change device timeout

# Pass in NETCONF connection to Config object
cnfg = Config(device_conn)
print(cnfg.lock()) # Lock device to others so only we can be in config mode
# Put pause in the program otherwise after program ends and connection terminates the lock is automatically released
pause = input("Go check if you can go into config mode on the device: ")

# Gracefully catch the lock error exception which is thrown when we try to lock the config a 2nd time
try:
    cnfg.lock()
    pause = input("Wait: ")
except LockError: # Run this code if this exception thrown
    print("Error: The configuration is already locked for editing")
    pause = input("Wait: ")
Esempio n. 33
0
def myprint(str):

    print(time.strftime(" %H:%M:%S", time.gmtime()) + "  " + str)


host = raw_input('hostname: ')
user = raw_input('username: '******'password: '******'Unable to connect: ', error
    exit()
dev.timeout = 2500

js = SnapAdmin()
config_file = "junos-upgrade.yml"

myprint("waiting 10 seconds before taking post snapshot....")
time.sleep(10)
myprint("gathering snapshot after upgrade....")
snapvalue = js.snap(config_file, "post", dev=dev)

print
myprint("Comparing pre and post snapshots")
snapvalue = js.check(config_file, "pre", "post", dev=dev)
Esempio n. 34
0
    for value in line:
        device.append(value)

device = [x.strip('\n') for x in device]
for item in device:
    dev = Device(user=user, host=item, password=passwd)
    dev.probe()
    print 'Opening connection to %s for user %s ...' % (item, user)

    if dev.probe() is False:
        print '%s is not reachable.  Skipping..' % (item)
    elif dev.probe() is True:
        dev.open()
        print 'Setting device parameter'

        dev.timeout = 10 * 60
        cu = Config(dev)

        print 'Setting banner message'

        set_cmd = 'set system login message "WARNING - THIS IS A PRIVATE SYSTEM"'
        cu.load(set_cmd, format='set')
        cu.pdiff()
        cu.commit_check

        print 'Check passed, committing changes...'

        cu.commit()

        print 'Exiting %s ' % (item)
Esempio n. 35
0
    }

    template_file = 'build_bgp.j2'
    with open(template_file) as f:
        bgp_template = f.read()

    for router_id, router_vars in my_vars.items():
        template = jinja2.Template(bgp_template)
        rtr_cfg = template.render(router_vars)
        print()
        print('-' * 60)
        print("This is router: {}".format(router_id))
        print(rtr_cfg)
        print()

        # Connecting to device
        host = router_vars['management_ip']
        a_device = Device(host=host, user=USER, password=PASSWD)
        a_device.open()
        a_device.timeout = 90
        cfg = Config(a_device)
        cfg.load(rtr_cfg, format="text", merge=False)
        diff = cfg.diff()
        print(diff)
        hit_enter()
        if diff:
            print("Commit changes")
            cfg.commit()
        else:
            print("No changes")
Esempio n. 36
0
"""

from jnpr.junos import Device
from jnpr.junos.utils.config import Config
from jnpr.junos.op.lldp import LLDPNeighborTable
from jnpr.junos.op.ethport import EthPortTable
from jnpr.junos.op.routes import RouteTable
from jnpr.junos.exception import LockError

from getpass import getpass
from pprint import pprint

password = getpass()
a_device = Device(host='184.105.247.76', user='******', password=password)
print a_device.open()
a_device.timeout = 300

#### Facts
pprint(a_device.facts)

#### Getter operations
print "\nLLDP Get"
print '-' * 50
lldp = LLDPNeighborTable(a_device)
lldp.get()

for k, v in lldp.items():
    print k
    for entry in v:
        print entry
Esempio n. 37
0
#!/usr/bin/env/python

from jnpr.junos import Device
from jnpr.junos.op.routes import RouteTable
from jnpr.junos.utils.config import Config
from pprint import pprint
from jnpr_devices import srx2
from task2 import gather_routes

connection = Device(**srx2)
connection.open()
connection.timeout = 60

pprint(gather_routes())
print("\n" + "#" * 20 + "\n")

cfg = Config(connection)
cfg.lock()
cfg.load(path="task4.conf", format="text", merge=True)
cfg.commit()

pprint(gather_routes())
print("\n" + "#" * 20 + "\n")

cfg.load("delete routing-options static route 203.0.113.55/32 discard",
         format="set",
         merge=True)
cfg.load("delete routing-options static route 203.0.113.56/32 discard",
         format="set",
         merge=True)
cfg.commit()
Esempio n. 38
0
    copy_Manifest_to_tempManifest()
    print name
    create_newFeed(feed)
    create_manifest_entry(name)
    copy_tempManifest_to_Manifest()
    #print "Completed, add the following line to your SRX to accept feed:\n set security dynamic-address address-name "+name+ " profile category IPFilter feed "+name 
    username = raw_input("Please enter your SRX Username:"******"Logging into SRX "+srx
     login=str(srx) 
     dev = Device(host=login,user=username,password=password)
     dev.open()
     dev.timeout = 300
     cu = Config(dev)
     set_cmd = 'set security dynamic-address address-name '+name+' profile category IPFilter feed '+name
     cu.load(set_cmd, format='set')
     print "Applying changes, please wait...."
     cu.commit()
     dev.close()
     
     
     
     




if sys.argv[1]=='drop':
if ssh.connect:
    # SCPCLient takes a paramiko transport as an argument
    scp = SCPClient(ssh.get_transport())
    print(style.BOLD + "- Transferring CA Cert file to vSRX..." + style.END)
    scp.put(filename)
else:
    print("!! No active ssh connection. Exiting!!")


######################################
# Configure SSL Proxy on vSRX
######################################
# Make connection to the vSRX and check to see if the cert already exists
try:
    dev.open()
    dev.timeout = 300
    look = dev.rpc.get_pki_local_certificate(certificate_id=cert_id)

except ConnectError as err:
	print("!!..The vSRX appears to be down..!!")
	print("Cannot connect to device: {0}".format(err))
	sleep(3)
	sys.exit(1)

except Exception as err:
	print("!!..The vSRX appears to be down..!!")
	print(err)
	sleep(3)
	sys.exit(1)

# Figure out what to do
Esempio n. 40
0
    ROUTES_LIST_DIFF = list(ROUTES_SET_DIFF)

    print("#" * 50)
    print("ROUTES THAT WERE ADDED: ")
    print("#" * 50)
    print(ROUTES_LIST_DIFF)
    print()


if __name__ == "__main__":

    srx2_device = Device(**jnpr_devices.srx2)

    srx2_device.open()

    srx2_device.timeout = 60

    # Checking the RIB before the change

    rib_table = gather_routes(srx2_device)

    print_rib(rib_table, "BEFORE")

    # Inventory Routes before the change

    ROUTES_BEFORE = list()

    for k in rib_table.keys():

        ROUTES_BEFORE.append(k)
Esempio n. 41
0
#!/usr/bin/env python
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
from jnpr.junos.exception import LockError

from pprint import pprint
from jnpr_devices import srx2

jnpr_device = Device(**srx2)
jnpr_device.open()
jnpr_device.timeout = 60

# 3a
jnpr_device_cfg = Config(jnpr_device)

jnpr_device_cfg.lock()

try:
    jnpr_device_cfg.lock()
except LockError as e:
    print("Config session already locked")

# 3b
print("Set new hostname in candidate config")
jnpr_device_cfg.load("set system host-name srx2-new-name",
                     format="set",
                     merge=True)

# 3c
print("Check diff")
print(jnpr_device_cfg.diff())
Esempio n. 42
0
from jnpr.junos import Device
from getpass import getpass




pwd = getpass()


a_device = Device(host='50.242.94.227', user='******', password=pwd)
a_device.timeout = 120 
a_device.open()

a_device.facts


from jnpr.junos.utils.config import Config
cfg = Config(a_device)

cfg.lock()

cfg.load("set system host-name pytest", format="set", merge=True)

cfg.rollback(0)


print cfg.diff()

cfg.commit(comment="testing commit comment using PyEZ")

Esempio n. 43
0
def update_process(type,host,package,threadIns):
    
    # verify package exists
    if type=='OS' or type=='Firmware':
        if not (os.path.isfile(package)):
            msg = 'Software package does not exist: {0}. '.format(package)
            logging.error(msg)
            print msg
            sys.exit()
    time.sleep(20)
    dev=None
    dev = Device(host=host,user=junos_username, passwd=junos_password)
    rebootTime=10
    rebootOK=False
    while not rebootOK:
        try:
            time.sleep(10)
            rebootOK=True
            dev.open()
        except Exception as err:
            print threadIns.hostIP+threadIns.serialNumber+':wait device time='+str(rebootTime)+"Sec  "+str(err)
            logging.info(threadIns.hostIP+threadIns.serialNumber+':wait device time='+str(rebootTime)+"Sec  "+str(err))
            rebootTime+=10
            rebootOK=False

        if rebootTime>=rebootTimeout:
            logging.error('Cannot connect to device\n')
            print 'Cannot connect to device\n'
            return
    
        



    
        
    if type=='createLogFile':
        threadIns.status="createLog"
        dev.timeout = 100
        create_log(dev)
        dev.close()
        return
        
    if type=='checkSerialNumber':
        threadIns.status="checkSN"
        threadIns.serialNumber=dev.facts['serialnumber']
        dev.close()
        return
    
    if type=='Snapshot':
        threadIns.status="Snapshot"
        dev.timeout = 600
        dev.rpc.request_snapshot(slice='alternate')
        dev.close()
        return
    
    if type=='setFixIP':
        threadIns.status="setFixIP"
        cu = Config(dev)
        cu.load('delete interfaces ge-0/0/0', format='set')
        cu.load('set interfaces ge-0/0/0 unit 0 family inet address '+threadIns.hostIP+'/'+str(dhcpNetmask), format='set')
        cu.pdiff()
        cu.commit()
        return
    
    
    
    
    # Create an instance of SW
    sw = SW(dev)
    
    threadIns.status="install"+type
    try:
        logging.info('Starting the software upgrade process: {0}'.format(package))
        okOS = sw.install(package=package, remote_path=remote_path,progress=update_progress, validate=validate)
        #okOS=True
    except Exception as err:
        msg = 'Unable to install software, {0}'.format(err)
        logging.error(msg)
        okOS = False
    
    
    if type=="Firmware":
        #UPGRADED SUCCESSFULLY
        logging.info("!!!!!!!!!!!!!")
        logging.info(dev.cli("request system firmware upgrade pic pic-slot 0 fpc-slot 1"))
        threadIns.status="checkFirmware"
        
        okFirmware=False
        #okFirmware=True
        firmwareTime=0
        
        while not okFirmware and okOS:
            xmlStr=etree.tostring(dev.rpc.get_system_firmware_information(), encoding='unicode')
            xmlRoot = ET.fromstring(xmlStr)
            for firmware_status in xmlRoot.iter('firmware-status'):
                #UPGRADED SUCCESSFULLY
                if firmware_status.text=='UPGRADED SUCCESSFULLY':okFirmware=True
                if firmware_status.text!='OK':print firmware_status.text
            print threadIns.hostIP+threadIns.serialNumber+":wate firmware upgrade "+str(firmwareTime)+" Sec"
            time.sleep(1)
            firmwareTime=firmwareTime+1
            #print okFirmware
    elif type=="OS":okFirmware=True
    
    
    
    threadIns.status="reboot after install"+type
    if okOS is True and okFirmware is True:
        logging.info('Software installation complete. Rebooting')
        rsp = sw.reboot()
        logging.info('Upgrade pending reboot cycle, please be patient.')
        logging.info(rsp)
    else:
        msg = 'Unable to install software, {0}'.format(okOS)
        logging.error(msg)
    
    # End the NETCONF session and close the connection
    dev.close()
Esempio n. 44
0
    def upgrade_device(self, ip, hostname, tar_code, reboot="askReboot"):
        # Upgrade single device
        # Status dictionary for post-upgrade reporting
        statusDict = {}
        if Menu.upgrade_list == '':
            statusDict['Upgrade_List'] = 'Juniper-Upgrade_' + Menu.username
        else:
            statusDict['Upgrade_List'] = Menu.upgrade_list
        statusDict['Upgrade_Start'] = ''
        statusDict['Upgrade_Finish'] = ''
        statusDict['IP'] = ip
        statusDict['Connected'] = 'N'
        statusDict['OS_installed'] = 'N'
        statusDict['Rebooted'] = 'N'
        statusDict['IST_Confirm_Loaded'] = ''
        statusDict['IST_Confirm_Rebooted'] = ''
        statusDict['Comments'] = ''

        # Start Logging
        now = datetime.datetime.now()
        date_time = now.strftime("%Y-%m-%d-%H%M")
        install_log = Menu.log_dir + "juniper-install-LOG_" + date_time + "_" + Menu.username + ".log"
        logging.basicConfig(filename=install_log,
                            level=logging.INFO,
                            format='%(asctime)s:%(name)s: %(message)s')
        logging.getLogger().name = ip
        print('Information logged in {0}'.format(install_log))

        # Upgrade Information
        self.do_log("Device: {0} ({1})".format(hostname, ip))
        self.do_log("JunOS: {0}".format(tar_code))

        # Verify package exists before starting upgrade process
        fullpathfile = Menu.image_dir + tar_code
        if os.path.isfile(fullpathfile):
            dev = Device(ip, user=Menu.username, password=Menu.password)
            self.do_log('\n')
            self.do_log(
                '------------------------- Opening connection to: {0} -------------------------\n'
                .format(ip))
            self.do_log('User: {0}'.format(Menu.username))
            # Try to open a connection to the device
            try:
                dev.open()
            # If there is an error when opening the connection, display error and exit upgrade process
            except Exception as err:
                sys.stderr.write('Cannot connect to device {0} : {1}'.format(
                    ip, err))
            # If
            else:
                # Record connection achieved
                statusDict['Connected'] = 'Y'
                # Increase the default RPC timeout to accommodate install operations
                dev.timeout = 600
                # Create an instance of SW
                sw = SW(dev)
                try:
                    # Logging...
                    self.do_log(
                        'Starting the software upgrade process: {0}'.format(
                            tar_code))
                    now = datetime.datetime.now()
                    statusDict['Upgrade_Start'] = now.strftime(
                        "%Y-%m-%d %H:%M")
                    self.do_log('Timestamp: {0}'.format(
                        statusDict['Upgrade_Start']))

                    # Actual Upgrade Function
                    ok = sw.install(package=fullpathfile,
                                    remote_path=Menu.remote_path,
                                    progress=True,
                                    validate=True)
                    # Failed install method...
                    # ok = sw.install(package=fullPathFile, remote_path=Menu.remote_path, progress=self.update_progress, validate=True)
                except Exception as err:
                    msg = 'Unable to install software, {0}'.format(err)
                    self.do_log(msg, level='error')
                else:
                    if ok is True:
                        # Logging...
                        statusDict['OS_installed'] = 'Y'
                        self.do_log('Software installation complete.')
                        now = datetime.datetime.now()
                        statusDict['Upgrade_Finish'] = now.strftime(
                            "%Y-%m-%d %H:%M")
                        self.do_log('Timestamp: {0}'.format(
                            statusDict['Upgrade_Finish']))
                        # Check rebooting status...
                        if reboot == "askReboot":
                            answer = getYNAnswer('Would you like to reboot')
                            if answer == 'y':
                                reboot = "doReboot"
                            else:
                                reboot = "noReboot"
                        if reboot == "doReboot":
                            rsp = sw.reboot()
                            statusDict['Rebooted'] = 'Y'
                            self.do_log(
                                'Upgrade pending reboot cycle, please be patient.'
                            )
                            self.do_log(rsp)
                            # Open a command terminal to monitor device connectivity
                            # os.system("start cmd /c ping -t " + ip)
                        elif reboot == "noReboot":
                            self.do_log(
                                'Reboot NOT performed. System must be rebooted to complete upgrade.'
                            )

                # End the NETCONF session and close the connection
                dev.close()
                self.do_log('\n')
                self.do_log(
                    '------------------------- Closed connection to: {0} -------------------------\n'
                    .format(ip))
        else:
            msg = 'Software package does not exist: {0}. '.format(fullpathfile)
            sys.exit(msg + '\nExiting program')

        return statusDict
cmdline.add_argument("-u",
                     metavar="username",
                     help="Remote username",
                     default=getpass.getuser())
args = cmdline.parse_args()

password = getPass(args.u + "@" +
                   args.target) if 'getPass' in globals() else ""

if (args.a == None and args.d == None):
    print "Nothing to do!"
    sys.exit(1)

dev = Device(host=args.t, user=args.u, port=args.p, password=password)
dev.open()
dev.timeout = 120

with Config(dev, mode="private") as config:
    if args.d != None:
        for p in args.d:
            try:
                config.load("delete policy-options prefix-list %s %s" %
                            (args.l, p),
                            format="set")
            except ConfigLoadError, e:
                if (e.rpc_error['severity'] == 'warning'):
                    print "Warning: %s" % e.message
                else:
                    raise
    if args.a != None:
        for p in args.a:
Esempio n. 46
0
def main():
    hosts = ['XX.YY.ZZ.EE']
    user = '******'

    for host in hosts:
        # https://www.juniper.net/techpubs/en_US/junos-pyez1.0/topics/topic-map/junos-pyez-authenticating-users-with-ssh-keys.html
        dev = Device(host=host,
                     user='******',
                     ssh_private_key_file='/home/bot/.ssh/id_rsa.pub'
                     )  # password=password)
        # Open the connection & config
        try:
            print "Opening connnection to:", host
            dev.open()
        except Exception as err:
            print "Cannot connect to device:", err
            return

        dev.timeout = 300

        yaml_plkanet = '''
---
prefixlist:
    name: pl-kanet
    prefixes:'''

        # Getting information through Table/View
        tblrt = RouteTable(dev)

        tblrt.get(table='RI-PPPoE-INET.inet.0',
                  community_name='community-city')
        ip_list = []
        for route_item in tblrt.keys():
            ip_list.append(IPNetwork(route_item))

        # summarize groups of IP subnets and addresses,
        # merging them together where possible to create the smallest possible list of CIDR subnets
        ip_list = cidr_merge(ip_list)

        for route_item in ip_list:
            yaml_plkanet += "\n    - " + str(route_item)

        pl_config = yaml.load(yaml_plkanet)

        dev.bind(cu=Config)
        # Lock the configuration, load changes, commit
        print "Locking the configuration on:", host
        try:
            dev.cu.lock()
        except LockError:
            print "Error: Unable to lock configuration on:", host
            dev.close()
            return
        print "Loading configuration changes on:", host
        try:
            dev.cu.load('''delete policy-options prefix-list pl-kanet''',
                        format='set')
            dev.cu.load(template=pl_template,
                        template_vars=pl_config,
                        format='text')
        except ValueError as err:
            print err.message
        except Exception as err:
            if err.rsp.find('.//ok') is None:
                rpc_msg = err.rsp.findtext('.//error-message')
                print "Unable to load config changes: ", rpc_msg
            print "Unlocking the configuration on:", host
            try:
                dev.cu.unlock()
            except UnlockError:
                print "Error: Unable to unlock configuration on:", host
            dev.close()
            return
        if dev.cu.diff() is None:
            print "configuration is up-to-date"
        else:
            print "configuration differences:", dev.cu.pdiff()
            print "Committing the configuration on:", host
            try:
                dev.cu.commit()
            except CommitError:
                print "Error: Unable to commit configuration on:", host
                print "Unlocking the configuration on:", host
                try:
                    dev.cu.unlock()
                except UnlockError:
                    print "Error: Unable to unlock configuration on:", host
                    dev.close()
                    return
        print "Unlocking the configuration on:", host
        try:
            dev.cu.unlock()
        except UnlockError:
            print "Error: Unable to unlock configuration on:", host
        print "Closing connection to:", host
        dev.close()
Esempio n. 47
0
def collector(**kwargs):

    for host in kwargs["host_list"]:
        kpi_tags = {}
        latest_datapoints = {}
        #        if ((db_schema == 1) and (not(use_hostname))):
        if (not (use_hostname)):
            latest_datapoints = get_latest_datapoints(host=host)
            logger.info("Latest Datapoints are:")
            logger.info(pformat(latest_datapoints))

        #    kpi_tags = get_host_base_tags(host=host)
        # Check host tag to identify what kind of connections will be used (ej junos / others  / etc)
        if "non_junos_devices" in hosts[host].split():
            pass
            # Reserved for future purposes
        else:  # By default it's a junos device
            # We need to CATCH errors then print then but we need to continue with next host...
            connected = False
            logger.info('Connecting to host: %s', host)
            target_commands = get_target_commands(host)
            timestamp_tracking = {}
            timestamp_tracking['collector_start'] = int(
                datetime.today().strftime('%s'))
            # Establish connection to hosts
            user, passwd = get_credentials(host)
            jdev = Device(user=user,
                          host=host,
                          password=passwd,
                          gather_facts=False,
                          auto_probe=True,
                          port=22)
            for i in range(1, max_connection_retries + 1):
                try:
                    jdev.open()
                    jdev.timeout = default_junos_rpc_timeout
                    connected = True
                    break
                except Exception, e:
                    if i < max_connection_retries:
                        logger.error(
                            '[%s]: Connection failed %s time(s), retrying....',
                            host, i)
                        time.sleep(1)
                        continue
                    else:
                        logging.exception(e)
                        connected = False  # Notify about the specific problem with the host BUT we need to continue with our list
            # First collect all kpi in datapoints {} then at the end we insert them into DB (performance improvement)
            if connected:
                datapoints = []
                # By default execute show version in order to get version and platform as default tags for all kpi related to this host
                kpi_tags = {}
                target_command = 'show version | display xml'
                version_xpath = "//package-information/comment"
                product_model_xpath = "//product-model"
                logger.info('[%s]: Executing command: %s', host,
                            target_command)
                result = execute_command(jdev, target_command)
                if result:
                    logger.debug('[%s]: Parsing command: %s', host,
                                 target_command)
                    xml_data = etree.fromstring(result)
                    value_tmp = xml_data.xpath(version_xpath)[0].text.strip()
                    version = re.search('\[(.*?)\]$', value_tmp)
                    if version:
                        kpi_tags['version'] = version.group(1)
                    else:
                        kpi_tags['version'] = 'unknown'
                    value_tmp = xml_data.xpath(
                        product_model_xpath)[0].text.strip()
                    kpi_tags['product-model'] = convert_variable_type(
                        value_tmp)

                    ## Based on parameter defined in config file

                    if use_hostname:
                        hostname_xpth = "//host-name"
                        hostname_tmp = xml_data.xpath(
                            hostname_xpth)[0].text.strip()
                        hostname = convert_variable_type(hostname_tmp)
                        logger.info(
                            '[%s]: Host will now be referenced as : %s', host,
                            hostname)
                        host = hostname
                        #                        if (db_schema == 1):
                        #                            latest_datapoints = get_latest_datapoints(host=host)
                        #                            logger.info("Latest Datapoints are:")
                        #                            logger.info(pformat(latest_datapoints))
                        latest_datapoints = get_latest_datapoints(host=host)
                        logger.info("Latest Datapoints are:")
                        logger.info(pformat(latest_datapoints))
                    else:
                        logger.info('[%s]: Host will be referenced as : %s',
                                    host, host)

                    kpi_tags['device'] = host
                    kpi_tags['kpi'] = "base-info"
                    match = {}
                    match["variable-name"] = "base-info"
                    # We'll add a dummy kpi in oder to have at least one fixed kpi with version/platform data.
                    get_metadata_and_add_datapoint(
                        datapoints=datapoints,
                        match=match,
                        value_tmp=value_tmp,
                        latest_datapoints=latest_datapoints,
                        host=host,
                        kpi_tags=kpi_tags)

                # Now we have all hosts tags that all host kpis will inherit
                # For each target_command execute it, parse it, and insert values into DB
                timestamp_tracking['collector_cli_start'] = int(
                    datetime.today().strftime('%s'))
                for target_command in target_commands:
                    logger.info('[%s]: Executing command: %s', host,
                                target_command)
                    # Execute rpc/command on host and get result
                    result = execute_command(jdev, target_command)
                    if result:
                        logger.debug('[%s]: Parsing command: %s', host,
                                     target_command)
                        parse_result(host, target_command, result, datapoints,
                                     latest_datapoints, kpi_tags)
                        time.sleep(delay_between_commands)
                jdev.close()
                timestamp_tracking['collector_cli_ends'] = int(
                    datetime.today().strftime('%s'))
                logger.info(
                    '[%s]: timestamp_tracking - CLI collection %s', host,
                    timestamp_tracking['collector_cli_ends'] -
                    timestamp_tracking['collector_cli_start'])

                if datapoints:  # Only insert datapoints if there is any :)
                    insert_datapoints(datapoints)

                timestamp_tracking['collector_ends'] = int(
                    datetime.today().strftime('%s'))
                logger.info(
                    '[%s]: timestamp_tracking - total collection %s', host,
                    timestamp_tracking['collector_ends'] -
                    timestamp_tracking['collector_start'])
            else:
                logger.error('[%s]: Skipping host due connectivity issue',
                             host)
Esempio n. 48
0
                continue
            entry = dict(name=old_entry.findtext('name').strip(),
                         admin-status=old_entry.findtext('admin-status').strip(),
                         oper-status=old_entry.findtext('oper-status').strip(),
                         description=old_entry.findtext('description').strip(),
                         mtu=old_entry.findtext('mtu').strip(),
                         speed=old_entry.findtext('speed').strip())
            table.append(entry)
        return table


    def __init__(self, *args, **kwargs):
        self.hostname = args[0] if len(args) else kwargs.get('host')
        self.user = kwargs.get('user', getenv('USER'))
        self.password = kwargs.get('password')
        self.timeout = kwargs.get('timeout')
        self.vpn = kwargs.get('vpn', 'default')

    def _connect(self):
        """Connect to a device.

        :returns: a connection to a Juniper Networks device.
        :rtype: ``Device``
        """
        if self.password='':
        	dev = Device(self.hostname, user=self.user)
        else:
        	dev = Device(self.hostname, user=self.user, password=self.password)
        dev.open()
        dev.timeout = self.timeout
        return dev
from jnpr.junos.utils.config import Config

##################        Opening the device     ######################################

dev_ip_address = '192.168.124.39'
dev_username = '******'
dev_password = '******'

dev_ssh = Device(host=dev_ip_address,
                 user=dev_username,
                 passwd=dev_password,
                 port='22')

try:
    dev_ssh.open()
    dev_ssh.timeout = 300
except ConnectError as err:
    print("Cannot connect to device: {0}".format(err))
    sys.exit(1)
except Exception as err:
    print(err)
    sys.exit(1)

with Config(dev_ssh, mode='private') as cu:
    cu.load(url="mx104_baseconfig.conf", overwrite=True)
    #cu.pdiff()
    cu.commit()

for i in xrange(40, 0, -1):
    time.sleep(1)