def sendArpReply(logger, device, destination, count=3, quiet=False, blocking=True): """This function sends ARP REPLY to a neighbour host Args: logger device - Name of network device where to send ARP REPLY packets destination - destination IP to ping count - stop after sending X ARP REQUEST packets quiet - quiet output blocking - if True, waits for command to complete Return: tuple (rc, stdout, stderr) Raise: None """ args = [Arping.ARPING_COMMAND_NAME, Arping.INTERFACE_OPTION, device, Arping.COUNT_OPTION, str(count), Arping.ARP_REPLY_OPTION] if quiet is True: args.append(Arping.QUIET_OPTION) # must set destination as last arg args.append(destination) rc = Command.execute(logger, Arping.ARPING_COMMAND_NAME, args, blocking=blocking) return rc
def sendRdiscReply(logger, device, destination, count=3, quiet=False, blocking=True): """This function sends IPv6 router discovery to a neighbour host Args: logger device - Name of network device where to send ICMPv6 Router Discovery packets destination - destination IP to ping count - send ICMPv6 Router Discovery X times quiet - quiet output blocking - if True, waits for command to complete Return: tuple (rc, stdout, stderr) Raise: None """ args = [Ndisc.RDISC6_COMMAND_NAME, Ndisc.COUNT_OPTION, str(count)] if quiet is True: args.append(Ndisc.QUIET_OPTION) # must set <destination> <iface> as last args args.append(destination) args.append(device) rc = Command.execute(logger, Ndisc.RDISC6_COMMAND_NAME, args, blocking=blocking) return rc
def __executeIpTablesCmd (self, ipVersion, args, filters=None): commandName = self.IPV4_TABLE_COMMAND_NAME if ipVersion == 6: commandName = self.IPV6_TABLE_COMMAND_NAME args.insert(0,commandName) command = " ".join(args) if filters: for text in filters: command += (" | grep %s" % text) self.__log("iptables-cmd-run").debug4("Run IPv%s Tables Cmd - '%s'", ipVersion, command) # execute command (rc, stdOut, stdErr) = Command.execute(self.__log, "iptables", command) if rc == 0: self.__log("iptables-cmd-stdout").debug3("show output of IPv%s tables command '%s' (rc=%s) - %s", ipVersion, command, rc, stdOut) else: self.__log("iptables-cmd-failed").error("fail to run IPv%s tables command '%s' (rc=%s) - %s", ipVersion, command, rc, stdErr) return (rc,stdOut)
def sendArpRequest(logger, device, destination, count=3, timeout=1, quiet=False, firstReply=False, blocking=True): """This function sends ARP REQUEST to a neighbour host Args: logger device - Name of network device where to send ARP REQUEST packets destination - destination IP to ping count - stop after sending X ARP REQUEST packets timeout - specify a timeout, in seconds, before arping exits quiet - quiet output firstReply - Finish after the first reply confirming that target is alive blocking - if True, waits for command to complete Return: tuple (rc, stdout, stderr) Raise: None """ args = [ Arping.ARPING_COMMAND_NAME, Arping.INTERFACE_OPTION, device, Arping.COUNT_OPTION, str(count), Arping.TIMEOUT_OPTION, str(timeout) ] if quiet is True: args.append(Arping.QUIET_OPTION) if firstReply is True: args.append(Arping.FIRST_REPLY_OPTION) # must set destination as last arg args.append(destination) rc = Command.execute(logger, Arping.ARPING_COMMAND_NAME, args, timeoutSec=(timeout + 3), blocking=blocking) return rc
def sendNdiscRequest(logger, device, destination, count=3, timeout=1, quiet=False, firstReply=False, blocking=True): """This function sends IPv6 neighbor discovery to a neighbour host Args: logger device - Name of network device where to send ARP REQUEST packets destination - destination IP to ping count - send ICMPv6 Neighbor Discovery X times timeout - specify a timeout, in seconds, before ndisc exits quiet - quiet output firstReply - Exit as soon as the first advertisement is received blocking - if True, waits for command to complete Return: tuple (rc, stdout, stderr) Raise: None """ args = [ Ndisc.NDISC6_COMMAND_NAME, Ndisc.COUNT_OPTION, str(count), Ndisc.TIMEOUT_OPTION, str(int(timeout) * 1000) ] # convert to milisec if quiet is True: args.append(Ndisc.QUIET_OPTION) if firstReply is True: args.append(Ndisc.FIRST_REPLY_OPTION) # must set <destination> <iface> as last args args.append(destination) args.append(device) rc = Command.execute(logger, Ndisc.NDISC6_COMMAND_NAME, args, timeoutSec=(timeout + 3), blocking=blocking) return rc
def getChipName(name, logger, deviceid): """This function returns the chip description""" if deviceid.startswith("0x"): deviceid = deviceid[2:] (rc, stdout, stderr) = Command.execute(logger, name, "grep '\< *%s\>' %s" % (deviceid,DeviceUtils.HWADDR_DATA_FILE_NAME)) if rc != 0: logger("chip-name-faild").error("Failed getting '%s' chip name", deviceid) return None chipName = stdout.split(None,1)[1].strip() return chipName
def getVendorName(name, logger, vendorid): """This function returns the vendor name""" if vendorid.startswith("0x"): vendorid = vendorid[2:] (rc, stdout, stderr) = Command.execute(logger, name, "grep -E '^%s' %s" % (vendorid,DeviceUtils.HWADDR_DATA_FILE_NAME)) if rc != 0: logger("vendor-name-faild").error("Failed getting '%s' vendor name", vendorid) return None vendorName = stdout[len(vendorid):].strip() return vendorName
def showEth(logger, device): """This function shows current settings of the specified device Args: logger device - device name Return: tuple (rc, stdout, stderr) Raise: None """ rc = Command.execute(logger, EthTool.ETH_TOOL_COMMAND_NAME, [EthTool.ETH_TOOL_COMMAND_NAME, device]) return rc
def showEthStatistics(logger, device): """This function shows the specified ethernet device for NIC- and driver-specific statistics Args: logger device - device name Return: tuple (rc, stdout, stderr) Raise: None """ rc = Command.execute(logger, EthTool.ETH_TOOL_COMMAND_NAME, [EthTool.ETH_TOOL_COMMAND_NAME, EthTool.STATISTICS_OPTION, device]) return rc
def get_ptp_stat(server): measurement_cmd = "pmc -d %s -u -b 2 'GET CURRENT_DATA_SET'" % (PTP_DOMAIN) command = Command(server=server, wait=True, command=measurement_cmd, control=None, timeout=-1, enable=True) try: output = command.execute(server.ssh) # convert multiline text to list and remove useless command header measurement_list = output.replace("\t", "").split("\n")[2:] return [i for i in measurement_list if i != ""] except Exception as e: print(e) return []
def sendArpReply(logger, device, destination, count=3, quiet=False, blocking=True): """This function sends ARP REPLY to a neighbour host Args: logger device - Name of network device where to send ARP REPLY packets destination - destination IP to ping count - stop after sending X ARP REQUEST packets quiet - quiet output blocking - if True, waits for command to complete Return: tuple (rc, stdout, stderr) Raise: None """ args = [ Arping.ARPING_COMMAND_NAME, Arping.INTERFACE_OPTION, device, Arping.COUNT_OPTION, str(count), Arping.ARP_REPLY_OPTION ] if quiet is True: args.append(Arping.QUIET_OPTION) # must set destination as last arg args.append(destination) rc = Command.execute(logger, Arping.ARPING_COMMAND_NAME, args, blocking=blocking) return rc
def getStatistics(name, logger, device): """This function returns the device statistics""" statsDir = os.path.join(DeviceUtils.INTERFACE_DIR_NAME, device, DeviceUtils.STATS_DIR_NAME) (rc, stdout, stderr) = Command.execute(logger, name, "grep \"\" *", cwd=statsDir) if rc != 0: logger("device-stats-faild").error("Failed getting '%s' statistics", device) return None statsContainer = {} for pair in stdout.split(): key,value = pair.split(":") statsContainer[key] = value return statsContainer
def sendRdiscReply(logger, device, destination, count=3, quiet=False, blocking=True): """This function sends IPv6 router discovery to a neighbour host Args: logger device - Name of network device where to send ICMPv6 Router Discovery packets destination - destination IP to ping count - send ICMPv6 Router Discovery X times quiet - quiet output blocking - if True, waits for command to complete Return: tuple (rc, stdout, stderr) Raise: None """ args = [Ndisc.RDISC6_COMMAND_NAME, Ndisc.COUNT_OPTION, str(count)] if quiet is True: args.append(Ndisc.QUIET_OPTION) # must set <destination> <iface> as last args args.append(destination) args.append(device) rc = Command.execute(logger, Ndisc.RDISC6_COMMAND_NAME, args, blocking=blocking) return rc
def sendArpRequest(logger, device, destination, count=3, timeout=1, quiet=False, firstReply=False, blocking=True): """This function sends ARP REQUEST to a neighbour host Args: logger device - Name of network device where to send ARP REQUEST packets destination - destination IP to ping count - stop after sending X ARP REQUEST packets timeout - specify a timeout, in seconds, before arping exits quiet - quiet output firstReply - Finish after the first reply confirming that target is alive blocking - if True, waits for command to complete Return: tuple (rc, stdout, stderr) Raise: None """ args = [Arping.ARPING_COMMAND_NAME, Arping.INTERFACE_OPTION, device, Arping.COUNT_OPTION, str(count), Arping.TIMEOUT_OPTION, str(timeout)] if quiet is True: args.append(Arping.QUIET_OPTION) if firstReply is True: args.append(Arping.FIRST_REPLY_OPTION) # must set destination as last arg args.append(destination) rc = Command.execute(logger, Arping.ARPING_COMMAND_NAME, args, timeoutSec=(timeout+3), blocking=blocking) return rc
def sendNdiscRequest(logger, device, destination, count=3, timeout=1, quiet=False, firstReply=False, blocking=True): """This function sends IPv6 neighbor discovery to a neighbour host Args: logger device - Name of network device where to send ARP REQUEST packets destination - destination IP to ping count - send ICMPv6 Neighbor Discovery X times timeout - specify a timeout, in seconds, before ndisc exits quiet - quiet output firstReply - Exit as soon as the first advertisement is received blocking - if True, waits for command to complete Return: tuple (rc, stdout, stderr) Raise: None """ args = [Ndisc.NDISC6_COMMAND_NAME, Ndisc.COUNT_OPTION, str(count), Ndisc.TIMEOUT_OPTION, str(int(timeout)*1000)] # convert to milisec if quiet is True: args.append(Ndisc.QUIET_OPTION) if firstReply is True: args.append(Ndisc.FIRST_REPLY_OPTION) # must set <destination> <iface> as last args args.append(destination) args.append(device) rc = Command.execute(logger, Ndisc.NDISC6_COMMAND_NAME, args, timeoutSec=(timeout+3), blocking=blocking) return rc
def setEth(logger, device, speed=None, fullduplex=True, autoneg=True): """This function changes some or all settings of the specified ethernet device Args: logger device - device name speed - Set speed in Mb/s fullduplex - Sets full or half duplex mode autoneg - Specifies whether autonegotiation should be enabled Return: tuple (rc, stdout, stderr) Raise: None """ args = [EthTool.ETH_TOOL_COMMAND_NAME, EthTool.CHANGE_OPTION, device] if speed: args += [EthTool.SPEED_SETTING, speed] args.append(EthTool.DUPLEX_SETTING) if fullduplex: args.append('full') else: args.append('half') args.append(EthTool.AUTO_NEG_SETTING) if autoneg: args.append('on') else: args.append('off') rc = Command.execute(logger, EthTool.ETH_TOOL_COMMAND_NAME, args) return rc
def getPciByIndex(logger, name, vendorid, deviceid, pciIndex): """This function returns the device pci (bus:device.function)""" (rc, stdout, stderr) = Command.execute(logger, name, "/sbin/lspci -n | grep %s:%s" % (vendorid[2:], deviceid[2:])) if rc != 0: logger("device-lspci-faild").error("Failed getting '%s:%s-%s' lspci",vendorid, deviceid, pciIndex) return None # example: # 04:00.0 0200: 8086:10fb (rev 01) # 04:00.1 0200: 8086:10fb (rev 01) # 06:00.0 0200: 8086:10fb (rev 01) output = stdout.splitlines() pci = None if pciIndex < len(output): pci = output[pciIndex].split()[0] else: logger("device-lspci-bad-index").debug1("PCI list contains only %s devices of type '%s:%s' ", len(output), vendorid, deviceid) return pci
def __getEthDevicesByAttr(name, logger, key, value): """This function lists all ethernet devices that matches the given attribute""" deviceDir = os.path.join("*", DeviceUtils.DEVICE_DIR_NAME) (rc, stdout, stderr) = Command.execute(logger, name, "grep \"\" %s" % os.path.join(deviceDir,key), cwd=DeviceUtils.INTERFACE_DIR_NAME) if rc != 0: logger("device-attr-faild").error("Failed getting eths attribute: %s", key) return None output = stdout.splitlines() ethList = [] for line in output: attr = line.split(':')[-1] if attr == value: deviceName = line.split('/')[0] ethList.append(deviceName) return ethList
def ping(logger, destination, source=None, version=4, count=3, interval=1, timeout=1, quiet=False, blocking=True, size=56, pattern=None, mtu=None): """This function shows current settings of the specified device Args: logger destination-destination IP to ping source - name of network device where to send ARP REQUEST packets version - IPv4 or IPv6 as an integer, 4 or 6 count - stop after sending X ECHO_REQUEST packets interval - wait X seconds between sending each packet timeout - time to wait for a response, in seconds quiet - quiet output blocking - if True, waits for command to complete size - specifies the number of data bytes to be sent pattern - specify up to 16 ''pad'' bytes (in hex) to fill out the packet you send mtu - select Path MTU Discovery strategy Return: tuple (rc, stdout, stderr) Raise: None """ pingCommand = Ping.PING_COMMAND_NAME if version == 6: pingCommand = Ping.PING6_COMMAND_NAME args = [ pingCommand, Ping.COUNT_OPTION, str(count), Ping.INTERVAL_OPTION, str(interval), Ping.TIMEOUT_OPTION, str(timeout), Ping.PACKET_SIZE_OPTION, str(size) ] if source: args += [Ping.INTERFACE_OPTION, str(source)] if pattern is not None: args += [Ping.PATTERN_OPTION, str(pattern)] if mtu is not None: args += [Ping.MTU_OPTION, str(mtu)] if quiet is True: args.append(Ping.QUIET_OPTION) # must set destination as last arg args.append(destination) logger("execute-ping").debug1("execute ping command: %s", " ".join(args)) rc = Command.execute(logger, pingCommand, args, timeoutSec=(timeout + 3), blocking=blocking) return rc