def __run(self, cmd): cmd = sub_process.Popen(cmd.split(), stdout=sub_process.PIPE, stderr=sub_process.PIPE, shell=False, close_fds=True) return [line for line in cmd.communicate()[0].strip().split('\n')]
def info(self, flags="-auxh"): """ Returns a struct of hardware information. By default, this pulls down all of the devices. If you don't care about them, set with_devices to False. """ flags.replace(";", "") # prevent stupidity cmd = sub_process.Popen(["/bin/ps", flags], executable="/bin/ps", stdout=sub_process.PIPE, stderr=sub_process.PIPE, shell=False) data, error = cmd.communicate() # We can get warnings for odd formatting. warnings != errors. if error and error[:7] != "Warning": raise codes.FuncException(error.split('\n')[0]) results = [] for x in data.split("\n"): tokens = x.split() results.append(tokens) return results
def get_running(self): """ Get a list of which services are running, stopped, or disabled. """ results = [] # Get services services = self.get_enabled() init_return_codes = { 0: 'running', 1: 'dead', 2:'locked', 3:'stopped' } for service in services: filename = os.path.join("/etc/init.d/",service[0]) # Run status for service try: init_exec = sub_process.Popen([filename, "status"], stdout=sub_process.PIPE, close_fds=True, env={ "LANG": "C" }) except Exception, e: raise codes.FuncException("Service status error %s on initscript %s" % (e, filename)) # Get status output data = init_exec.communicate()[0] # Wait for command to complete init_exec.wait() # Append the result, service name, return status and status output results.append((service[0], init_return_codes[init_exec.returncode], data))
def hal_info(self): """ Returns the output of lshal, but split up into seperate devices for easier parsing. Each device is a entry in the return hash. """ cmd = sub_process.Popen(["/usr/bin/lshal"], shell=False, stdout=sub_process.PIPE) data = cmd.communicate()[0] data = data.split("\n") results = {} current = "" label = data[0] for d in data: if d == '': results[label] = current current = "" label = "" else: if label == "": label = d current = current + d return results
def run(self, command): """ Runs a command, returning the return code, stdout, and stderr as a tuple. NOT FOR USE WITH INTERACTIVE COMMANDS. """ cmdref = sub_process.Popen(command.split(), stdout=sub_process.PIPE, stderr=sub_process.PIPE, shell=False) data = cmdref.communicate() return (cmdref.returncode, data[0], data[1])
def umount(self, dir, killall=False, force=False, lazy=False): # succeed if its not mounted if not os.path.ismount(dir): return True if killall: cmd = sub_process.Popen(["/sbin/fuser", "-mk", dir], executable="/sbin/fuser", stdout=sub_process.PIPE, shell=False) cmd.wait() cmdline = ["/bin/umount"] if force: cmdline.append("-f") if lazy: cmdline.append("-l") cmdline.append(dir) cmd = sub_process.Popen(cmdline, executable="/bin/umount", stdout=sub_process.PIPE, shell=False) if cmd.wait() == 0: return True else: return False
def get_running(self): """ Get a list of which services are running, stopped, or disabled. """ chkconfig = sub_process.Popen(["/sbin/service", "--status-all"], stdout=sub_process.PIPE) data = chkconfig.communicate()[0] results = [] for line in data.split("\n"): if line.find(" is ") != -1: tokens = line.split() results.append((tokens[0], tokens[-1].replace("...", ""))) return results
def get(self, oid, rocommunity, hostname='localhost'): """ Runs an snmpget on a specific oid returns the output of the call. """ command = '%s -c %s %s %s' % (base_snmp_command, rocommunity, hostname, oid) cmdref = sub_process.Popen(command.split(), stdout=sub_process.PIPE, stderr=sub_process.PIPE, shell=False) data = cmdref.communicate() return (cmdref.returncode, data[0], data[1])
def run(self, check_command): """ Runs a nagios check returning the return code, stdout, and stderr as a tuple. """ nagios_path = '/usr/lib/nagios/plugins' command = '%s/%s' % (nagios_path, check_command) cmdref = sub_process.Popen(command.split(), stdout=sub_process.PIPE, stderr=sub_process.PIPE, shell=False) data = cmdref.communicate() return (cmdref.returncode, data[0], data[1])
def run_iptables(args): cmd = sub_process.Popen(["/sbin/iptables"] + args.split(), executable="/sbin/iptables", stdout=sub_process.PIPE, stderr=sub_process.PIPE, shell=False) data, error = cmd.communicate() results = [] for line in data.split("\n"): tokens = line.split() results.append(tokens) return results
def __init__(self): cmd = sub_process.Popen("uname -r", shell=True, stdout=sub_process.PIPE, close_fds=True) output = cmd.communicate()[0] if output.find("xen") != -1: conn = libvirt.open(None) else: conn = libvirt.open("qemu:///system") if not conn: raise codes.FuncException("hypervisor connection failure") self.conn = conn
def mount(self, device, dir, type="auto", options=None, createdir=False): cmdline = ["/bin/mount", "-t", type] if options: cmdline.append("-o") cmdline.append(options) cmdline.append(device) cmdline.append(dir) if createdir: try: os.makedirs(dir) except: return False cmd = sub_process.Popen(cmdline, executable="/bin/mount", stdout=sub_process.PIPE, shell=False) if cmd.wait() == 0: return True else: return False
def list(self): cmd = sub_process.Popen(["/bin/cat", "/proc/mounts"], executable="/bin/cat", stdout=sub_process.PIPE, shell=False) data = cmd.communicate()[0] mounts = [] lines = [l for l in data.split("\n") if l] #why must you append blank crap? for line in lines: curmount = {} tokens = line.split() curmount['device'] = tokens[0] curmount['dir'] = tokens[1] curmount['type'] = tokens[2] curmount['options'] = tokens[3] mounts.append(curmount) return mounts
def ssh(host, cmdargs, input=None, user=SSH_USER): cmdline = [SSH, SSH_OPTS, "%s@%s" % (user, host)] cmdline.extend(cmdargs) cmd = sub_process.Popen(cmdline, executable=SSH, stdin=sub_process.PIPE, stdout=sub_process.PIPE, stderr=sub_process.PIPE, shell=False) (out, err) = cmd.communicate(input) if cmd.wait() != 0: raise GenericSSHError, err else: return out + err
def info(self,flags="-q onecheck"): """ Returns a struct of hardware information. By default, this pulls down all of the devices. If you don't care about them, set with_devices to False. """ flags.replace(";","") # prevent stupidity cmd = sub_process.Popen("/usr/sbin/smartd %s" % flags,stdout=sub_process.PIPE,shell=True,close_fds=True) data = cmd.communicate()[0] results = [] for x in data.split("\n"): results.append(x) return (cmd.returncode, results)
def get_enabled(self): """ Get the list of services that are enabled at the various runlevels. Xinetd services only provide whether or not they are running, not specific runlevel info. """ chkconfig = sub_process.Popen(["/sbin/chkconfig", "--list"], stdout=sub_process.PIPE, close_fds=True, env={ "LANG": "C" }) data = chkconfig.communicate()[0] results = [] for line in data.split("\n"): if line.find("0:") != -1: # regular services tokens = line.split() results.append((tokens[0],tokens[1:])) elif line.find(":") != -1 and not line.endswith(":"): # xinetd.d based services tokens = line.split() tokens[0] = tokens[0].replace(":","") results.append((tokens[0],tokens[1])) return results
def manage(self, project_path, command): """ Executes django-admin.py adding the path and settings variables for a command. project_path is the path to the Django project. command is the command to run. """ self._verify_path(project_path) settings = self._get_settings(project_path) command = "django-admin.py %s --pythonpath=%s --settings=settings" % ( command, project_path) cmdref = sub_process.Popen(command.split(), stdout=sub_process.PIPE, stderr=sub_process.PIPE, shell=False, close_fds=True) data = cmdref.communicate() return (cmdref.returncode, data[0], data[1])
def __run_command(self, command, opts=[]): full_cmd = [command] + opts cmd = sub_process.Popen(full_cmd, stdout=sub_process.PIPE) return [line for line in cmd.communicate()[0].split('\n')]