python - 在 Python 中捕获 netcat shell 命令输出

标签 python linux shell subprocess

<分区>

我想从我的 python 脚本执行 netcat 命令,所以我使用“subprocess.Popen”,但问题是这个命令的输出直接打印在 may shell 控制台中,我想让他在一个变量中,所以我可以在打印之前做一些修改。

res = subprocess.Popen("nc -v 192.168.1.1 25", stdout=subprocess.PIPE, stderr=None, shell=True)
#output = res.communicate()
#output = str(output)
#print output

最佳答案

如果您想在 Python 中轻松调用 shell 命令,请使用 sh图书馆。

在 sh 中这将是:

  from sh import nc
  output = nc("-v", "192.168.1.1", "25")  # output is string of the command output

一如既往,installing Python libraries are recommended to do with virtualenv .

关于python - 在 Python 中捕获 netcat shell 命令输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22386072/

相关文章:

python - 为什么在有 flask 的情况下使用 werkzeug

python - 如何在函数中使用 *args 返回字典列表?

python - 使用字符串列表按列标签对 Pandas DataFrame 进行切片

c - 使用gcc命令行从.c文件构建.so文件

c - linux-我如何使用 C 中的 execlp 函数运行任何带参数的命令

c++ - 如何以编程方式获取 Linux 中目录的可用磁盘空间

bash - 如何列出我的 shell 中定义的函数?

python - AWS Elastic Beanstalk 上的 Django 403 禁止访问

linux - 在脚本中关闭 echo

bash - sed - 在进行替换后从列出的值中删除最后一个逗号