def note_mac_address_ip(context, device, index=None): if subprocess.call("ip a s %s |grep -q ether" %device, shell=True) == 0: mac = nmci_step.command_output(context, "ip link show %s | grep 'link/ether' | awk '{print $2}'" % device).strip() if subprocess.call("ip a s %s |grep -q infiniband" %device, shell=True) == 0: ip_out = nmci_step.command_output(context, "ip link show %s | grep 'link/inf' | awk '{print $2}'" % device).strip() mac = ip_out.split()[-1] client_id = "" mac_split = mac.split(":")[-8:] for i in mac_split: if i == mac_split[-1]: client_id+=i else: client_id+=i+":" mac = client_id if index: if not hasattr(context, 'noted'): context.noted = {} context.noted[index] = mac else: if not hasattr(context, 'noted'): context.noted = {} context.noted['noted-value'] = mac print (mac)
def send_com_via_editor(context, commands, connection): coms = commands.split(';') final = "echo -e '" for c in coms: final = final + "%s\n" % c.strip() final = final + "print\nsave\nquit\n' | nmcli c edit %s" % connection nmci_step.command_output(context, final)
def create_policy_based_routing_files(context, profile, dev, table): ips = nmci_step.command_output( context, "nmcli connection sh %s |grep IP4.ADDRESS |awk '{print $2}'" % profile) ip_slash_prefix = ips.split('\n')[0] ip = ip_slash_prefix.split('/')[0] gw = nmci_step.command_output( context, "nmcli connection sh %s |grep IP4.GATEWAY |awk '{print $2}'" % profile).strip() nmci_step.command_code( context, "echo '%s dev %s table %s' > /etc/sysconfig/network-scripts/route-%s" % (ip_slash_prefix, dev, table, profile)) nmci_step.command_code( context, "echo 'default via %s dev %s table %s' >> /etc/sysconfig/network-scripts/route-%s" % (gw, dev, table, profile)) nmci_step.command_code( context, "echo 'prio 17201 iif %s table %s' > /etc/sysconfig/network-scripts/rule-%s" % (dev, table, profile)) nmci_step.command_code( context, "echo 'prio 17200 from %s table %s' >> /etc/sysconfig/network-scripts/rule-%s" % (ip, table, profile)) time.sleep(1)
def is_file(context, filename, seconds=5): for _ in range(int(seconds)): if os.path.isfile(filename): return time.sleep(1) ls = nmci_step.command_output(context, 'ls -la "%s"' % filename) assert os.path.isfile(filename), '"%s" is not a file:\n%s' % (filename, ls)
def add_secondary_addr_same_subnet(context, device): from netaddr import IPNetwork primary_ipn = IPNetwork(nmci_step.command_output(context, "ip -4 a s %s | awk '/inet .*dynamic/ {print $2}'" % device)) if str(primary_ipn.ip).split('.')[2] == str(primary_ipn.ip+1).split('.')[2]: secondary_ip = primary_ipn.ip+1 else: secondary_ip = primary_ipn.ip-1 assert nmci_step.command_code(context, 'ip addr add dev %s %s/%d' % (device, str(secondary_ip), primary_ipn.prefixlen)) == 0
def correct_lifetime(context, typ, valid_lft, pref_lft, device): if typ == 'IPv6': inet = "inet6" if typ == 'IPv4': inet = "inet" valid_cmd = "ip a s '%s' |grep -A 1 -w '%s'| grep -A 1 -w 'scope global' |grep valid_lft |awk '{print $2}'" % (device, inet) pref_cmd = "ip a s '%s' |grep -A 1 -w '%s'| grep -A 1 -w 'scope global' |grep valid_lft |awk '{print $4}'" % (device, inet) valid = nmci_step.command_output(context, valid_cmd).split()[0] pref = nmci_step.command_output(context, pref_cmd).split()[0] valid = valid.strip() valid = valid.replace('sec', '') pref = pref.strip() pref = pref.replace('sec', '') assert int(valid) < int(valid_lft) and int(valid) >= int(valid_lft)-50, "valid: %s, not close to: %s" % (valid, valid_lft) assert int(pref) < int(pref_lft) and int(pref) >= int(pref_lft)-50, "pref: %s, not close to : %s" % (pref, pref_lft)
def check_ipv6_connectivity_on_assumal(context, profile, device): context.nm_restarted = True address = nmci_step.command_output(context, "ip -6 a s %s | grep dynamic | awk '{print $2; exit}' | cut -d '/' -f1" % device) assert nmci_step.command_code(context, 'systemctl stop NetworkManager.service') == 0 assert nmci_step.command_code(context, "sed -i 's/UUID=/#UUID=/' /etc/sysconfig/network-scripts/ifcfg-%s" % profile) == 0 ping = pexpect.spawn('ping6 %s -i 0.2 -c 50' % address, logfile=context.log, encoding='utf-8') time.sleep(1) assert nmci_step.command_code(context, 'systemctl start NetworkManager.service') == 0 time.sleep(12) r = ping.expect(["0% packet loss", pexpect.EOF, pexpect.TIMEOUT]) if r != 0: raise Exception('Had packet loss on pinging the address!')
def check_solicitation(context, dev, file): #file = '/tmp/solicitation.txt' #dev = 'enp0s25' cmd = "ip a s %s |grep ff:ff|awk {'print $2'}" % dev mac = "" for line in nmci_step.command_output(context, cmd).split('\n'): if line.find(':') != -1: mac = line.strip() mac_last_4bits = mac.split(':')[-2] + mac.split(':')[-1] dump = open(file, 'r') assert mac_last_4bits not in dump.readlines( ), "Route solicitation from %s was found in tshark dump" % mac
def global_tem_address_check(context, dev): cmd = "ip a s %s" %dev mac = "" temp_ipv6 = "" ipv6 = "" for line in nmci_step.command_output(context,cmd).split('\n'): if line.find('brd ff:ff:ff:ff:ff:ff') != -1: mac = line.split()[1] if line.find('scope global temporary dynamic') != -1: temp_ipv6 = line.split()[1] if line.find('scope global dynamic') != -1: ipv6 = line.split()[1] assert temp_ipv6 != ipv6, 'IPV6 Address are similar!' temp_ipv6_end = temp_ipv6.split('/')[0].split(':')[-1] mac_end = mac.split(':')[-2]+mac.split(':')[-1] assert temp_ipv6_end != mac_end, 'Mac and tmp Ipv6 are similar in the end %s..%s'
def force_renew_ipv6(context, device): mac = nmci_step.command_output(context, "ip a s %s |grep fe80 |awk '{print $2}'" % device).strip() nmci_step.command_code(context, "ip -6 addr flush dev %s" % (device)) nmci_step.command_code(context, "ip addr add %s dev %s" % (mac, device))
def note_mac_address(context, device): if not hasattr(context, 'noted'): context.noted = {} context.noted['noted-value'] = nmci_step.command_output(context, "ethtool -P %s |grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'" % device).strip()
def modify_connection(context, name, options): out = nmci_step.command_output( context, "nmcli connection modify %s %s" % (name, options)) if 'Error' in out: raise Exception('Got an Error while modifying %s options %s\n%s' % (name, options, out))
def note_the_output_of(context, command): if not hasattr(context, 'noted'): context.noted = {} context.noted['noted-value'] = nmci_step.command_output(context, command).strip()
def note_the_output_as(context, command, index): if not hasattr(context, 'noted'): context.noted = {} context.noted[index] = nmci_step.command_output(context, command+" 2>/dev/null").strip()