标签:远程执行 while print dir 信息 style break 执行 RoCE
一、需求
# server 下发命令
# client 执行命令
# ssh协议
# import os
# ret = os.popen(‘ls‘).read()
# print(ret)
import subprocess
# 内置模块 和os模块的功能有相似之处
# 能执行操作系统的命令的功能
ret = subprocess.Popen(‘dir‘, # 要执行的命令
shell=True, # 表示要执行的是一条系统命令
stdout=subprocess.PIPE, # 存储执行结果的正常信息
stderr=subprocess.PIPE) # 存储执行结果的错误信息
print(‘stdout : ‘,ret.stdout.read().decode(‘gbk‘))
print(‘stderr : ‘,ret.stderr.read().decode(‘gbk‘))
二、2.1 TCP
server端
import socket
sk = socket.socket()
sk.bind((‘127.0.0.1‘,8090))
sk.listen()
conn,addr = sk.accept()
while True:
cmd = input(‘cmd : ‘)
if cmd == ‘q‘:
conn.send(cmd.encode(‘utf-8‘))
break
conn.send(cmd.encode(‘utf-8‘))
print(‘stdout : ‘,conn.recv(1024).decode(‘gbk‘))
conn.close()
sk.close()
client端
import socket
import subprocess
sk = socket.socket()
sk.connect((‘127.0.0.1‘,8090))
while True:
cmd = sk.recv(1024).decode(‘utf-8‘)
if cmd == ‘q‘: break
res = subprocess.Popen(cmd,shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
sk.send(res.stdout.read())
sk.send(res.stderr.read())
sk.close()
2.2 UDP
server端
import socket
sk = socket.socket(type=socket.SOCK_DGRAM)
sk.bind((‘127.0.0.1‘,8090))
msg,addr = sk.recvfrom(1024)
while True:
cmd = input(‘cmd : ‘)
if cmd == ‘q‘:
sk.sendto(cmd.encode(‘utf-8‘),addr)
break
sk.sendto(cmd.encode(‘utf-8‘),addr)
print(‘stdout : ‘,sk.recvfrom(2048)[0].decode(‘gbk‘))
print(‘stderr : ‘,sk.recvfrom(2048)[0].decode(‘gbk‘))
sk.close()
client端
import socket
import subprocess
sk = socket.socket(type=socket.SOCK_DGRAM)
sk.sendto(b‘111‘,(‘127.0.0.1‘,8090))
while True:
cmd = sk.recvfrom(1024)[0].decode(‘utf-8‘)
if cmd == ‘q‘: break
res = subprocess.Popen(cmd,shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
sk.sendto(res.stdout.read()*100,(‘127.0.0.1‘,8090))
sk.sendto(res.stderr.read(),(‘127.0.0.1‘,8090))
sk.close()
标签:远程执行 while print dir 信息 style break 执行 RoCE
原文地址:https://www.cnblogs.com/mys6/p/10799572.html