Beispiel #1
0
    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
Beispiel #2
0
    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
Beispiel #3
0
 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
Beispiel #4
0
    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)
Beispiel #5
0
    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)
Beispiel #6
0
    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
Beispiel #7
0
    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
Beispiel #8
0
    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 ()
Beispiel #9
0
    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 ()
Beispiel #10
0
    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
Beispiel #11
0
    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