Python sends a command through a socket

I have a problem's. I want to create a simple program that connects to a server and executes a command using a subprocess, and returns the result to the client. It is simple, but I cannot get it to work. Now this is what I have: Client:

import sys, socket, subprocess conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = sys.argv[1] port = int(sys.argv[2]) socksize = 1024 conn.connect((host, port)) while True: shell = raw_input("$ ") conn.send(shell) data = conn.recv(socksize) #msglen = len(data) output = data iotype = subprocess.PIPE cmd = ['/bin/sh', '-c', shell] proc = subprocess.Popen(cmd, stdout=iotype).wait() stdout,stderr = proc.communicate() conn.send(stdout) print(output) if proc.returncode != 0: print("Error") 

Server:

 import sys, socket, subprocess host = '' port = 50106 socksize = 1024 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((host, port)) print("Server started on port: %s" %port) s.listen(1) print("Now listening...\n") conn, addr = s.accept() while True: print 'New connection from %s:%d' % (addr[0], addr[1]) data = conn.recv(socksize) cmd = ['/bin/sh', '-c', data] proc = subprocess.Popen(cmd, stdout=subprocess.PIPE).wait() stdout,stderr = cmd.communicate() if not data: break elif data == 'killsrv': sys.exit() 
+4
source share
4 answers

Danger, Will Robinson !!!

Do you really want to send commands in clear text without network authentication? It is very, very dangerous.

Do it via SSH with paramiko .

Well, I have heard this answer too many times. I do not want to use SSH. I just build it to learn more about sockets. I am not going to actually use this if I want to send commands to the system. - AustinM

I could not deduce this noble quest from your question. :-)

The socket module is a thin layer on top of the posix library; simple sockets are tiring and hard to reach. As of today (2014), asynchronous I / O and concurrency are not among the most powerful features of Python - 3.4 starts to change, but libraries lag behind for a while. My advice is to spend your time learning a higher level API such as Twisted ( twistedmatrix.com/trac ). If you are really interested in low-level material, immerse yourself in the source of the project.

Good. Any idea on how I can use twisted things for this type of thing? - AustinM

Take a look at twistedmatrix.com/documents/current/core/examples/#auto2

+7
source

Well, I can understand your disappointment in Austin; I was in the same boat. However, trial and error, finally, did not work. Hope you were looking for this:

 print "Command is:",command op = subprocess.Popen(command, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE) if op: output=str(op.stdout.read()) print "Output:",output conn.sendall(output) else: error=str(op.stderr.read()) print "Error:",error conn.sendall(error) 
+3
source

It is not clear why you are using subprocess.Popen() for the same command on both the client and the server. Here is a diagram of what I will try to do (pseudocode):

customer

 while True: read command from user send command to server wait for and then read response from server print response to user 

server

 while True: wait for and then read command from client if command is "killsrv", exit execute command and capture output send output to client 
+1
source

The problem with your code is this line (both on the client and on the server):

 proc = subprocess.Popen(cmd, stdout=iotype).wait() stdout,stderr = proc.communicate() 

You call wait on the Popen object, which means the proc variable gets int ( wait returned) instead of the Popen object. You can simply get rid of wait - since communicate waiting for the process to complete before returning, and you still do not check the exit code, you do not need to call it.

Then, in your client, I don’t think that you even need subprocess calls if you do not execute any command that the server sends back.

0
source

Source: https://habr.com/ru/post/1339582/


All Articles