def connect(self, application_name, **kwargs): network_application = NetworkApplication.get_instance() network_application.name = application_name self.network_element = network_application.get_network_element( self.element_hostname) if self.network_element is None: self.logger.error("Failed to get network element") sys.exit(1) self.logger.info("We have a NetworkElement : " + self.network_element.__str__()) session_config = SessionConfig( SessionConfig.SessionTransportMode.TLS) #default is TLS session_config.eventQueueSize = self.properties["eventQueueSize"] session_config.eventThreadPool = self.properties["eventThreadPool"] session_config.eventDropMode = self.properties["eventDropMode"] session_config.keepAliveIdleTime = self.properties["keepAliveIdleTime"] session_config._keepAliveInterval = self.properties[ "keepAliveInterval"] session_config.keepAliveRetryCount = self.properties[ "keepAliveRetryCount"] session_config.reconnectTimer = self.properties["reconnectTimer"] session_config.ca_certs = self.root_cert_path self.session_handle = self.network_element.connect( self.username, self.password, session_config) if self.session_handle is None: logging.error("Failed to connect to the network element") return False logging.info("successful in connecting to the network element") return True
def connect(self, applicationName): """ Obtains a NetworkApplication instance, sets the name to applicationName, gets a network element for the hostname or address in the command line arguments or tutorial.properties file and then tries to connect to the Network Element with the username and password supplied, or from the tutorial.properties file. @param applicationName: The NetworkApplication name is set to this value. @return True if the connection succeeded without exception, else false. @throws OnepException """ # START SNIPPET: init_myapp network_application = NetworkApplication.get_instance() # END SNIPPET: init_myapp # START SNIPPET: name_myapp network_application.name = applicationName # END SNIPPET: name_myapp # START SNIPPET: get_ne_opt1 self.network_element = network_application.get_network_element( self.element_hostname) # END SNIPPET: get_ne_opt1 if self.network_element == None: logger.error("Failed to get network element") sys.exit(1) logger.info("We have a NetworkElement : " + self.network_element.__str__()) # START SNIPPET: connect session_config = SessionConfig( SessionConfig.SessionTransportMode.TLS) #default is TLS if self.transport.lower() == "tipc" or self.transport == "2": session_config = SessionConfig( SessionConfig.SessionTransportMode.TIPC) #Set all the TLS properties in session_config session_config.ca_certs = self.root_cert_path session_config.keyfile = self.client_key_path session_config.certfile = self.client_cert_path session_config.set_tls_pinning(self.tls_pinning_file, PinningHandler(self.tls_pinning_file)) self.session_handle = self.network_element.connect( self.username, self.password, session_config) # END SNIPPET: connect if self.session_handle == None: # START SNIPPET: loggerError logger.error("Failed to connect to NetworkElement - ") # END SNIPPET: loggerError return False logger.info("Successful connection to NetworkElement - ") return True
def establish_session(self): ''' Take a onepk network device and go through the steps to establish a session. This method assumes that you are using a PIN_FILE Sets the following attributes self.network_application self.net_element self.session_config self.session_handle ''' # Create NetworkApplication instance self.network_application = NetworkApplication.get_instance() # Create NetworkElement instance self.net_element = self.network_application.get_network_element(self.ip) # Create SessionConfig instance session_config = SessionConfig(SessionConfig.SessionTransportMode.TLS) session_config.ca_certs = None session_config.keyfile = None session_config.certfile = None session_config.port = self.port # Use existing pin_file (previously saved) so that remote certificate is accepted. session_config.set_tls_pinning(self.pin_file, None) self.session_config = session_config # Create a session handle object self.session_handle = self.net_element.connect(self.username, self.password, self.session_config)
def sampleapp(): appname = raw_input('Enter name of application : ') session_config = SessionConfig(SessionConfig.SessionTransportMode.TLS) #default is TLS if transport.lower() == "tipc" or transport == 2: session_config = SessionConfig(SessionConfig.SessionTransportMode.TIPC) session_config.ca_certs = root_cert_path session_config.certfile = client_cert_path session_config.keyfile = client_key_path ne = NetworkElement(element_address, appname) con = ne.connect(username, password, session_config) print 'Connected to host' print "System Name: ", ne.properties.sys_name print "System Uptime: ", ne.properties.sys_uptime print "Total System Memory: ", ne.total_system_memory print "Free System Memory: ", ne.free_system_memory print "System CPU Utilization: ", ne.system_cpu_utilization, "%\n" print "System Connect Time: ", ne.get_connect_time() print "System Disconnect Time: ", ne.get_disconnect_time() print "System __str__ Method: ", ne print "Host Content String:\n", ne.properties.content_string ne.disconnect()
def _default_connect(self): self.ios_ip_addr = os.getenv('ONEP_UT_ADDR', '127.0.0.1') self.onep_user = os.getenv('ONEP_USER', '') self.onep_pwd = os.getenv('ONEP_PWD', '') self.onep_cert = os.getenv('ONEP_CERTIFICATE', '~/ca.pem') self.onep_transport = os.getenv('ONEP_TRANSPORT', SessionConfig.SessionTransportMode.TIPC) self.sess = SessionConfig(self.onep_transport) self.sess.ca_certs = self.onep_cert self.ne = NetworkElement(self.ios_ip_addr, 'dohost') self.con = self.ne.connect(self.onep_user, self.onep_pwd, self.sess) self.vty = VtyHelper(self.ne)
def connect(self): ''' establish connection via onepk to network device ''' self.network_application = NetworkApplication.get_instance() self.net_element = self.network_application.get_network_element(self.ipaddr) # create SessionConfig session_config = SessionConfig(SessionConfig.SessionTransportMode.TLS) session_config.ca_certs = None session_config.keyfile = None session_config.certfile = None session_config.port = self.port session_config.set_tls_pinning(self.pin_file, None) self.session_config = session_config self.session_handle = self.net_element.connect(self.username, self.password, self.session_config)
def establish_session(self): ''' Take a onepk network device and go through the steps to establish a session. This method assumes that you are using a PIN_FILE Sets the following attributes self.network_application self.net_element self.session_config self.session_handle ''' # Create NetworkApplication instance self.network_application = NetworkApplication.get_instance() # Create NetworkElement instance self.net_element = self.network_application.get_network_element( self.ip) # Create SessionConfig instance session_config = SessionConfig(SessionConfig.SessionTransportMode.TLS) session_config.ca_certs = None session_config.keyfile = None session_config.certfile = None session_config.port = self.port # Use existing pin_file (previously saved) so that remote certificate is accepted. session_config.set_tls_pinning(self.pin_file, None) self.session_config = session_config # Create a session handle object self.session_handle = self.net_element.connect(self.username, self.password, self.session_config)
def config(self): session_config = SessionConfig(SessionConfig.SessionTransportMode.TLS) session_config.ca_certs = self.ca_path return session_config
def createSessionConfig(self, mode): """ Creates an instance of SessionConfig with the given transport mode and sets the reconnect timer to one minute. All other attributes are set to their default values. When connecting to a network element, the caller may optionally provide a SessionConfig that contains the desired configuration for the resulting session. When creating the SessionConfig, the only required attribute is the transport mode. TLS is the transport mode used for the end node hosting model. TIPC (sometimes referred to as LOCAL) may be used in process and blade hosting models. All other attributes are optional, and will take on their default values if not explicitly set. To demonstrate reconnecting to the session, the reconnect timer will be set to one minute. @param mode The transport mode used by the connection. @return a SessionConfig instance. """ # START SNIPPET: create_session_config # Construct a SessionConfig instance with the given transport mode. config = SessionConfig(mode) # Set the reconnect timer to one minute. config.reconnectTimer = 60 # The session attributes below this point are set to their default # values. # # Set the port to connect to on the network element. # TLS 15002 # TIPC N/A # if mode.lower() == "tls": config.port = config.DEFAULT_PORT config.transportMode = SessionConfig.SessionTransportMode.TLS config.ca_certs = tutorial.root_cert_path config.keyfile = tutorial.client_key_path config.certfile = tutorial.client_cert_path else: # Not required for TIPC. pass # Set the event queue size of the session. config.eventQueueSize = config.DEFAULT_EVENT_QUEUE_SIZE # Set the event thread pool size of the session. config.eventThreadPool = config.DEFAULT_THREADPOOL_SIZE # Set the event drop mode of the session. config.eventDropMode = config.DEFAULT_EVENT_DROP_MODE # Set the keepalive attributes of the session. # Idle time in seconds config.keepAliveIdleTime = config.DEFAULT_KEEPALIVE_IDLE_TIME # Interval between keepalives in seconds config.keepAliveInterval = config.DEFAULT_KEEPALIVE_INTERVAL # Number of keepalives config.keepAliveRetryCount = config.DEFAULT_KEEPALIVE_RETRY_COUNT # END SNIPPET: create_session_config config.set_tls_pinning(tutorial.tls_pinning_file, PinningHandler(tutorial.tls_pinning_file)) return config
from onep.element import SessionConfig from onep.vty import VtyService class IPAttributes: ttl = defaultTTL insertedAt = 0 resolvedIPs={} networkApplication=NetworkApplication.get_instance() networkApplication.name = "DNSACL" ne = networkApplication.get_network_element(RouterIP) session_config = SessionConfig(SessionConfig.SessionTransportMode.TLS) session_config.ca_certs = CertDirectory+CACertFile session_config.keyfile = CertDirectory+ClientKeyFile session_config.certfile = CertDirectory+ClientCertFile while True: currentTime=time.time() #update the list with new IPs or new TTLs for host in hostList: try: answer = dns.resolver.query(host, 'A') except dns.resolver.NXDOMAIN: print("WARNING: Non existing hostname: "+host)