我运行netcat来监听bash shell中的服务器:
nc domain.com 1234
服务器输出三个数字;我需要对它们执行算术并将结果发送回服务器。
我有这段代码:
#!/usr/bin/env python
import subprocess
nccommand = "nc domain.com 1234 > ncoutput.txt"
subprocess.Popen(nccommand, shell=True)
with open("ncoutput.txt") as f:
lines = f.readlines()
lines = [x.strip() for x in lines]
lines = [int(x) for x in lines]
result = (lines[0]*lines[1])/lines[2]
*command to input result into the netcat stream goes here*
在关闭netcat命令之前,不会创建 ncouput.txt
,这是有问题的,因为我需要打开它来传输计算结果
另外,我不确定如何在netcat流仍处于打开状态时将字符串输入命令行。我应该subprocess.Popen(numinput, shell=True)
吗?