模拟ssh的远程操控:
服务端:
import socket import subprocess phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM) phone.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) phone.bind((‘127.0.0.1‘, 8081)) phone.listen(5) print(‘starting----------------‘) while 1: conn,cilent_addr = phone.accept() print(conn, cilent_addr) while 1: try: ret = conn.recv(1024) if not ret:break res = subprocess.Popen(ret.decode(‘utf_8‘), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout = res.stdout.read() stderr = res.stderr.read() conn.send(stdout + stderr) except Exception:break conn.close() phone.close()
客户端:
import socket phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM) phone.connect((‘127.0.0.1‘, 8081)) while 1: cmd = input(‘>>>: ‘).strip() if not cmd: continue phone.send(cmd.encode(‘utf_8‘)) cet = phone.recv(1024) print(cet.decode(‘gbk‘)) phone.close()
时间: 2024-10-11 00:38:06