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, close_fds=True) 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, close_fds=True) if cmd.wait() == 0: return True else: return False
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 usage(self, partition=None): """ Returns the results of df -PT """ results = {} # splitting the command variable out into a list does not seem to function # in the tests I have run command = '/bin/df -PT' if (partition): command += ' %s' % (partition) cmdref = sub_process.Popen(command, stdout=sub_process.PIPE, stderr=sub_process.PIPE, shell=True, close_fds=True) (stdout, stderr) = cmdref.communicate() for disk in stdout.split('\n'): if (disk.startswith('Filesystem') or not disk): continue (device, fstype, total, used, available, percentage, mount) = disk.split() results[mount] = { 'device': device, 'total': str(total), 'used': str(used), 'available': str(available), 'fstype': str(fstype), 'percentage': int(percentage[:-1]) } return results
def echo(self,linecount): """ TODO: response system messages info """ command = "/usr/bin/tail -n "+str(linecount)+" /var/log/messages" cmdref = sub_process.Popen(command, stdout=sub_process.PIPE, stderr=sub_process.PIPE, shell=True, close_fds=True) data = cmdref.communicate() return (cmdref.returncode, date[0], data[1])
def run(self, command, env=None): """ Runs a command, returning the return code, stdout, and stderr as a tuple. NOT FOR USE WITH INTERACTIVE COMMANDS. """ cmdref = sub_process.Popen(command, stdout=sub_process.PIPE, stderr=sub_process.PIPE, shell=True, close_fds=True, env=env) data = cmdref.communicate() return (cmdref.returncode, data[0], data[1])
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, close_fds=True) 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 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 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, close_fds=True) 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, close_fds=True) 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 __run_command(self, command, opts=[]): full_cmd = [command] + opts cmd = sub_process.Popen(full_cmd, stdout=sub_process.PIPE, close_fds=True) return [line for line in cmd.communicate()[0].split('\n')]