【发布时间】:2020-06-12 19:10:34
【问题描述】:
我正在尝试使用 Python 控制 netcat 会话,连接将是这样的:
listening on [any] 1234 ...
connect to [127.0.0.1] from (UNKNOWN) [127.0.0.1] 37878
id #### ----> user input
uid=0(root) gid=0(root) groups=0(root) #### ----> output
pwd #### ----> user input
/root #### ----> output
exit #### ----> user input
但是,我希望能够在发送之前控制和处理每个输入,以及在显示之前控制和处理每个输出。
我尝试使用子进程,但无法让它工作,我的代码类似于:
#!/usr/bin/python
import subprocess
def listener():
command = ''
proc = subprocess.Popen(['nc', '-lvn','1234'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while (command != 'exit'):
command = bytes(input(),'utf-8')
proc.stdin.write(command)
print(proc.stdout.readline().decode())
listener()
我无法从另一台机器收到任何正确的输出,我收到的唯一输出是:
sh: no job control in this shell
如果有帮助,我不介意使用除 subprocess 之外的任何其他库。
【问题讨论】:
-
直接使用socket不是更方便吗?
-
@SamMason 我试过了,但我无法让它工作,请参阅我之前的问题,我询问了我面临的问题,但没有答案。
-
@Ash-Ishh.. 现在测试它,希望它能完成工作。
标签: python python-3.x subprocess