# -*- coding: utf-8 -*- """Module allowing to command and control the plug SPC300""" import ssh import spc300 import re import plug interfaces = {} def reset_interfaces(): """Reset all interfaces""" global interfaces interfaces = {} def get_interface(host): """Determine on a host the ethernet interface where to send an MME""" global interfaces try: return interfaces[host] except: eth_netmask = spc300.config["eth_netmask"] stdoutdata = check_output(host, "route -n") eth_interface = extract_eth_interface(eth_netmask, stdoutdata) stdoutdata = check_output(host, "sudo hpav_test list_if") interface = extract_interface(eth_interface, stdoutdata) interfaces.update({host:interface}) return interfaces[host] def send_spid_vs_get_ce_stats_request(host, rx_key, tx_key): """Send mme spid_vs_get_ce_stats and get response""" assert plug.get_module(rx_key) == spc300 STA_MAC = plug.get_mac_address(rx_key) PEER_MAC = plug.get_mac_address(tx_key) interface = get_interface(host) command = "sudo hpav_test test_mme spid_vs_get_ce_stats_req " + \ interface + " " + STA_MAC + " " + PEER_MAC return check_output(host, command) def check_output(host, command): (user, non_plc_ip_address) = host return ssh.check_output(user, non_plc_ip_address, command) def extract_eth_interface(eth_netmask, stdoutdata): pattern = eth_netmask + ".* (eth\d+)( )*" return re.search(pattern, stdoutdata).groups()[0] def extract_interface(eth_int, stdoutdata): pattern = "Interface (\d+) : " + eth_int return re.search(pattern, stdoutdata).groups()[0] if __name__ == "__main__": assert "eth1" == extract_eth_interface( "169.254.0.", "172.27.92.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0\n" "169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 eth1\n" "0.0.0.0 172.27.92.254 0.0.0.0 UG 0 0 0 eth0") assert "1" == extract_interface( "eth2", "Interface 0 : eth0 (MAC : 00:21:9b:33:03:1d)\n" "Interface 1 : eth2 (MAC : 00:0d:88:68:3b:71)\n" "Interface 2 : eth3 (MAC : 00:0d:88:68:3b:72)\n" "Interface 3 : eth4 (MAC : 00:0d:88:68:3b:73)\n" "Interface 4 : eth5 (MAC : 00:0d:88:68:3b:70)\n" "Interface 5 : eth6 (MAC : 94:0c:6d:80:6a:ec)")