示例#1
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 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 = '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)
示例#3
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)
示例#4
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 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 = '''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())
示例#6
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
                    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))
示例#7
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 
                    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)

    int1 = NX.Interface('eth1/2')
    int2 = NX.Interface('eth1/3')

    # ConfigInterfacs object is used to configure multiple
    # interfaces at a time (No need of multiple REST calls)
    # Note: Using Interface object also an interface can be configured
    config = NX.ConfigInterfaces()

    # Adding interfaces to be configured
    config.add_interface(int1)
    config.add_interface(int2)

    # Setting interface attributes
    # Note: if attributes are not set, then default values will be used
    int1.set_admin_status('up')
    int1.set_layer('Layer2')
    int1.set_duplex('auto')
    int1.set_link_log('default')
    int1.set_mode('access')
    int1.set_speed('10G')
    int1.set_access_vlan('vlan-300')
    int1.set_trunk_log('default')
    int1.set_link_log('default')
    int1.set_descr('configured by nxtoolkit')

    # Push entire configuration to the switch
    # Note:To configure only one interface use int1.get_url() & int1.get_json()
    resp = session.push_to_switch(config.get_url(), config.get_json())
    if not resp.ok:
        print('%% Could not push to Switch')
        print resp.text
        sys.exit(0)

    ethpm = NX.Ethpm()
    ethpm.set_default_admin_st('up')
    ethpm.set_default_layer('Layer2')
    ethpm.set_jumbomtu('9216')
    ethpm.set_unsupported_transceiver('yes')

    resp = session.push_to_switch(ethpm.get_url(), ethpm.get_json())
    if not resp.ok:
        print('%% Could not push to Switch')
        print resp.text
        sys.exit(0)

    # Uncomment below lines to get the configured ethpm
    '''   
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 ipv4 on 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)

    #Ensure that  port as layer 3 mode
    int1 = NX.Interface('eth1/2')
    int2 = NX.Interface('eth1/3')

    # ConfigInterfacs object is used to configure multiple
    # interfaces at a time (No need of multiple REST calls)
    # Note: Using Interface object also an interface can be configured
    config = NX.ConfigInterfaces()

    # Adding interfaces to be configured
    config.add_interface(int1)
    config.add_interface(int2)

    # Setting interface attributes
    # Note: if attributes are not set, then default values will be used
    int1.set_admin_status('up')
    int1.set_layer('Layer3')


    # Push entire configuration to the switch
    # Note:To configure only one interface use int1.get_url() & int1.get_json()
    resp = session.push_to_switch(config.get_url(), config.get_json())
    if not resp.ok:
        print ('%% Could not push to Switch')
        print resp.text
        sys.exit(0)



    # Creating interface objects
    # Note: interfaces should be L3 interface
    int1 = NX.Interface('eth1/2')
    config = NX.ConfigInterfaces()



    # Create IPv4 instance
    ipv4 = NX.IP()

    # Enable ip directed broadcast on the interface
    ipv4.enable_directed_broadcast(int1)

    # Add interfaces
    ipv4.add_interface_address(int1, '192.168.60.1/24')

  #  print ipv4.get_url()
  #  print ipv4.get_json()
    resp = session.push_to_switch(ipv4.get_url(), ipv4.get_json())
    if not resp.ok:
        print ('%% Could not push to Switch.')
        print resp.text
        sys.exit(0)

    # Uncomment below to delete the resources
    '''
示例#9
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 
                configure ipv6 on 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)

    # Creating interface objects
    # Note: interfaces should be L3 interface
    int1 = NX.Interface('eth1/20')

    # Create a L3 port channel
    pc1 = NX.PortChannel('211', layer='Layer3')

    # Create the port channel in the switch
    # Note:(port channel should be exist in the switch before
    # assigning IPv6 to it)
    resp = session.push_to_switch(pc1.get_url(), pc1.get_json())
    if not resp.ok:
        print('%% Could create port channel in the Switch')
        print resp.text
        sys.exit(0)

    ipv6 = NX.IP('v6')

    # Add interfaces
    ipv6.add_interface_address(int1, '2004:0DB8::1/10', link_local='FE83::1')

    # Add port channel
    ipv6.add_interface_address(pc1, '2022:0DB8::1/13')

    # Configure IPv6 route and Nexthop information
    r1 = NX.IPRoute('2000:0::0/12', version='v6')
    r1.add_next_hop('234E:44::1', int1, vrf='default', track_id='0', tag='1')
    r1.add_next_hop('234E:44::4', pc1, vrf='default', track_id='1', tag='2')

    # Add route to IPv6
    ipv6.add_route(r1)

    print ipv6.get_url()
    print ipv6.get_json()
    resp = session.push_to_switch(ipv6.get_url(), ipv6.get_json())
    if not resp.ok:
        print('%% Could not push to Switch')
        print resp.text
        sys.exit(0)

    # Uncomment below to delete the resources
    '''
示例#10
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 configure VRRP'''
    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)

    # Object to create multiple VRRP's
    vrrp = NX.ConfigVrrps(session)

    # Create interface object
    int1 = NX.Interface('eth1/12')

    # Make it L3 interface
    int1.set_layer('Layer3')

    # Push interface configuration to the switch
    resp = session.push_to_switch(int1.get_url(), int1.get_json())
    if not resp.ok:
        print('%% Error: Could not push configuration to Switch')
        print(resp.text)

    # Create VRRP object for interface
    vrrp_int1 = NX.Vrrp(int1)

    #create vrrpID
    vrrp_id1 = NX.VrrpID('50')

    # Set the parameter in VrrpID
    vrrp_id1.set_primary('10.10.0.11')
    vrrp_id1.set_secondary('10.10.1.12')

    # Attach vrrpID to vrrp interface
    vrrp_int1.add_vrrp_id(vrrp_id1)

    # Attach the modules to VRRP object
    vrrp.add_vrrp(vrrp_int1)

    # Uncomment the below two lines to print url and json response
    # print vrrp.get_url()
    # print vrrp.get_json()

    # Push entire configuration to the switch
    resp = session.push_to_switch(vrrp.get_url(), vrrp.get_json())
    if not resp.ok:
        print('%% Error: Could not push configuration to Switch')
        print(resp.text)
示例#11
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 
                configure ipv4 on 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)

    # Creating interface objects
    # Note: interfaces should be L3 interface
    int1 = NX.Interface('eth1/20')

    # Create a L3 port channel
    pc1 = NX.PortChannel('211', layer='Layer3')

    # Create the port channel in the switch
    # Note:(port channel should be exist in the switch before
    # assigning IPv6 to it)
    resp = session.push_to_switch(pc1.get_url(), pc1.get_json())
    if not resp.ok:
        print('%% Could create port channel in the Switch')
        print resp.text
        sys.exit(0)

    # Create IPv4 instance
    ipv4 = NX.IP()

    # Enable ip directed broadcast on the interface
    ipv4.enable_directed_broadcast(int1)

    # Add interfaces
    ipv4.add_interface_address(int1, '1.1.1.1/20')

    # Add port channel
    ipv4.add_interface_address(pc1, '3.3.3.211/13')

    # Configure IPv4 route and Nexthop information
    r1 = NX.IPRoute('4.4.4.4/32')
    r1.add_next_hop('5.5.5.5', int1, vrf='default', track_id='0', tag='1')
    r1.add_next_hop('7.7.7.7', pc1, vrf='default', track_id='1', tag='2')

    # Add route to IPv4
    ipv4.add_route(r1)

    print ipv4.get_url()
    print ipv4.get_json()
    resp = session.push_to_switch(ipv4.get_url(), ipv4.get_json())
    if not resp.ok:
        print('%% Could not push to Switch.')
        print resp.text
        sys.exit(0)

    # Uncomment below to delete the resources
    '''
示例#12
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 Configure STP.'''
    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)
    
    stp = NX.STP() # Create STP instance
    stp.set_mode('pvrst')
    
    stp.add_port_type('bpdufilter')
    stp.add_port_type('bpduguard')
    stp.add_port_type('edge')
    stp.add_port_type('network')
    
    mst_etity = NX.StpMst()
    mst_etity.set_simulate('disabled')
    
    vlan = NX.StpVlan('222')
    vlan.set_admin_st('enabled')
    vlan.set_bdg_priority('12288')
    
    int = NX.Interface('eth1/20')
    i_face = NX.StpInterface(int)
    
    i_face.set_mode('network')  # Mode can be network/edge/normal only for l2
                                # interface
    
    stp.add(mst_etity)
    stp.add(vlan)
    stp.add(i_face)
    
    ''' Push STP configuration to the switch '''
    resp = session.push_to_switch(stp.get_url(), stp.get_json())
    if not resp.ok:
        print resp.text
        print ('Could not push to Switch')
        exit(0)

    '''
    # Delete interface STP configuration
    resp = session.delete(i_face.get_url())
    if not resp.ok:
        print('%% Could not delete from Switch')
        sys.exit(0)
    

    # Uncomment below lines to delete all the STP configuration
    resp = session.delete(stp.get_url())
    if not resp.ok:
        print('%% Could not delete from Switch')
        sys.exit(0)
    '''

    # Get the STP details
    data = NX.STP.get(session)
    print "STP Mode:\t", data.mode
    print "STP Port type:\t", data.port_type
    for mst in data.msts:
        print "MSt state:\t", mst.simulate
        print "Hello time:\t", mst.hello_time
        print "Forward time:\t", mst.fwd_delay
        print "Maximum age:\t", mst.max_age
    
    template = "{0:14} {1:14} {2:14} {3:14}"
    print (template.format("Interface", "Mode", "Priority", "Cost"))
    print (template.format("-----------", "-----------", "-----------", 
                           "-----------"))
    for i_face in data.i_faces:
        print (template.format(i_face.id, i_face.mode, i_face.priority, 
                               i_face.cost))
    
    for vlan in data.vlans:
        print "\n"
        print ("Vlan-%s, Admin State: %s, Protocol: %s" % (vlan.id,
                                                           vlan.admin_st,
                                                           vlan.protocol))
        print ("Root:: Address: %s\tPriority: %s\tCost: %s\tPort Number: %s" %
               (vlan.root_addr, vlan.root_priority, vlan.root_cost, 
                vlan.root_port_no))
        print ("Bridge:: Address: %s\tPriority: %s" % (vlan.bdg_addr,
                                                       vlan.bdg_priority))