go.ligato.io/vpp-agent/v3@v3.5.0/tests/robot/libraries/linux.py (about) 1 import json 2 3 # input - output from 'ip a' command 4 # output - interfaces with parameters in json 5 def Parse_Linux_Interfaces(data): 6 ints = {} 7 for line in data.splitlines(): 8 if line[0] != ' ': 9 if_name = line.split()[1][:-1] 10 ints[if_name] = {} 11 if "mtu" in line: 12 ints[if_name]["mtu"] = line[line.find("mtu"):].split()[1] 13 if "state" in line: 14 ints[if_name]["state"] = line[line.find("state"):].split()[1].lower() 15 else: 16 line = line.strip() 17 if "link/" in line: 18 ints[if_name]["mac"] = line.split()[1] 19 if "inet " in line: 20 ints[if_name]["ipv4"] = line.split()[1] 21 if "inet6" in line and "scope link" not in line: 22 ints[if_name]["ipv6"] = line.split()[1] 23 return ints 24 25 def Pick_Linux_Interface(ints, name): 26 int = [] 27 for key in ints[name]: 28 int.append(key+"="+ints[name][key]) 29 return int 30 31 32 # input - json output from Parse_Linux_Interfaces 33 # output - true if interface exist, false if not 34 def Check_Linux_Interface_Presence(data, mac): 35 present = False 36 for iface in data: 37 if data[iface]["mac"] == mac: 38 present = True 39 return present 40 41 # input - json output from Parse_Linux_Interfaces 42 # output - true if interface exist, false if not 43 def Check_Linux_Interface_IP_Presence(data, mac, ip): 44 present_mac = False 45 present_ip = False 46 for iface in data: 47 if "mac" in data[iface]: 48 if data[iface]["mac"] == mac: 49 present_mac = True 50 if "ipv4" in data[iface]: 51 if data[iface]["ipv4"] == ip: 52 present_ip = True 53 if "ipv6" in data[iface]: 54 if data[iface]["ipv6"] == ip: 55 present_ip = True 56 if present_mac == True and present_ip == True: 57 return True 58 else: 59 return False 60 61 62 def parse_linux_arp_entries(data): 63 """Parse output of arp command and return list of ARP entries. 64 65 :param data: output of 'arp' command. 66 :type data: str 67 :returns: Parsed ARP entries. 68 :rtype: list of dict 69 """ 70 entries = [] 71 72 for line in data.splitlines(): 73 if "ip address" in line.lower(): 74 # skip column headers line 75 continue 76 entry_data = line.split() 77 entry = { 78 "interface": entry_data[5], 79 "ip_addr": entry_data[0], 80 "mac_addr": entry_data[3] 81 } 82 entries.append(entry) 83 84 return entries 85 86 87 def parse_linux_ipv6_neighbor_entries(data): 88 """Parse output of ip neighbor command and return list of neighbor entries. 89 90 :param data: output of 'ip neighbor' command. 91 :type data: str 92 :returns: Parsed neighbor entries. 93 :rtype: list of dict 94 """ 95 entries = [] 96 97 for line in data.splitlines(): 98 if "ip address" in line.lower(): 99 # skip column headers line 100 continue 101 entry_data = line.split() 102 entry = { 103 "interface": entry_data[2], 104 "ip_addr": entry_data[0], 105 "mac_addr": entry_data[4] 106 } 107 entries.append(entry) 108 109 return entries 110