def interface_configuration(router_id): """ This function is used to configure our ports. """ hostname, vendor, ports, as_number, ip = db.get_device_by_id(router_id) # Let's engage the user. print "You can configure up to {0} ports.".format(ports) while True: number = raw_input("How many ports do you want to configure now?:") if int(number) > ports: print "You typed an invalid amount of ports. Try again." else: break interface_info = "" i = 0 while i < int(number): interface_type = raw_input("Interface type and number: (e.g. FastEthernet0/0)") while True: try: interface_addr = IPNetwork(raw_input("IP address (x.x.x.x/x format): ")) interface = interface_type + ":" + str(interface_addr.ip) + ":" + str(interface_addr.netmask) + ";" interface_info += interface break except: print "You typed something wrong! Please insert a valid IP address" i += 1 if i == int(number): db.insert_interface(router_id, interface_info) # What vendor is this? if vendor == "Cisco": interface_commands = _cisco_configuration(router_id, hostname) else: interface_commands = _juniper_configuration(router_id, hostname) return interface_commands
def push_configuration(user, password, router_id, command_list): """ This function helps to push configurations inside network devices. """ hostname, vendor, ports, as_number, ip = db.get_device_by_id(router_id) # What vendor is this? if vendor == "Cisco": command_results = _cisco_push(user, password, ip, command_list) else: command_results = _juniper_push(user, password, ip, command_list) if command_results is not 0: # Create a file with the configuration output. filename = hostname + "_configuration.txt" with open(filename, 'a') as f: f.write(command_results) print "Configuration saved!" return 1 else: return 0
def bgp_configuration(router_id): """ This function is used to configure BGP inside our devices. """ hostname, vendor, ports, as_number, ip = db.get_device_by_id(router_id) # Let's engage the user. bgp_info = "" while True: choice = raw_input("Do you want to insert a new BGP peer? (Y/N): ") if choice.upper() == 'Y': while True: peer_as = raw_input("Which AS do you want to peer with?: ") try: if int(peer_as): # Everything is fine. break except: print "You typed an invalid amount of ports. Try again." while True: try: neighbor = IPAddress(raw_input("Type the neighbor ID: ")) bgp_peer = peer_as + ":" + str(neighbor) + ";" bgp_info += bgp_peer break except: print "You typed and invalid neighbor ID. Please insert something in the format x.x.x.x" elif choice.upper() == 'N': break else: print "You typed something wrong. Please type 'Y' or 'N'!" # Populate db. db.insert_neighbor(router_id, bgp_info) # What vendor is this? if vendor == "Cisco": bgp_commands = _cisco_configuration(router_id, as_number, bgp_info) else: bgp_commands = _juniper_configuration(router_id, as_number, bgp_info) return bgp_commands
def interface_configuration(router_id): """ This function is used to configure our ports. """ hostname, vendor, ports, as_number, ip = db.get_device_by_id(router_id) # Let's engage the user. print "You can configure up to {0} ports.".format(ports) while True: number = raw_input("How many ports do you want to configure now?:") if int(number) > ports: print "You typed an invalid amount of ports. Try again." else: break interface_info = "" i = 0 while i < int(number): interface_type = raw_input( "Interface type and number: (e.g. FastEthernet0/0)") while True: try: interface_addr = IPNetwork( raw_input("IP address (x.x.x.x/x format): ")) interface = interface_type + ":" + str( interface_addr.ip) + ":" + str( interface_addr.netmask) + ";" interface_info += interface break except: print "You typed something wrong! Please insert a valid IP address" i += 1 if i == int(number): db.insert_interface(router_id, interface_info) # What vendor is this? if vendor == "Cisco": interface_commands = _cisco_configuration(router_id, hostname) else: interface_commands = _juniper_configuration(router_id, hostname) return interface_commands