def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application that logs on to the Switch and configure l2bd.''' creds = NX.Credentials('switch', description) args = creds.get() # Login to Switch session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) # Create a ConfigBDs object to configure multiple l2bd at a time bds = NX.ConfigBDs() # Create L2BD objects l2BDs = NX.L2BD('vlan-274') # Attach L2DB instance bds.add_l2bds(l2BDs) # Push the tenant to the Switch resp = session.push_to_switch(bds.get_url(), bds.get_json()) if not resp.ok: print('%% Error: Could not push configuration to Switch') print(resp.text)
def main(): # Login to Switch session = NX.Session("http://" + mgmt_ip, user, password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) bgpSession = NX.BGPSession("65000") bgpDom = NX.BGPDomain("default") bgpDom.set_router_id("192.168.0.1") bgpPeer = NX.BGPPeer("192.168.0.2", bgpDom) bgpPeer.set_remote_as("65000") bgpPeerAf = NX.BGPPeerAF('ipv4-ucast', bgpPeer) bgpPeer.add_af(bgpPeerAf) bgpDom.add_peer(bgpPeer) bgpSession.add_domain(bgpDom) # Push the bgpSession to the switch resp = session.push_to_apic(bgpSession.get_url(bgpSession), bgpSession.get_json()) if not resp.ok: print('%% Could not push configuration to Switch') print(resp.text)
def main(): """ Simple example to output BGP peers in JSON set the following environment variables or manually enter credentials when prompted export NX_URL=https://nexus-ip export NX_LOGIN=admin export NX_LOGIN=password :return: None """ description = 'messing w/ bgp api' creds = nx.Credentials('switch', description) args = creds.get() ''' Login to Switch ''' session = nx.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) bgp = session.get('/api/node/class/bgpPeer.json') ''' GET BGP peers in JSON ''' print json.dumps(bgp.json()['imdata'], indent=2)
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application that logs on to the Switch and configure DNS.''' creds = NX.Credentials('switch', description) args = creds.get() # Login to Switch session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) # Create DNS instance dns = NX.DNS() dns.enable_lookup() prof1 = NX.DnsProfile() dns_provider = NX.DnsProvider('1.1.1.2') prof1.add(dns_provider) dns_domain = NX.DnsDom('name') prof1.add(dns_domain) dns_dmn_ext = NX.DnsDomExt('name1') prof1.add(dns_dmn_ext) dns_host = NX.DnsHost('name2', '1:1::12') prof1.add(dns_host) # Create VRF instance vrf1 = NX.DnsVrf('test_vrf1') vrf2 = NX.DnsVrf('test_vrf2') vrf1.use_in(dns_provider) vrf2.use_in(dns_dmn_ext) # Add VRF to DNS prof1.add(vrf1) prof1.add(vrf2) dns.add_profile(prof1) # Push DNS configuration to the switch resp = session.push_to_switch(dns.get_url(), dns.get_json()) if not resp.ok: print resp.text print('Could not configure DNS') exit(0) # Uncomment below lines to delete the dns configuration '''
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application that logs on to the Switch and get ipv6 details.''' creds = NX.Credentials('switch', description) creds.add_argument( '-v', '--version', type=str, default='v4', help='''IP version (Example: v4, v6), By default its v4''') args = creds.get() # Login to Switch session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) # Get IP infos. from the switch(default version is v4) ipv = NX.IP.get(session, args.version) # Display ip interface details template = "{0:15} {1:15} {2:32}" print( template.format( " Interface ", " Admin status ", " IP%s addresses %s" % (args.version, ('/ Link-local address' if 'v6' in args.version else '')))) print( template.format("-----------", "--------------", "------------------------------------")) for iface in ipv.interfaces: print( template.format(iface.get_if_name(), iface.get_admin_st(), iface.get_address())) # Display ip route details template = "{0:20} {1:15} {2:15} {3:15} {4:15}" for route in ipv.routes: print "\nRoute prefix : %s" % (route.prefix) for n_hop in route.next_hops: print( template.format("\tNext Hop Addr ", " Interface ", " Vrf ", " Tag ", " Track Id")) print( template.format("\t--------------", "--------------", "------", "-----", "---------")) print( template.format("\t" + n_hop.addr, n_hop.i_face, n_hop.vrf, n_hop.tag, n_hop.track_id))
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application that logs on to the Switch and displays all of the Interfaces.''' creds = NX.Credentials('switch', description) args = creds.get() # Login to Switch session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) # Download all of the interfaces # and store the data as tuples in a list l3Inst = NX.L3Inst('ranga') l2BDs = NX.L2BD('vlan-273', l3Inst) # Push the tenant to the Switch resp = session.push_to_switch(l3Inst.get_url(), l3Inst.get_json()) if not resp.ok: print('%% Error: Could not push configuration to Switch') print(resp.text)
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application that logs on to the Switch and Configure Arp.''' creds = NX.Credentials('switch', description) args = creds.get() ''' Login to Switch ''' session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) arp = NX.ARP() # Create ARP instance arp.set_timeout('100') ''' Push ARP configuration to the switch ''' resp = session.push_to_switch(arp.get_url(), arp.get_json()) if not resp.ok: print resp.text print('Could not push to Switch') exit(0) # To remove the configuration do the post request without setting timeout arp_data = NX.ARP.get(session) print "IP ARP Timeout: ", arp_data.timeout
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application that logs on to the Switch and Configure Neighbor Discovery.''' creds = NX.Credentials('switch', description) args = creds.get() ''' Login to Switch ''' session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) nd = NX.ND() # Create ND instance nd_int = NX.NdInterface('vlan123') nd_int.disable_redirect() nd_int.set_ra_interval('600') nd_int.set_prefix('2000::/12', '100', '99') nd.add(nd_int) print nd.get_json() ''' Push ND configuration to the switch ''' resp = session.push_to_switch(nd.get_url(), nd.get_json()) if not resp.ok: print resp.text print('Could not push to Switch') exit(0) # Uncomment below lines to delete nd configuration of specific interface ''' nd_int = NX.NdInterface('vlan123') resp = session.delete(nd_int.get_url()) if not resp.ok: print('%% Could not delete from Switch') sys.exit(0) ''' template = "{0:20} {1:20} {2:20}" print template.format("Interface/Vlan", "Ra Interval", "Redirection State") print template.format("===============", "===============", "===============") nd_data = NX.ND.get(session) for int in nd_data.interfaces: print template.format(int.id, int.ra_interval, int.redirect_st) for prefix in int.prefixes: print("Prefix Address:%s\tlifetime:%s\tpreferred lifetime:%s" % (prefix.address, prefix.lifetime, prefix.pref_lifetime)) print("\n") # Uncomment below lines to get specific interface details '''
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application that logs on to the Switch and create vlan.''' creds = NX.Credentials('switch', description) args = creds.get() # Login to Switch session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) # Create L2BD objects vlan1 = NX.L2BD('vlan-112') vlan2 = NX.L2BD('vlan-223') # Create a ConfigBDs object to configure multiple l2bd at a time bds = NX.ConfigBDs() # Attach L2DB instance or created VLANS bds.add_l2bds(vlan1) bds.add_l2bds(vlan2) # Configures the switch # Note: vlan1.get_json() and vlan1.get_url() methods can be used to # configure a single vlan instead of bds.get_url(), bds.get_json() resp = session.push_to_switch(bds.get_url(), bds.get_json()) if not resp.ok: print resp.text print('Could not create vlans') exit(0) # Create interface objects int1 = NX.Interface('eth1/15') int2 = NX.Interface('eth1/16') # Enable above created vlans on the interfaces int1.set_access_vlan('vlan-111') int2.set_access_vlan('vlan-222') #ConfigInterfaces class is used to configure multiple interfaces at a time config = ConfigInterfaces() config.add_interface(int1) config.add_interface(int2) # Push all interface configuration to the switch resp = session.push_to_switch(config.get_url(), config.get_json()) if not resp.ok: print resp.text print('Could not create port-channel') exit(0)
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application to login into the switch and configure icmp.''' creds = NX.Credentials('switch', description) args = creds.get() # Login to Switch session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) # Create an instance of interface int1 = NX.Interface('eth1/20') int1.set_layer('Layer3') # Push the configuration to the switch to make the interface L3 resp = session.push_to_switch(int1.get_url(), int1.get_json()) if not resp.ok: print('%% Could not push to Switch') print resp.text sys.exit(0) # Create an instance of icmp icmp = NX.ICMP('v4', int1, 'redirect') resp = session.push_to_switch(icmp.get_url(), icmp.get_json()) if not resp.ok: print('%% Could not push to Switch') print resp.text sys.exit(0) # Uncomment below lines to delete Icmp from the given interface ''' resp = session.delete(icmp.get_url()) if not resp.ok: print ('%% Could not delete from Switch') print resp.text sys.exit(0) ''' # To get the redirection state icmps = NX.ICMP.get(session) template = "{0:16} {1:16} {2:16}" print template.format("Interface/Vlan", "Redirect state", "Version") print template.format("---------------", "---------------", "---------------") for icmp in icmps: print template.format(icmp.id, icmp.status, icmp.version)
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application that logs on to the Switch and configure SVI.''' creds = NX.Credentials('switch', description) args = creds.get() # Login to Switch session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) # Create ConfigureInterfaces to do multiple SVI configuration at a time config = NX.ConfigInterfaces() # Create SVI objects providing vlans svi1 = NX.SVI('vlan33') svi2 = NX.SVI('vlan44') # Add svis to the config config.add_svis(svi1) config.add_svis(svi2) # Push entire configuration to the switch # Note: Using svi1.get_url() and svi1.get_json() only one svi # configuration can be pushed to the switch resp = session.push_to_switch(config.get_url(), config.get_json()) if not resp.ok: print('%% Could not login to Switch') print resp.text sys.exit(0) data = [] svis = NX.SVI.get(session) for svi in svis: data.append((svi.id, svi.admin_st, svi.bw, svi.mtu, svi.descr)) # Display the data downloaded (Uncomment below # lines to get the configured SVI) template = "{0:15} {1:15} {2:15} {3:15} {4:40}" print( template.format(" ID ", " ADMIN ", " BANDWIDTH ", " MTU ", " DESCR.")) print( template.format("---------", "-------", "-----------", "------ ", "--------")) for rec in data: print(template.format(*rec)) # Uncomment below lines to delete the created svi '''
def main(): """Simple main method to retrieve hostname.""" session = NX.Session(DEVICE, USERNAME, PASSWORD) session.login() print("This NXOS device has the following hostname:") print( session.get('/api/mo/sys.json').json()['imdata'][0]['topSystem'] ['attributes']['name'])
def main(): """Simple main method to retrieve hostname.""" session = NX.Session(device, username, password) session.login() print("This NXOS device has the following hostname:") print( session.get('/api/mo/sys.json').json()['imdata'][0]['topSystem'] ['attributes']['name'])
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application that logs on to the Switch and configure Syslogs''' creds = NX.Credentials('switch', description) args = creds.get() # Login to Switch session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) # Create Logging object log = NX.Logging() # Create timestamp timestamp = NX.LogTimeStamp(format='milliseconds') # Create logging console console = NX.LogConsole(admin_st='1', severity='2') # Create logging monitor monitor = NX.LogMonitor(admin_st='1', severity='2') # Create source interface logs source_iface = NX.LogSourceInterface(admin_st='1', if_name='lo 2') # Create logging level level = NX.LogLevel(severity='2', facility='local5') # Create server logs server = NX.LogServer(host='10.10.1.12', severity='2', vrf_name='management', fwd_facility='auth') # Attach or set all the log log.add_log(timestamp) log.add_log(console) log.add_log(monitor) log.add_log(source_iface) log.add_log(level) log.add_log(server) # Push entire configuration to switch resp = session.push_to_switch(log.get_url(), log.get_json()) if not resp.ok: print('%% Error: Could not push configuration to Switch') print(resp.text)
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application that logs on to the Switch and displays all of the Interfaces.''' creds = NX.Credentials('switch', description) args = creds.get() # Login to Switch session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) # Download all of the interfaces # and store the data as tuples in a list data = [] interfaces = NX.Interface.get(session) for interface in interfaces: data.append( (interface.attributes['if_name'], interface.attributes['porttype'], interface.attributes['adminstatus'], interface.attributes['operSt'], interface.attributes['speed'], interface.attributes['mtu'], interface.attributes['usage'])) # Display the data downloaded template = "{0:17} {1:6} {2:^6} {3:^6} {4:7} {5:6} {6:9}" print( template.format("INTERFACE", "TYPE", "ADMIN", "OPER", "SPEED", "MTU", "USAGE")) print( template.format("---------", "----", "------", "------", "-----", "---", "---------")) #for rec in data: # print(template.format(*rec)) # For learning purpose, you can uncomment some of these. #print the contents of the data[]; since it is a list , we need to read one at a time #this will print the contents of the data list. if you want to print a particular value inside the tupple # you can reference it like this x[position in tuple] # if you want to know the operation state, you need to look at 3 #for x in data: # print x #Hemant adds #print only the interfaces that are up #data is a list of tupples. The 3 column has the operations state of the interface data_up = [x for x in data if x[3] == "up"] for d in data_up: print(template.format(*d))
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application that logs on to the Switch and displays stats for all of the Interfaces.''' creds = NX.Credentials('switch', description) creds.add_argument( '-i', '--interface', type=str, help='Specify a particular interface module/port e.g. 1/21') creds.add_argument('-f', '--full', action="store_true", help='''Show full statistics - only available if interface is specified''') creds.add_argument( '-n', '--nonzero', action='store_true', help='''Show only interfaces where the counters are not zero. - only available if interface is NOT specified''') args = creds.get() # Login to switch session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) # Download all of the interfaces and get their stats # and display the stats if args.interface: interface = args.interface if 'eth ' in interface: interface = interface[4:] #(module, port) = interface.split('/') #interfaces = NX.Interface.get(session, module, port) interfaces = NX.Interface.get(session, 'eth' + interface) else: interfaces = NX.Interface.get(session) if not args.full or not args.interface: show_stats_short(args, interfaces) else: show_stats_long(args, interfaces)
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application that logs on to the Switch and get ipv6 details.''' creds = NX.Credentials('switch', description) args = creds.get() # Login to Switch session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) # Get ipv6 datas from the switch ipv6 = NX.IPV6.get(session) # Display ipv6 interface details template = "{0:15} {1:15} {2:32}" print( template.format(" Interface ", " Admin status ", " IPv6 addresses / Link-local address")) print( template.format("-----------", "--------------", "------------------------------------")) for iface in ipv6.interfaces: print( template.format(iface.get_if_name(), iface.get_admin_st(), iface.get_address())) # Display ipv6 route details template = "{0:20} {1:15} {2:15} {3:15} {4:15}" for route in ipv6.routes: print "\nRoute prefix : %s" % (route.prefix) for n_hop in route.next_hops: print( template.format("\tNext Hop Addr ", " Interface ", " Vrf ", " Tag ", " Track Id")) print( template.format("\t--------------", "--------------", "------", "-----", "---------")) print( template.format("\t" + n_hop.addr, n_hop.i_face, n_hop.vrf, n_hop.tag, n_hop.track_id))
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application that logs on to the Switch and Configure UDLD.''' creds = NX.Credentials('switch', description) args = creds.get() ''' Login to Switch ''' session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) udld = NX.UDLD() # Create UDLD instance int = NX.Interface('eth1/2') udld.enable_aggress() udld.disable_aggress(int) ''' Push UDLD configuration to the switch ''' resp = session.push_to_switch(udld.get_url(), udld.get_json()) if not resp.ok: print resp.text print('Could not push to Switch') exit(0) # Uncomment below lines to delete UDLD configuration ''' resp = session.delete(udld.get_url()) if not resp.ok: print('%% Could not delete from Switch') sys.exit(0) ''' udld_data = NX.UDLD.get(session) # Pass interface to get specific # interface details print "UDLD global configuration mode aggressive:", udld_data.aggress print "UDLD global message interval:", udld_data.g_msg_int template = "{0:12} {1:12}" print template.format("Interface", "aggress") print template.format("----------", "----------") for (id, aggress) in zip(udld_data.i_faces, udld_data.int_aggresses): print template.format(id, aggress)
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = 'create port channel and attach interface' creds = NX.Credentials('switch', description) args = creds.get() # Login to Switch session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) # ConfigInterfaces instance is required to configure multiple port # channel at a time config = NX.ConfigInterfaces() # Create a POrtChannels object pc1 = NX.PortChannel('444') pc2 = NX.PortChannel('445') int1 = NX.Interface('eth1/5') int2 = NX.Interface('eth1/8') int3 = NX.Interface('eth1/9') # Attach interfaces to the port channel pc1.attach(int1) pc1.attach(int2) pc2.attach(int3) # Add port channels to the config object config.add_port_channel(pc1) config.add_port_channel(pc2) print config.get_json() # Push/ create the port channel object to the switch # Note: To configure only single port channel use pc1.get_url() and # pc1.get_json() instead of config.get_url() and config.get_json() resp = session.push_to_switch(config.get_url(), config.get_json()) if not resp.ok: print('%% Could not push to Switch: %s' % (resp.text)) sys.exit(0)
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application that logs on to the Switch and enable features''' creds = NX.Credentials('switch', description) args = creds.get() # Login to Switch session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) #Create Feature Base object feature = NX.Feature(session) feature.enable('bgp') feature.enable('dhcp') feature.enable('interface-vlan') feature.enable('udld') feature.enable('vrrp') feature.enable('nxapi') feature.enable('tacacsplus') feature.enable('lacp') # Push entire configuration to switch resp = session.push_to_switch(feature.get_url(), feature.get_json()) if not resp.ok: print('%% Error: Could not push configuration to Switch') print(resp.text) template = "{0:20} {1:16} {2:16}" print(template.format("Feature Name", "Instance", "state")) print(template.format("------------", "------------", "------------")) for data in feature.get(): print(template.format(data.name, data.instance, data.admin_st))
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application that logs on to the Switch and displays the DNS details.''' creds = NX.Credentials('switch', description) args = creds.get() # Login to Switch session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) dns = NX.DNS.get(session) print "Dns lookup state:", dns.get_admin_st() for prof in dns.profiles: print "\nDns profile name:", prof.name for provider in prof.providers: print "\tProvider ip:", provider.address for domain in prof.domains: print "\tDomain name:", domain.name for domain_ext in prof.domain_exts: print "\tDomain list name:", domain_ext.name for host in prof.hosts: print "\tHost name:%s\t\tAddress:%s"% (host.name, host.address) for vrf in prof.vrfs: for provider in vrf.providers: print "\tVrf name:%s\tProvider ip:%s"% (vrf.name, provider.address) for domain in vrf.domains: print "\tVrf name:%s\tDomain name:%s"% (vrf.name, domain.name) for domain_ext in vrf.domain_exts: print "\tVrf name:%s\tDomain list name:%s"% (vrf.name, domain_ext.name)
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application that logs on to the Switch and displays all of the vlans.''' creds = NX.Credentials('switch', description) args = creds.get() # Login to Switch session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) # Download all of the interfaces # and store the data as tuples in a list data = [] l2BDs = NX.L2BD.get(session) for l2BD in l2BDs: data.append((l2BD.id, l2BD.bridgeMode, l2BD.adminSt, l2BD.operSt, l2BD.unkMacUcastAct, l2BD.unkMcastAct)) data = sorted(data) # Display the data downloaded template = "{0:5} {1:12} {2:^11} {3:^10} {4:9} {5:8}" print(template.format("ID", "Bridge Mode", "ADMIN STATE", "OPER STATE", "UNK UCAST", "UNK MCAST")) print(template.format("----", "------------", "-----------", "----------", "---------", "--------")) for rec in data: print(template.format(*rec))
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application that logs on to the Switch and configure the Interfaces.''' creds = NX.Credentials('switch', description) args = creds.get() # Login to Switch session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) interface = NX.Interface('eth1/10') # Print original access VLAN print("Starting VLAN:") print(interface.get_access_vlan()) # Setting interface attributes # Note: if attributes are not set, then default values will be used interface.set_mode('access') interface.set_access_vlan('vlan-100') # Push entire configuration to the switch # Note:To configure only one interface use int1.get_url() & int1.get_json() resp = session.push_to_switch(interface.get_url(), interface.get_json()) if not resp.ok: print ('%% Could not push to Switch') print (resp.text) sys.exit(0) # Print original access VLAN print("New VLAN:") print(interface.get_access_vlan())
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application that logs on to the Switch and displays the cdp neighbors''' creds = NX.Credentials('switch', description) args = creds.get() # Login to Switch session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) cdp_detail = NX.LinkNeighbors.get(session) data = [] if not len(cdp_detail): print "NO CDP entry found for given interface." exit() else: for cdp in cdp_detail: data.append((cdp.attributes['devId'], cdp.attributes['id'], cdp.attributes['Hldtme'], cdp.attributes['cap'], cdp.attributes['platId'], cdp.attributes['portId'])) # Display the data downloaded template = "{0:35} {1:13} {2:6} {3:40} {4:20} {5:10} " print(template.format("Device-ID", "Local Iface", "Hldtme", "Capability", "Platform", "Port ID")) print(template.format("---------", "-----------", "------", "----------", "--------", "--------",)) for rec in data: print(template.format(*rec))
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application that logs on to the Switch and displays all of the Interfaces.''' creds = NX.Credentials('switch', description) args = creds.get() # Login to Switch session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) # Download all of the interfaces # and store the data as tuples in a list data = [] interfaces = NX.Interface.get(session) for interface in interfaces: data.append((interface.attributes['if_name'], interface.attributes['porttype'], interface.attributes['adminstatus'], interface.attributes['operSt'], interface.attributes['speed'], interface.attributes['mtu'], interface.attributes['usage'])) # Display the data downloaded template = "{0:17} {1:6} {2:^6} {3:^6} {4:7} {5:6} {6:9}" print(template.format("INTERFACE", "TYPE", "ADMIN", "OPER", "SPEED", "MTU", "USAGE")) print(template.format("---------", "----", "------", "------", "-----", "---", "---------")) for rec in data: print(template.format(*rec))
def main(): """ Main show Process routine :return: None """ description = '''Simple application that logs on to the Switch and displays process information for a switch''' creds = Credentials('switch', description) args = creds.get() session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print '%% Could not login to Switch' sys.exit(0) switch = NX.Node.get(session) processes = NX.Process.get(session, switch) tables = NX.Process.get_table(processes, 'Process list for Switch ::') for table in tables: print table.get_text(tablefmt='fancy_grid') + '\n'
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application that logs on to the Switch and set the boot variable.''' creds = NX.Credentials('switch', description) args = creds.get() # Login to Switch session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) # Create Boot instance boot = NX.BootNxos('n9000-dk9.7.0.3.I2.0.551') # Push boot configuration to the switch resp = session.push_to_switch(boot.get_url(), boot.get_json()) if not resp.ok: print resp.text print ('Could not set the boot variable') exit(0) boot_nxos = boot.get(session) print "Current Boot Variables:" print "Sup1" print "NXOS variable = ", boot_nxos.sup1 print "Boot Variables on next reload:" print "Sup2" print "NXOS variable = ", boot_nxos.sup2 # Uncomment below lines to delete unset the boot variable '''
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = """Simple application that logs on to the Switch and displays Power supply information.""" creds = NX.Credentials('switch', description) args = creds.get() # Login to Switch session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) # Download all of the interfaces # and store the data as tuples in a list data = [] psus = NX.Powersupply.get(session) for psu in psus: data.append( (pwr_sup.slot, pwr_sup.model, pwr_sup.serial, pwr_sup.oper_st, pwr_sup.voltage_source, pwr_sup.fan_status, pwr_sup.hardware_version, pwr_sup.hardware_revision)) # Display the data downloaded template = "{0:5} {1:12} {2:^11} {3:^10} {4:9} {5:6} {6:6}" print( template.format("SLOT", "MODEL", "SERIAL NUM", "OPER STATE", "VOLT SRC", "HW VER", "HW REV")) print( template.format("----", "------------", "-----------", "----------", "---------", "------", "------")) for rec in data: print(template.format(*rec))
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application to login into the switch and configure lacp rate on the interface''' creds = NX.Credentials('switch', description) args = creds.get() # Login to Switch session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) int1 = NX.Interface('eth1/2') lacp = NX.Lacp(rate='fast', interface=int1, session=session) # Push entire configuration to switch resp = session.push_to_switch(lacp.get_url(), lacp.get_json()) if not resp.ok: print('%% Error: Could not push configuration to Switch') print(resp.text) # Uncomment below line to get the configuration of all interfaces from # switch get_data = NX.Lacp.get(session) # Get LACP rate configuration from the switch get_data = NX.Lacp.get(session) template = "{0:16} {1:16}" print(template.format("Interface", "rate")) print(template.format("-------------", "-------------")) for data in get_data: print(template.format(data.interface, data.rate))
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = '''Simple application that logs on to the Switch and fan info.''' creds = NX.Credentials('switch', description) args = creds.get() # Login to Switch session = NX.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to Switch') sys.exit(0) # Download all of the interfaces # and store the data as tuples in a list data = [] fantrays = NX.Fantray.get(session) for fantray in fantrays: data.append( (fantray.slot, fantray.model, fantray.name, fantray.serial, fantray.oper_st, fantray.id, fantray.direction, fantray.speed)) # Display the data downloaded template = "{0:5} {1:12} {2:^11} {3:^10} {4:9} {5:10} {6:5}" print( template.format("SLOT", "MODEL", "SERIAL NUM", "OPER STATE", "ID", "DIRECTION", "SPEED")) print( template.format("----", "------------", "-----------", "----------", "---------", "----------", "-----")) for rec in data: print(template.format(*rec))