def get_ips(self): """ get ips setted in system local netwoworks which starts with 127.x.x.x or 192.x.x.x will ignore @rtype list @return list of ip's in system """ #print(socket.gethostbyname(socket.gethostname())) try: lines = utils.execute("ip", "address", "|", utils.bash_command("grep"), "[[:space:]]inet[[:space:]]") pattern = r"inet\s(\d{1,3}[^127|^192]\.\d{1,3}\.\d{1,3}\.\d{1,3})" result = [] for line in lines: match = re.match(pattern, line.strip()) if match: result.append(match.group(1)) return result except ScalixExternalCommandException as exception: logger.warning("Could not get ips ", exception) return False
def is_running(self): cmd = [ 'ps', 'aux', '|', utils.bash_command('grep'), "$(echo '{0}' | sed s/^\(.\)/[\\1]/g )".format(self.name) ] lines = utils.execute(cmd) return not lines
def determine_interface(self, ip_str): """ get network interface on which ip setted @param ip string @return string name of interface or None """ try: cmd = ["ip", "addr", "show", "|", utils.bash_command("grep"), "[[:space:]]inet[[:space:]]{0}/".format(ip_str), "|", utils.bash_command("head"), "-1", "|", utils.bash_command("gawk"), "'{ print $NF }'"] lines = utils.execute(cmd) if lines: return lines[0].strip() except ScalixExternalCommandException as exception: logger.warning("Could not determine interface", exception)
def determine_interface(self, ip_str): """ get network interface on which ip setted @param ip string @return string name of interface or None """ try: cmd = [ "ip", "addr", "show", "|", utils.bash_command("grep"), "[[:space:]]inet[[:space:]]{0}/".format(ip_str), "|", utils.bash_command("head"), "-1", "|", utils.bash_command("gawk"), "'{ print $NF }'" ] lines = utils.execute(cmd) if lines: return lines[0].strip() except ScalixExternalCommandException as exception: logger.warning("Could not determine interface", exception)
def run_level(self): """Returns run level on linux @rtype: int @return int if could not determine run level it will return -1 """ try: cmd = ["runlevel", "|", utils.bash_command("gawk"), "'{print $2}'"] return int(utils.execute(cmd)[0]) except ScalixExternalCommandException as exception: logger.critical("Could not get run level", exception) return -1
def listening_port(self, port): """checks if port opened @param port: port number integer @return: on success tulip which contain (Proto, Recv-Q, Send-Q, Local Address, Foreign Address, State) or False """ try: result = utils.execute("netstat", "-ln", "|", utils.bash_command("grep"), ":{0:d}[^0-9]".format(port)) return (i for i in result[0].strip().split()) except ScalixExternalCommandException as exception: logger.warning("Could not get port is listening ", exception) return ()
def partition_size(self, folder): """ get folder size in system(linux only) @rtype int @param folder - string full path to the folder @return size or -1 if something went wrong some """ try: #"df -lP %s | gawk '{print $4}'" result = utils.execute("df", "-BK", "-lP", folder, "|", utils.bash_command("gawk"), "'{print int($4)}'") try: return int(result[1]) * 1024 except (UnicodeEncodeError, ValueError) as exception: logger.critical("Could not get partition size", exception, result) return -1 except ScalixExternalCommandException as exception: logger.critical("Could not get partition size", exception) return -1