def ping_router(Device, Action, Peer_IP, Count="5"): print(Action) device_data = getdata.get_data() IP_add = device_data['Device_Details'][Device]['ip_add'] Port_no = device_data['Device_Details'][Device]['port'] child = pexpect.spawn('telnet ' + IP_add + ' ' + Port_no) clear_buffer.flushBuffer(5, child) if child: child.sendcontrol('m') child.sendcontrol('m') flag = child.expect(['PC-1>*', 'root*', pexpect.EOF, pexpect.TIMEOUT], timeout=50) if flag in (0, 1): configs = "ping %s -c %s " % (Peer_IP, Count) commands = configs.split('\n') execute.execute(child, commands) resp = child.before.decode('utf-8') child.sendcontrol('m') child.sendcontrol('m') regex = r'bytes from %s' %(Peer_IP) if re.search(regex, resp): return True else: return False else: return False
def connect(self, Device): device_data = getdata.get_data() IP_add = device_data['Device_Details'][Device]['ip_add'] Port_no = device_data['Device_Details'][Device]['port'] Password = device_data['Device_Details'][Device]['pwd'] hostname = device_data['Device_Details'][Device]['Hostname'] child = pexpect.spawn('telnet ' + IP_add + ' ' + Port_no) child.sendcontrol('m') child.sendcontrol('m') clear_buffer.flushBuffer(5, child) child.sendcontrol('m') child.sendcontrol('m') child.sendcontrol('m') flag = child.expect([ 'Router>', 'Router#', hostname + '>', hostname + '#', pexpect.EOF, pexpect.TIMEOUT ], timeout=50) if (flag == 0 or flag == 1 or flag == 2 or flag == 3): self.Login(Device, child) if flag == 4: return False if flag == 5: self.connect(Device) return child
def Login(self, Device, child): child.sendcontrol('m') device_data = getdata.get_data() hostname = device_data['Device_Details'][Device]['Hostname'] Password = device_data['Device_Details'][Device]['pwd'] clear_buffer.flushBuffer(5, child) child.sendcontrol('m') flag = child.expect([ 'Router>', 'Router#', hostname + '>', hostname + '#', pexpect.EOF, pexpect.TIMEOUT ], timeout=50) if flag == 0 or flag == 2: child.send('enable') child.sendcontrol('m') child.send(Password) child.sendcontrol('m') clear_buffer.flushBuffer(5, child) child.sendcontrol('m') child.sendcontrol('m') child.sendcontrol('m') flag1 = child.expect([ hostname + '>', hostname + '#', 'Router#', pexpect.EOF, pexpect.TIMEOUT ], timeout=50) if flag1 == 0: self.Login(Device, child) return
def set_IP_Host(self, Device, Action, Mask): # import sys, pdb; pdb.Pdb(stdout=sys.__stdout__).set_trace() device_data = getdata.get_data() IP_add = device_data['Device_Details'][Device]['ip_add'] Network = device_data['Device_Details'][Device]['network'] Port_no = device_data['Device_Details'][Device]['port'] Gateway = device_data['Device_Details'][Device]['gateway'] child = pexpect.spawn('telnet ' + IP_add + ' ' + Port_no) clear_buffer.flushBuffer(1, child) if child: child.sendcontrol('m') child.sendcontrol('m') flag = child.expect(['PC*', pexpect.EOF, pexpect.TIMEOUT], timeout=50) if flag == 0: if Action == 'configure': configs = "ip %s %s %s""" % (Network, Mask, Gateway) commands = configs.split('\n') execute.execute(child, commands) child.sendcontrol('m') else: unconfig = "clear ip" commands = unconfig.split('\n') execute.execute(child, commands) child.sendline('exit') child.sendcontrol('m') return True else: return False
def route(self, Device, AS_id, Interface): device_data = getdata.get_data() hostname = device_data['Device_Details'][Device]['Hostname'] Dev = Devices.Devices() child = Dev.connect(Device) if (child): clear_buffer.flushBuffer(10, child) child.sendcontrol('m') child.sendcontrol('m') child.sendcontrol('m') flag = child.expect([ hostname + '>', hostname + '#', 'Router\>', 'Router\#', pexpect.EOF, pexpect.TIMEOUT ], timeout=90) #print 'flag =%d' % flag if flag == 0 or flag == 2: Dev.Login(Device, child) configs = """ configure terminal router bgp %d neighbor %s next-hop-self end """ % (AS_id, Interface) commands = configs.split('\n') execute.execute(child, commands) #time.sleep(5) child.sendcontrol('m') # print "BGP synchronization enabled in %s " % (Device) if flag == 1 or flag == 3: configs = """ configure terminal router bgp %d neighbor %s next-hop-self end """ % (AS_id, Interface) commands = configs.split('\n') execute.execute(child, commands) #time.sleep(5) child.sendcontrol('m') # print "BGP synchronization enabled in %s " % (Device) #else: # print 'Expected prompt not found' return True else: return False
def redistribution(self, Device, AS_id, Process_id=None): device_data = getdata.get_data() hostname = device_data['Device_Details'][Device]['Hostname'] Dev = Devices.Devices() child = Dev.connect(Device) if (child): clear_buffer.flushBuffer(10, child) child.sendcontrol('m') child.sendcontrol('m') child.sendcontrol('m') flag = child.expect([ hostname + '>', hostname + '#', 'Router\>', 'Router\#', pexpect.EOF, pexpect.TIMEOUT ], timeout=90) if flag == 0 or flag == 2: Dev.Login(Device, child) configs = """ configure terminal router bgp %d redistribute ospf %d exit router ospf %d redistribute bgp %d subnets end """ % (AS_id, Process_id, Process_id, AS_id) commands = configs.split('\n') execute.execute(child, commands) child.sendcontrol('m') if flag == 1 or flag == 3: configs = """ configure terminal router bgp %d redistribute ospf %d exit router ospf %d redistribute bgp %d subnets end """ % (AS_id, Process_id, Process_id, AS_id) commands = configs.split('\n') execute.execute(child, commands) child.sendcontrol('m') return True else: return False
def checking_operabilty(Device, command): device_data = getdata.get_data() hostname = device_data['Device_Details'][Device]['Hostname'] Dev = Devices.Devices() child = Dev.connect(Device) if child: clear_buffer.flushBuffer(1, child) child.sendcontrol('m') child.sendcontrol('m') child.sendcontrol('m') flag = child.expect([hostname+'>', hostname+'#', 'Router\>', 'Router\#',\ pexpect.EOF, pexpect.TIMEOUT], timeout=90) if flag in (0, 2): Dev.Login(Device, child) configs = """ %s """ % (command) commands = configs.split('\n') execute.execute(child, commands) resp = child.before.decode('utf-8') child.sendcontrol('m') child.sendcontrol('m') regex = r'up' if re.search(regex, resp): return True else: return False if flag in (1, 3): configs = """ %s """ % (command) commands = configs.split('\n') execute.execute(child, commands) resp = child.before.decode('utf-8') child.sendcontrol('m') child.sendcontrol('m') regex = r'up' if re.search(regex, resp): return True else: return False return True else: return False
def Login(self, Device, child): child.sendcontrol('m') device_data = getdata.get_data() hostname = device_data['Device_Details'][Device]['Hostname'] Password = device_data['Device_Details'][Device]['pwd'] clear_buffer.flushBuffer(1, child) if Password != 'zebra': child.sendcontrol('m') flag = child.expect(['Router>', 'Router#', hostname+'>', \ hostname+'#', 'Password*', pexpect.EOF, pexpect.TIMEOUT], timeout=50) if flag in (0, 2): child.send('enable') child.sendcontrol('m') child.send(Password) child.sendcontrol('m') clear_buffer.flushBuffer(1, child) child.sendcontrol('m') child.sendcontrol('m') child.sendcontrol('m') flag1 = child.expect([hostname+'>', hostname+'#', 'Router#', \ pexpect.EOF, pexpect.TIMEOUT], timeout=50) if flag1 == 0: self.Login(Device, child) else: child.send('zebra') child.sendcontrol('m') flag = child.expect(['R*>', pexpect.EOF, pexpect.TIMEOUT], timeout=50) if flag == 0: child.send('enable') child.sendcontrol('m') flag = child.expect(['Password*', pexpect.EOF, pexpect.TIMEOUT], timeout=50) if flag == 0: child.send('zebra') child.sendcontrol('m') child.sendcontrol('m') flag = child.expect(['R*#', pexpect.EOF, pexpect.TIMEOUT], timeout=50) if flag == 0: child.send('show ip route') child.sendcontrol('m') return
def route(self, Device, AS_id, Interface): device_data = getdata.get_data() hostname = device_data['Device_Details'][Device]['Hostname'] Dev = Devices.Devices() child = Dev.connect(Device) if (child): clear_buffer.flushBuffer(10, child) child.sendcontrol('m') child.sendcontrol('m') child.sendcontrol('m') flag = child.expect([ hostname + '>', hostname + '#', 'Router\>', 'Router\#', pexpect.EOF, pexpect.TIMEOUT ], timeout=90) if flag == 0 or flag == 2: Dev.Login(Device, child) configs = """ configure terminal router bgp %d neighbor %s next-hop-self end """ % (AS_id, Interface) commands = configs.split('\n') execute.execute(child, commands) child.sendcontrol('m') if flag == 1 or flag == 3: configs = """ configure terminal router bgp %d neighbor %s next-hop-self end """ % (AS_id, Interface) commands = configs.split('\n') execute.execute(child, commands) child.sendcontrol('m') return True else: return False
def set_loopback(self, Device, Action): device_data = getdata.get_data() hostname = device_data['Device_Details'][Device]['Hostname'] ip_add = device_data['Device_Details'][Device]["ip_add"] child = self.connect(Device) if child != False: clear_buffer.flushBuffer(5, child) child.sendcontrol('m') child.sendcontrol('m') child.sendcontrol('m') child.expect([hostname + '\#', pexpect.EOF, pexpect.TIMEOUT], timeout=60) child.sendcontrol('m') LO_interface_add = device_data['Device_Details'][Device]['lo'] if Action == 'set': configs = """ configure terminal interface %s ip address %s end exit """ % ('loopback0', LO_interface_add) commands = configs.split('\n') execute.execute(child, commands) child.sendcontrol('m') else: unconfig = """ configure terminal interface %s ip address 127.0.0.1 255.255.255.255 end exit """ % ('loopback0') commands = unconfig.split('\n') execute.execute(child, commands) child.sendcontrol('m') child.sendcontrol('m') return True else: return False
def set_IP_DockerHost(self, Device, Action, Mask): # import sys, pdb; pdb.Pdb(stdout=sys.__stdout__).set_trace() device_data = getdata.get_data() IP_add = device_data['Device_Details'][Device]['ip_add'] Network = device_data['Device_Details'][Device]['network'] Port_no = device_data['Device_Details'][Device]['port'] Gateway = device_data['Device_Details'][Device]['gateway'] child = pexpect.spawn('telnet ' + IP_add + ' ' + Port_no) try: clear_buffer.flushBuffer(1, child) except pexpect.exceptions.EOF as exe: print("Unable to reach router prompt") if child: child.sendcontrol('m') child.sendcontrol('m') flag = child.expect(['root*', pexpect.EOF, pexpect.TIMEOUT], timeout=50) if flag == 0: if Action == 'configure': configs = "ifconfig eth0 %s""" % (Network) commands = configs.split('\n') execute.execute(child, commands) child.sendcontrol('m') configs = "route add default gw %s" % (Gateway) commands = configs.split('\n') execute.execute(child, commands) child.sendcontrol('m') else: unconfig = "ifconfig eth0 0" commands = unconfig.split('\n') execute.execute(child, commands) child.sendline('exit') child.sendcontrol('m') return True else: return False
def checking_operabilty(Device, command): device_data = getdata.get_data() hostname = device_data['Device_Details'][Device]['Hostname'] Dev = Devices.Devices() child = Dev.connect(Device) if (child): clear_buffer.flushBuffer(10, child) child.sendcontrol('m') child.sendcontrol('m') child.sendcontrol('m') flag = child.expect([ hostname + '>', hostname + '#', 'Router\>', 'Router\#', pexpect.EOF, pexpect.TIMEOUT ], timeout=90) if flag == 0 or flag == 2: Dev.Login(Device, child) configs = """ %s """ % (command) commands = configs.split('\n') execute.execute(child, commands) child.sendcontrol('m') if flag == 1 or flag == 3: configs = """ %s """ % (command) commands = configs.split('\n') execute.execute(child, commands) child.sendcontrol('m') return True else: return False
def pingvrf_PE(Device, vrf_name, host_ip): device_data = getdata.get_data() IP_add = device_data['Device_Details'][Device]['ip_add'] Port_no = device_data['Device_Details'][Device]['port'] child = pexpect.spawn('telnet ' + IP_add + ' ' + Port_no) clear_buffer.flushBuffer(1, child) if child: child.sendcontrol('m') child.sendcontrol('m') flag = child.expect(['R*#', pexpect.EOF, pexpect.TIMEOUT], timeout=50) if flag == 0: configs = "ping vrf %s %s" % (vrf_name, host_ip) commands = configs.split('\n') execute.execute(child, commands) resp = child.before.decode('utf-8') child.sendcontrol('m') child.sendcontrol('m') regex = r'Success rate is \d+ percent \(\d\/\d\)*' if re.search(regex, resp): return True else: return False else: return False
def connect(self, Device): device_data = getdata.get_data() IP_add = device_data['Device_Details'][Device]['ip_add'] Port_no = device_data['Device_Details'][Device]['port'] Password = device_data['Device_Details'][Device]['pwd'] hostname = device_data['Device_Details'][Device]['Hostname'] child = pexpect.spawn('telnet ' + IP_add + ' ' + Port_no) if not device_data['Device_Details'][Device]['port'] == 'zebra': child.sendcontrol('m') child.sendcontrol('m') try: clear_buffer.flushBuffer(1, child) except pexpect.exceptions.EOF as exc: return False child.sendcontrol('m') child.sendcontrol('m') child.sendcontrol('m') flag = child.expect(['Router>', 'Router#', hostname+'>', hostname+'#', \ 'Password*', pexpect.EOF, pexpect.TIMEOUT], timeout=50) if flag in (0, 1, 2, 3, 4): self.Login(Device, child) if flag == 5: return False if flag == 6: self.connect(Device) return child
def Configure_ospf(self, Device, process_id, Networks_connected, Area, Action): device_data = getdata.get_data() hostname = device_data['Device_Details'][Device]['Hostname'] Dev = Devices.Devices() child = Dev.connect(Device) if (child): clear_buffer.flushBuffer(10, child) child.sendcontrol('m') child.sendcontrol('m') child.sendcontrol('m') flag = child.expect([ hostname + '>', hostname + '#', 'Router\>', 'Router\#', pexpect.EOF, pexpect.TIMEOUT ], timeout=50) if flag == 0 or flag == 2: Dev.Login(self, Device, child) if Action == 'enable': if (isinstance(Networks_connected, list)): for NID in Networks_connected: print NID configs = """ configure terminal router ospf %s network %s area %s exit exit """ % (process_id, NID, Area) commands = configs.split('\n') execute.execute(child, commands) child.sendcontrol('m') child.sendline('exit') child.sendcontrol('m') else: NID = Networks_connected configs = """ configure terminal router ospf %s network %s area %s end """ % (process_id, NID, Area) commands = configs.split('\n') execute.execute(child, commands) child.sendcontrol('m') else: unconfig = """ configure terminal no router ospf %s end """ % (process_id, NID, Area) commands = configs.split('\n') execute.execute(child, commands) child.sendcontrol('m') if flag == 1 or flag == 3: if Action == 'enable': if (isinstance(Networks_connected, list)): for NID in Networks_connected: configs = """ configure terminal router ospf %s network %s area %s exit exit """ % (process_id, NID, Area) commands = configs.split('\n') execute.execute(child, commands) child.sendcontrol('m') child.sendline('exit') child.sendcontrol('m') else: NID = Networks_connected configs = """ configure terminal router ospf %s network %s area %s end """ % (process_id, NID, Area) commands = configs.split('\n') execute.execute(child, commands) child.sendcontrol('m') else: unconfig = """ configure terminal no router ospf %s end """ % (process_id, NID, Area) commands = configs.split('\n') execute.execute(child, commands) child.sendcontrol('m') return True else: return False
def set_IP(self, Device, Links, Action): child = self.connect(Device) if child: device_data = getdata.get_data() hostname = device_data['Device_Details'][Device]['Hostname'] clear_buffer.flushBuffer(1, child) child.sendcontrol('m') flag = child.expect(['R*#', pexpect.EOF, pexpect.TIMEOUT], timeout=50) # if (flag == 0 or flag == 3 or flag == 4): # print("Expected prompt not present") # child.sendcontrol('m') # if (flag == 0 or flag == 2): # self.Login(Device,child) # flag=1 if flag == 0: if Action == 'configure': if isinstance(Links, list): for Lnk in Links: interface = device_data['Link_Details'][Lnk][Device] interface_add = device_data['Device_Details'][Device][interface] configs = """ configure terminal interface %s ip address %s no shutdown exit exit """ % (interface, interface_add) commands = configs.split('\n') execute.execute(child, commands) child.sendcontrol('m') else: interface = device_data['Link_Details'][Lnk][Device] interface_add = device_data['Device_Details'][Device][interface] configs = """ configure terminal interface %s ip address %s no shutdown exit exit """ % (interface, interface_add) commands = configs.split('\n') execute.execute(child, commands) child.sendcontrol('m') child.sendline('exit') child.sendcontrol('m') else: if isinstance(Links, list): for Link in Links: interface = device_data['Link_Details'][Link][Device] interface_add = device_data['Device_Details'][Device][interface] unconfig = """ configure terminal interface %s no ip address %s shutdown exit exit """ % (interface, interface_add) commands = unconfig.split('\n') execute.execute(child, commands) child.sendline('exit') child.sendcontrol('m') else: interface = device_data['Link_Details'][Link][Device] interface_add = device_data['Device_Details'][Device][interface] unconfig = """ configure terminal interface %s no ip address %s shutdown exit exit """ % (interface, interface_add) commands = unconfig.split('\n') execute.execute(child, commands) child.sendcontrol('m') return True else: return False
def checking_operabilty_bgp(Device, command): device_data = getdata.get_data() hostname = device_data['Device_Details'][Device]['Hostname'] port = device_data['Device_Details'][Device]['port'] Dev = Devices.Devices() child = Dev.connect(Device) if port != "zebra": if child: clear_buffer.flushBuffer(1, child) child.sendcontrol('m') child.sendcontrol('m') child.sendcontrol('m') flag = child.expect([hostname+'>', hostname+'#', 'Router\>', \ 'Router\#', pexpect.EOF, pexpect.TIMEOUT], timeout=90) if flag in (0, 2): Dev.Login(Device, child) configs = """ %s """ % (command) commands = configs.split('\n') execute.execute(child, commands) resp = child.before.decode('utf-8') child.sendcontrol('m') child.sendcontrol('m') regex = r'never' if re.search(regex, resp): return False else: return True if flag in (1, 3): configs = """ %s """ % (command) commands = configs.split('\n') execute.execute(child, commands) resp = child.before.decode('utf-8') child.sendcontrol('m') child.sendcontrol('m') regex = r'never' if re.search(regex, resp): return False else: return True elif port == 'zebra': port = "bgpd" device_data = getdata.get_data() IP_add = device_data['Device_Details'][Device]['ip_add'] child = pexpect.spawn('telnet ' + IP_add + ' ' + port) hostname = device_data['Device_Details'][Device]['Hostname'] clear_buffer.flushBuffer(1, child) child.sendcontrol('m') flag = (child.expect(['bgpd*', 'Password*', pexpect.EOF, pexpect.TIMEOUT], timeout=100)) if flag == 1: child.send('zebra') child.sendcontrol('m') flag = child.expect(['bgpd*>', pexpect.EOF, pexpect.TIMEOUT], timeout=50) if flag == 0: child.send('enable') child.sendcontrol('m') if child: flag = child.expect(['bgpd#*', pexpect.EOF, pexpect.TIMEOUT], timeout=90) if flag == 0: Dev.Login(Device, child) configs = """ %s """ % (command) commands = configs.split('\n') execute.execute(child, commands) resp = child.before.decode('utf-8') child.sendcontrol('m') child.sendcontrol('m') regex = r'never' if re.search(regex, resp): return False else: return True else: return False
def Configure_IBGP(self, Device, AS_id, Interface, Action): device_data = getdata.get_data() hostname = device_data['Device_Details'][Device]['Hostname'] Dev = Devices.Devices() child = Dev.connect(Device) if (child): clear_buffer.flushBuffer(10, child) child.sendcontrol('m') child.sendcontrol('m') child.sendcontrol('m') flag = child.expect([ hostname + '>', hostname + '#', 'Router\>', 'Router\#', pexpect.EOF, pexpect.TIMEOUT ], timeout=90) #print 'flag =%d' % flag if flag == 0 or flag == 2: Dev.Login(Device, child) if Action == 'enable': if (isinstance(Interface, list)): for interface in Interface: # print interface configs = """ configure terminal router bgp %d neighbor %s remote-as %d neighbor %s update-source loopback 0 exit exit """ % (AS_id, interface, AS_id, interface) commands = configs.split('\n') execute.execute(child, commands) time.sleep(6) child.sendcontrol('m') # print "IBGP configured in %s " % (Device) # time.sleep(15) child.sendline('exit') child.sendcontrol('m') else: interface = Interface configs = """ configure terminal router bgp %d neighbor %s remote-as %d neighbor %s update-source loopback 0 exit exit """ % (AS_id, interface, AS_id, interface) commands = configs.split('\n') execute.execute(child, commands) # time.sleep(15) child.sendcontrol('m') # print "IBGP configured in %s " % (Device) else: unconfig = """ configure terminal no router bgp %d exit exit """ % (AS_id) commands = configs.split('\n') execute.execute(child, commands) # time.sleep(15) child.sendcontrol('m') # print "IBGP disabled in %s" % Device if flag == 1 or flag == 3: if Action == 'enable': if (isinstance(Interface, list)): for interface in Interface: print interface configs = """ configure terminal router bgp %d neighbor %s remote-as %d neighbor %s update-source loopback 0 exit exit """ % (AS_id, interface, AS_id, interface) commands = configs.split('\n') execute.execute(child, commands) time.sleep(6) child.sendcontrol('m') # print "IBGP configured in %s " % (Device) # time.sleep(15) child.sendline('exit') child.sendcontrol('m') else: interface = Interface configs = """ configure terminal router bgp %d neighbor %s remote-as %d neighbor %s update-source loopback 0 exit exit exit """ % (AS_id, interface, AS_id, interface) commands = configs.split('\n') execute.execute(child, commands) # time.sleep(15) child.sendcontrol('m') # print "IBGP configured in %s " % (Device) else: unconfig = """ configure terminal no router bgp %d exit exit """ % (AS_id) commands = configs.split('\n') execute.execute(child, commands) # time.sleep(15) child.sendcontrol('m') # print "IBGP disable in %s" % Device # else: # print 'Expected prompt not found' return True else: return False