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
def execute(self): """ Executes the install_nsclient command which installs the nsclient agent for windows machines """ user_env = os.environ if self.merge_env: user_env.update(self.merge_env) if not self.script_args: self.script_args = [] try: self.process = Popen([self.script] + self.script_args, env=user_env, stdout=PIPE, stderr=STDOUT, bufsize=1, shell=False) # Make stdout non blocking fd = self.process.stdout.fileno() flags = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK) except Exception as e: raise okconfig.OKConfigError(e)
def add_defaultservice_to_host(host_name): """ Given a specific hostname, add default service to it """ # Get our host try: my_host = Model.Host.objects.get_by_shortname(host_name) except ValueError: raise okconfig.OKConfigError("Host %s not found." % host_name) # Dont do anything if file already exists service = Model.Service.objects.filter(name=host_name) if len(service) != 0: return False # Make sure that host belongs to a okconfig-compatible group hostgroup_name = my_host['hostgroups'] or "default" hostgroup_name = hostgroup_name.strip('+') if hostgroup_name in okconfig.get_groups(): GROUP = hostgroup_name else: GROUP = 'default' template = default_service_template template = re.sub("HOSTNAME", host_name, template) template = re.sub("GROUP", GROUP, template) fh = open(my_host['filename'], 'a') fh.write(template) fh.close() return True
def traceroute(host="localhost"): """ Returns a list of every host discovered while tracerouting Returns: ["ip1","ip2","ip3",...] """ result = [] returncode,stdout,stderr = runCommand("traceroute -n '%s'" % host) print(stdout) if returncode > 0: raise okconfig.OKConfigError("Failed to traceroute host %s, error: %s" % (host, stderr)) for line in stdout.split("\n"): line = line.split() if len(line) < 2: continue if line[0].isdigit(): result.append(line[1]) return result