def close(self): self.p.stdin.close() self.p.stdout.close() self.p.stderr.close() self.p.terminate() self.p.wait() log.info("Process {} exited with code {}".format( green(self.p.pid), green(self.p.returncode)))
def __init__(self, host, port, timeout=5): super(remote, self).__init__() self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.s.settimeout(timeout) self.s.connect((host, port)) self.host = host self.port = port log.info("Connecting to {} on port {}".format(green(host), green(port)))
def recvuntil(self, delim): data = self.recv(len(delim), nolog=True) while data[-len(delim):] != delim: data += self.recv(1, nolog=True) log.debug("Received {} bytes\n{}".format(green(len(data)), hexdump(data))) return data
def recvall(self): data = b"" try: while True: data += self.recv(1, nolog=True) except: log.debug("Received {} bytes\n{}".format(green(len(data)), hexdump(data))) return data
def __init__(self, args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE): super(process, self).__init__() self.p = subprocess.Popen(args, stdin=stdin, stdout=stdout, stderr=stderr, shell=False) log.info("Process started with PID {} {}".format( green(self.p.pid), args))
def send(self, data): self.s.sendall(data) log.debug("Sent {} bytes\n{}".format(green(len(data)), hexdump(data)))
def recv(self, size, nolog=False): data = self.s.recv(size) if not nolog: log.debug("Received {} bytes\n{}".format(green(len(data)), hexdump(data))) return data
def close(self): self.s.shutdown(socket.SHUT_RDWR) self.s.close() log.info("Closed connection with {} on port {}".format( green(self.host), green(self.port)))
def info(self, msg, details=None): if self.level >= 1: if details is not None: msg += "\n{}\n{}\n{}".format("\u2014"*term.columns, details, "─"*term.columns) self.stdout.write("[{}]: {}\n".format(green("INFO"), msg))
def recvline(self): data = self.p.stdout.readline() log.debug("Received {} bytes\n{}".format(green(len(data)), hexdump(data))) return data
def send(self, data): self.p.stdin.write(data) log.debug("Sent {} bytes\n{}".format(green(len(data)), hexdump(data))) self.p.stdin.flush()