033 remote execution command

Keywords: Python socket shell Linux encoding

Execute command on server
It seems that there is no module to execute the command before. Now it is added

import  subprocess
obj = subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE)
#Get an object. The command is in front, shell The script can be executed, stdout Set a process pipeline. There are actually two processes, one is the main process and the other is shell process
x = obj.stdout.read()    # Here is the execution information
print(str(x,'gbk'))  #  str(x,'utf8')Forget that the system is gbk Encoding

 

###Example of remote command execution:

#If the execution result of the instruction is short, there will be no problem

 1 import socket
 2 import subprocess
 3 def get_server_socket():
 4     sk = socket.socket()
 5     server_address = ('127.0.0.1',8888)
 6     sk.bind(server_address)
 7     sk.listen(5)
 8     return sk
 9 
10 def get_conn(sk):
11     print('waitconnect...')
12     conn,addr = sk.accept()
13     return conn
14 
15 if __name__ == '__main__':
16     sk = get_server_socket()
17     conn = get_conn(sk)
18     while True:
19         try:
20             data = conn.recv(1024)##Linux will not report an error here. If conn is closed forcibly, data will become empty, that is to say, Linux will not try directly.
21         except Exception as e:
22             conn = get_conn(sk)
23         print(str(data,'utf8'))
24         if not data:
25             conn = get_conn(sk)
26             continue
27         sp = subprocess.Popen(str(data,'utf8'),shell=True,stdout=subprocess.PIPE)#Note if the command is of string type
28         result = sp.stdout.read()#str(sp.stdout.read(),'gbk')
29         conn.send(result)
30         print(str(result,'gbk'))
31         print('waiting...')
32     conn.close()
cmd_server.py
 1 import socket
 2 
 3 def connect_server():
 4     sk = socket.socket()
 5     server_address = ('127.0.0.1',8888)
 6     sk.connect(server_address)
 7     return sk
 8 
 9 if __name__ == '__main__':
10     sk = connect_server()
11     while True:
12         inp = input('>>>')
13         if inp == 'exit':
14             break
15         sk.send(bytes(inp,'utf8'))
16         print('waiting...')
17         data = sk.recv(1024)
18         print(str(data,'gbk'))
19     sk.close()
cmd_client.py

 

#The instruction execution result is too long to be delivered at one time. The idea of transferring documents.

 1 import  socket
 2 import  subprocess
 3 def  get_server_socket():
 4     sk = socket.socket()
 5     server_address = ('127.0.0.1',8888)
 6     sk.bind(server_address)
 7     sk.listen(5)
 8     return sk
 9 def  get_conn(sk):
10     print('waitconnect...')
11     conn, addr = sk.accept()
12     return conn
13 
14 if __name__ == '__main__':
15     sk = get_server_socket()
16     conn = get_conn(sk)
17     while True:
18         try:
19             data = conn.recv(1024)##Linux will not report an error here. If conn is closed forcibly, data will become empty, that is to say, Linux will not try directly.
20         except Exception as e:
21             conn = get_conn(sk)
22         print(str(data,'utf8'))
23         if not data:
24             conn = get_conn(sk)
25             continue
26         sp = subprocess.Popen(str(data,'utf8'),shell=True,stdout=subprocess.PIPE)#Note if the command is of string type
27         result = sp.stdout.read()#Read content
28         conn.sendall(bytes(str(len(result)),'utf8'))#Sending length
29         conn.sendall(result)#send content
30         print(str(result,'gbk'))
31         print('waiting...')
32     conn.close()
cmd_server.py
 1 import socket
 2 def connect_server():
 3     sk = socket.socket()
 4     server_address = ('127.0.0.1',8888)
 5     sk.connect(server_address)
 6     return sk
 7 
 8 if __name__ == '__main__':
 9     sk = connect_server()
10     while True:
11         inp = input('>>>')
12         if inp == '__exit':
13             break
14         sk.send(bytes(inp,'utf8'))
15     print('waiting...')
16 
17     result_length = int(str(sk.recv(1024),'utf8'))#Record the length, and then stop receiving the corresponding length
18     data = bytes()
19     while  len(data) != result_length:
20         r = sk.recv(1024)
21         data += r
22     print(str(data,'gbk'))
23     sk.close()
cmd_client.py

 

####In addition, two consecutive reception will cause packet sticking

Make a partition, that is, the receiver sends a random data, and the sender continues to send after receiving it, so that it is solved.

Posted by rdog157h on Sat, 04 Apr 2020 15:55:01 -0700