def run_linux(self): try: # run this command (exit_code, stdout, stderr) = Command.run(self, ["ip", "addr"]) # empty result by default res = [] # split the stdout into lines lines = stdout.split('\n') for line in lines: line = line.strip() if not line.startswith("inet"): continue pos = line.find(' ') if pos != -1: line = line[pos + 1:] pos = line.find(' ') if pos != -1: line = line[:pos] if line.startswith("127.0.0.1/") or line.startswith("::1/"): continue res.append(line) return res except Exception as e: return [str(e)]
def run_windows(self, server): (exit_code, stdout, stderr) = Command().run(["nslookup", "-type=SRV", server]) # first collect only interesting lines from output name_re = re.compile(".*svr\s*hostname\s*=\s*(.*)$", re.IGNORECASE) port_re = re.compile(".*port\s*=\s*(.*)$", re.IGNORECASE) # interesting entries entries = [] for line in stdout.split('\r\n'): match = name_re.match(line) if match: entries.append(match.group(1).strip()) match = port_re.match(line) if match: entries.append(match.group(1).strip()) servers = [] # now make servers out of entries if len(entries) > 1: if len(entries) >= 4: servers.append((entries[1], entries[0])) servers.append((entries[3], entries[2])) else: servers.append((entries[1], entries[0])) return servers
def get_str_unsafe(self, request): arg_port = "-p" # bad fix - need to base decision on squid version and not on the system if System.name() == System.WS_FREEBSD: arg_port = "-a" args = [ self.exe, arg_port, str(self.port), "-h", self.host, "mgr:%s" % request ] (exit_code, stdout, stderr) = Command().run(args) if exit_code == 0: pos = stdout.find("\r\n\r\n") if pos != -1: stdout = stdout[pos + 4:] return stdout # if we got here everything failed raise Exception( "Command %s failed.\n\tExit Code: %d\n\tSTDOUT: %s\n\tSTDERR: %s" % (" ".join(args), exit_code, stdout, stderr))
def get_freebsd_total_mem(self, pagesize): (exit_code, stdout, stderr) = Command.run(self, ["sysctl", "-n", "vm.stats.vm.v_page_count"]) if exit_code == 0: return pagesize * int(stdout) return 0
def dump_device(self, name): try: # construct args args = [] if System.WS_LINUX == System.name(): args = ["ip", "addr", "show", name] elif System.WS_FREEBSD == System.name(): args = ["ifconfig", name] else: raise Exception( "NetDeviceDumper::dump_device - not implemented on system '%s'" % System.name()) # and run them (ret, stdout, stderr) = Command().run(args) if ret != 0: raise Exception("Invalid exit code: %d; (%s, %s)" % (ret, stdout, stderr)) # if we got here then it is fine return stdout except Exception as e: return "ERROR: %s\n%s" % (str(e), traceback.format_exc())
def generate(self, path_to_cert_pem, country, state, city, organization, ou, email, cn, days): # create arguments array data = { 'country': country, 'province': state, 'city': city, 'organization': organization, 'organizational-unit': ou, 'common-name': cn, 'e-mail': email, 'lifetime': days, 'output': path_to_cert_pem } args = [self.exe, "--action=create-root-certificate"] for key in data.keys(): args.append("--%s=%s" % (key, str(data[key]))) (exit_code, stdout, stderr) = Command().run(args) if exit_code == 0: return # if we got here everything is bad raise Exception( "Command %s failed.\n\tExit Code: %d\n\tSTDOUT: %s\n\tSTDERR: %s" % (" ".join(args), exit_code, stdout, stderr))
def run_freebsd(self, result): pagesize = self.get_freebsd_page_size() # update result result['mem']['total'] = self.get_freebsd_total_mem(pagesize) result['mem']['free'] = self.get_freebsd_free_mem(pagesize) result['mem']['used'] = result['mem']['total'] - result['mem']['free'] used = float(result['mem']['used']) total = float(result['mem']['total']) result['mem']['percent'] = int(100 * (used/total)) # and for memory and swap [total, used, free] array swaptotal = 0 swapfree = 0 swapused = 0 # run for swap (exit_code, stdout, stderr) = Command.run(self, ["swapinfo", "-k", "1K"]) if exit_code == 0: (swaptotal, swapused, swapfree) = self.parse_freebsd_swap(stdout) result['swap']['total'] = swaptotal result['swap']['free'] = swapfree result['swap']['used'] = swapused used = float(result['swap']['used']) total = float(result['swap']['total']) result['swap']['percent'] = int(100 * (used/total))
def categorize(self, domain): try: name = "categories_checker" if System.WS_WINDOWS == System.name(): name += ".exe" exe = os.path.join(Paths.bin_dir(), name) arg1 = "--definitions=%s" % (os.path.join( Paths.var_dir(), "spool", "categories", "definitions.dat")) arg2 = "--domain=%s" % domain (exit_code, stdout, stderr) = Command().run([exe, arg1, arg2]) if 0 == exit_code: data = stdout.strip() data = data.strip("]") data = data.strip("[") data = data.strip() if len(data) == 0: return [] else: return data.split(':') except Exception as e: pass return []
def get_freebsd_page_size(self): pagesize = 0 (exit_code, stdout, stderr) = Command.run(self, ["sysctl", "-n", "hw.pagesize"]) if exit_code == 0: pagesize = int(stdout) return pagesize
def run(self): try: (exit_code, stdout, stderr) = Command().run([BinaryLdap.full_path(), "--test"]) return {'exit_code': exit_code, 'stdout': stdout, 'stderr': stderr} except Exception as e: return {'exit_code': 1, 'stdout': '', 'stderr': str(e)}
def run(self): try: # run this command (exit_code, stdout, stderr) = Command.run(self, ["uptime"]) # and convert return stdout except Exception as e: return str(e)
def run(self): (exit_code, stdout, stderr) = Command.run(self, ["df", "-k", Paths.var_dir()]) if exit_code == 0: line = filter(None, stdout.split('\n')[1:2]) rows = line.split(' ') for row in rows: if row.find('%') != -1: return row return '0%'
def run_freebsd(self, keytab_path): args = [self.exe, "-v", "-k", keytab_path, "list"] (exit_code, stdout, stderr) = Command().run(args) if exit_code != 0: raise Exception("Cannot run command %s, error:\n%s" % (" ".join(args), stdout + stderr)) return stdout + "\n" + stderr
def delete_user(self, user): if not os.path.exists(self.htpasswd): return # construct args args = [self.exe, "-D", self.htpasswd, user] # and run the command return Command().run(args)
def run_freebsd(self, keytab_path, spn): # now we should also call the actual kinit to see if it can connect to the domain args = [self.exe, "-k", "-t", keytab_path, spn] (exit_code, stdout, stderr) = Command().run(args) if exit_code != 0: raise Exception("Cannot run command %s, error:\n%s" % (" ".join(args), stdout + stderr)) return stdout + "\n" + stderr
def run(self, file): name = "ldap" if System.name() == System.WS_WINDOWS: name += ".exe" args = [os.path.join(Paths.bin_dir(), name), "--file=" + file] # run this command (exit_code, stdout, stderr) = Command.run(self, args) # and convert return {'exit_code': exit_code, 'stdout': stdout, 'stderr': stderr}
def create_user(self, user, pasw): # construct args args = [self.exe, "-b"] if not os.path.exists(self.htpasswd): args.append("-c") args.append(self.htpasswd) args.append(user) args.append(pasw) # and run return Command().run(args)
def as_json(self, pem): arg1 = "--action=verify-root-certificate" arg2 = "--input=" + pem args = [self.exe, arg1, arg2] # get the certificate info (exit_code, stdout, stderr) = Command().run(args) if exit_code == 0: return json.loads(stdout) # if we got here everything is bad raise Exception( "Command %s failed.\n\tExit Code: %d\n\tSTDOUT: %s\n\tSTDERR: %s" % (" ".join(args), exit_code, stdout, stderr))
def dump(self, cacert): cacert_path = os.path.join(Paths.etc_dir(), cacert) if not os.path.isfile(cacert_path): raise Exception("File %s does not exist or is not accessible!" % cacert_path) args = [self.exe, "x509", "-in", cacert_path, "-text"] (exit_code, stdout, stderr) = Command().run(args) if exit_code != 0: raise Exception("Cannot run command %s, error:\n%s" % (" ".join(args), stdout + stderr)) # if everything is fine - return the cert contents return "%s\n%s" % (stdout, stderr)
def to_der(self, pem, der): # run the certificate convert tool arg1 = "--action=convert-root-certificate" arg2 = "--input=" + pem arg3 = "--output=" + der args = [self.exe, arg1, arg2, arg3] (exit_code, stdout, stderr) = Command().run(args) if exit_code == 0: return # if we got here everything is bad raise Exception( "Command %s failed.\n\tExit Code: %d\n\tSTDOUT: %s\n\tSTDERR: %s" % (" ".join(args), exit_code, stdout, stderr))
def run(self, folder): # assume failure success = False exit_code = -1 stdout = "" stderr = "" try: # run this command (exit_code, stdout, stderr) = Command.run(self, ["df", "-h", "-l", folder]) # and mark success success = True except Exception as e: stderr = str(e) # the data data = { 'error': stderr, 'size': '', 'used': '', 'avail': '', 'ratio': '' } # check if we were successful try: if success: # get the most valud array = self.parse(stdout) data['size'] = array[1] data['used'] = array[2] data['avail'] = array[3] data['ratio'] = array[4] except Exception as e: success = False data['error'] = str(e) return (success, data)
def get_str(self): result = "" try: (exit_code, stdout, stderr) = Command().run(self.args) if exit_code == 0: result = stdout + stderr else: raise Exception( "Command %s failed.\n\tExit Code: %d\n\tSTDOUT: %s\n\tSTDERR: %s" % (" ".join(self.args), exit_code, stdout, stderr) ) except Exception as e: result = str(e) result += "\n%s\n" % traceback.format_exc() return result
def run(self): try: args = ["top", "-d", "0.5", "-b", "-n2"] if System.WS_FREEBSD == System.name(): args = ["top", "-d", "2", "-b"] # run top command twice (it gives correct output only second time) (exit_code, stdout, stderr) = Command.run(self, args) if exit_code != 0: raise Exception( "Cannot run top command, exit code: %d, stdout: %s, stderr: %s" % (exit_code, stdout, stderr)) # parse it return self.parse(stdout) except Exception as e: return 0
def run(self): if System.WS_FREEBSD == System.name(): args = [ "ps", "-U", self.username, "-o", "pid,user,cputime,pcpu,rss,pmem,command" ] else: args = [ "ps", "-U", self.username, "-u", self.username, "-o", "pid,user,cputime,pcpu,rss,pmem,command" ] # run this command (exit_code, stdout, stderr) = Command.run(self, args) # and convert if exit_code == 0: return self.parse_output(stdout) else: return {}
def run_linux(self, server): (exit_code, stdout, stderr) = Command().run(["dig", "srv", server, "+short"]) servers = [] for line in stdout.split('\n'): output = line.split(' ') if len(output) != 4: continue servers.append((output[3].strip().strip('.'), output[2])) # we only take first two if len(servers) >= 2: break return servers
def run(self, icap_port="1344"): if System.WS_FREEBSD == System.name(): args = ["netstat", "-an", "-p", "tcp"] else: args = [ "netstat", "--tcp", "--all", "--numeric", "--program", "--verbose" ] # run this command (exit_code, stdout, stderr) = Command.run(self, args) # and convert if exit_code == 0: if System.WS_FREEBSD == System.name(): return self.parse_freebsd_output(stdout, icap_port) else: return self.parse_linux_output(stdout, icap_port) else: return []
def dump(self): args = [ sys.executable, self.exe, "--server1", self.server1, "--port1", self.port1, "--dump" ] if self.server2: args.extend(["--server2", self.server2, "--port2", self.port2]) if self.token: args.extend(["--token", self.token]) (exit_code, stdout, stderr) = Command().run(args) if exit_code == 0: return json.loads(stdout) # if we got here everything failed raise Exception( "Command %s failed.\n\tExit Code: %d\n\tSTDOUT: %s\n\tSTDERR: %s" % (" ".join(args), exit_code, stdout, stderr))
def running_tz_str(self): # for debug only if System.WS_WINDOWS == System.name(): return "Europe/London" # this is the value to return value = "" if System.name() == System.WS_LINUX: # on linux we call timedatectl args = ["timedatectl", "status", "--no-pager"] (exit_code, stdout, stderr) = Command().run(args) if exit_code != 0: raise Exception("Cannot run command %s, exit code: %d, error message:\n%s" % (" ".join(args), exit_code, stdout + stderr)) for line in stdout.split('\n'): pos = line.find(":") if pos == -1: continue left = line[:pos].strip() right = line[pos + 1:].strip() match = re.match( r'.*time.*zone.*', line, re.M|re.I) if match: value = right if System.name() == System.WS_FREEBSD: # TODO # on freebsd it is a little different; the /etc/localtime can be either a symlink or a copy of any file # including one in subdirectories of /usr/share/zoneinfo. We need to first find that file and then # parse out the zone information as folder/subfolder starting from /usr/share/zoneinfo as root, complex! raise Exception("NOT_IMPL of FreeBSD") return value
def run_freebsd(self): try: # run this command (exit_code, stdout, stderr) = Command.run(self, ["ifconfig"]) # empty result by default res = [] # split the stdout into lines lines = stdout.split('\n') for line in lines: line = line.strip() interesting = False if line.startswith("inet ") or line.startswith("inet6 "): interesting = True if not interesting: continue pos = line.find(' ') if pos != -1: line = line[pos + 1:] pos = line.find(' ') if pos != -1: line = line[:pos] if line.startswith("127.0.0.1") or line.startswith("::1"): continue if line.find("%lo") != -1: continue res.append(line) return res except Exception as e: return [str(e)]
def run(self, params): name = "database" if System.name() == System.WS_WINDOWS: name += ".exe" exe = os.path.join(Paths.bin_dir(), name) args = [exe] args.extend(params) (exit_code, stdout, stderr) = Command.run(self, args) if exit_code != 0: logging.error("ERROR: ") logging.error("ERROR: Cannot switch database, error: %d", exit_code) logging.error("ERROR: ") logging.error("STDOUT: ") logging.error(stdout) logging.error("STDERR: ") logging.error(stderr) raise Exception("FAILURE!!!")