我遇到了Pythons`subprocess.popen`悬挂的问题

时间:2016-05-29 10:29:22

标签: python subprocess

我有一个简单的函数,可以对列表中的多个项目调用相同的subprocess.popen命令。它执行列表中的前2项没有问题,但第三项上的操作挂起。

似乎process_null.communicate()从未执行或至少没有完成,因为我从未从列表中的第3项获得输出。我试过更改列表,但得到相同的结果。有什么想法在这里发生?

def check_list(server_list):
    null_list = []

    for target in server_list:
        command_null="rpcclient -N -U '' {}". format (str(target))
        process_null = subprocess.Popen(command_null, shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
        output_null = process_null.communicate()[0].replace('\n','')

        if "NT_STATUS_ACCESS_DENIED" in output_null or "NT_STATUS_CONNECTION_REFUSED" in output_null or "NT_STATUS_UNSUCCESSFUL" in output_null:
            print '[-]  ' + target + '\tDenied NULL Session'
        else:
            print '[+]  ' + target + '\tAccepted NULL Session'
            null_list.append(target)


    return null_list

输出

[-]  192.168.1.3    Denied NULL Session
[-]  192.168.1.65   Denied NULL Session

1 个答案:

答案 0 :(得分:2)

rpcclient成功建立连接时,它会启动一个shell并等待stdin上的命令输入,如果没有使用-c标志给出,这就是这里发生的事情。您没有看到提示,因为您将所有输出(stdout + stderr)重定向到管道,但您不会对stdin执行相同操作,这意味着输入将从运行python解释器的相同tty中读取。

此外,如果不是绝对必要,你不应该使用shell=True字符串参数,而是使用参数列表:

command_null = ['rpcclient', '-N', '-U', '', str(target)]

要解决您的问题,您有两种选择:

  • 提供成功连接时执行的命令:

    command_null = ['rpcclient', '-N', '-U', '', '-c', 'exit', str(target)]
    
  • 在打开流程时使用stdin=PIPE,这会导致rcpclientstdin关闭communicate()时退出:

    process_null = subprocess.Popen(command_null, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
    
相关问题