# Path: okconfig/helper_functions.py
# def runCommand(command):
#     """runCommand: Runs command from the shell prompt.
# 
#      Arguments:
#          command: string containing the command line to run
#      Returns:
#          stdout/stderr of the command run
#      Raises:
#          BaseException if returncode > 0
#     """
#     proc = Popen(command, shell=True, stdin=PIPE,stdout=PIPE,stderr=PIPE,)
#     stdout, stderr = proc.communicate('through stdin to stdout')
#     result = proc.returncode,stdout,stderr
#     if proc.returncode > 0:
#         error_string = "* Could not run command (return code= %s)\n" % proc.returncode
#         error_string += "* Error was:\n%s\n" % (stderr.strip())
#         error_string += "* Command was:\n%s\n" % command
#         error_string += "* Output was:\n%s\n" % (stdout.strip())
#         if proc.returncode == 127: # File not found, lets print path
#             path=os.getenv("PATH")
#             error_string += "Check if y/our path is correct: %s" % path
#         raise okconfig.OKConfigError( error_string )
#     else:
#         return result


from builtins import object
from pynag import Model
from .helper_functions import runCommand
import subprocess
import socket
import okconfig
        """returns hostname given a specific ip address"""
        try:
            name= socket.gethostbyaddr(self.ipaddress)
            return name[0]
        except Exception:
            return None
    def is_windows(self):
        """Returns true if machine replies on port 3389"""
        return check_tcp(host=self.ipaddress, port=3389)
    def is_agent_installed(self):
        """Returns True if nrpe client is running"""
        return check_tcp(host=self.ipaddress, port=5666)
    def is_linux(self):
        """Returns true if machine replies on port 22"""
        return check_tcp(host=self.ipaddress, port=22)
    def has_webserver(self):
        """Returns true if machine replies on port 80"""
        return check_tcp(host=self.ipaddress, port=80)
    def has_mssql(self):
        """Returns true if machine replies on port 1433"""
        return check_tcp(host=self.ipaddress, port=1433)
    def has_nrpe(self):
        """Returns true if machine replies on port 5666"""
        return check_tcp(host=self.ipaddress, port=5666)
    def has_ssl(self):
        """Returns true if machine replies on port 443"""
        return check_tcp(host=self.ipaddress, port=443)

    def is_agent_responding(self):
        """returns true if host responds to check_nrpe commands"""

# Path: okconfig/helper_functions.py
# def runCommand(command):
#     """runCommand: Runs command from the shell prompt.
# 
#      Arguments:
#          command: string containing the command line to run
#      Returns:
#          stdout/stderr of the command run
#      Raises:
#          BaseException if returncode > 0
#     """
#     proc = Popen(command, shell=True, stdin=PIPE,stdout=PIPE,stderr=PIPE,)
#     stdout, stderr = proc.communicate('through stdin to stdout')
#     result = proc.returncode,stdout,stderr
#     if proc.returncode > 0:
#         error_string = "* Could not run command (return code= %s)\n" % proc.returncode
#         error_string += "* Error was:\n%s\n" % (stderr.strip())
#         error_string += "* Command was:\n%s\n" % command
#         error_string += "* Output was:\n%s\n" % (stdout.strip())
#         if proc.returncode == 127: # File not found, lets print path
#             path=os.getenv("PATH")
#             error_string += "Check if y/our path is correct: %s" % path
#         raise okconfig.OKConfigError( error_string )
#     else:
#         return result


# Path: okconfig/network_scan.py
from builtins import object
from pynag import Model
from .helper_functions import runCommand
import subprocess
import socket
import okconfig

        """returns hostname given a specific ip address"""
        try:
            name= socket.gethostbyaddr(self.ipaddress)
            return name[0]
        except Exception:
            return None
    def is_windows(self):
        """Returns true if machine replies on port 3389"""
        return check_tcp(host=self.ipaddress, port=3389)
    def is_agent_installed(self):
        """Returns True if nrpe client is running"""
        return check_tcp(host=self.ipaddress, port=5666)
    def is_linux(self):
        """Returns true if machine replies on port 22"""
        return check_tcp(host=self.ipaddress, port=22)
    def has_webserver(self):
        """Returns true if machine replies on port 80"""
        return check_tcp(host=self.ipaddress, port=80)
    def has_mssql(self):
        """Returns true if machine replies on port 1433"""
        return check_tcp(host=self.ipaddress, port=1433)
    def has_nrpe(self):
        """Returns true if machine replies on port 5666"""
        return check_tcp(host=self.ipaddress, port=5666)
    def has_ssl(self):
        """Returns true if machine replies on port 443"""
        return check_tcp(host=self.ipaddress, port=443)

    def is_agent_responding(self):
        """returns true if host responds to check_nrpe commands"""

        returncode, stdout, stderr = runCommand("check_nrpe -H '%s'" % self.ipaddress)