Python脚本-使用参数启动nmap

问题描述

这些脚本启动应用程序nmap。只有它不会将var输出到应用程序nmap。但是,如果我分别打印它们,它就可以。

正在寻找一些建议,非常感谢。

Image script-Image output

脚本:

import os
import subprocess

xIP=input("Please enter IP-address to use on NMAP port-scan: ")
xOP=input("Please apply options to add on port-scan: ")

print("Entered values: ",xIP + xOP)

command = "nmap print xIP xOP"
#os.system(command)
subprocess.Popen(command)

input("Press ENTER to exit application...")

解决方法

command = "nmap print xIP xOP"

这是问题所在。首先,与Uriya Harpeness mentioned一样,subprocess.Popen()在将命令分成列表等集合中时效果最好。 shlex.split()并非为此建议使用str.split(),而是Popen文档中的示例所示。

但是,您遇到的另一个问题是将变量名放入字符串文字中,因此它们仅被视为字符串的一部分,而不是对其内容进行评估。要获得所需的行为,只需直接引用变量:

command = ["nmap","print",xIP,xOP]
subprocess.Popen(command)
,

查看此处:https://docs.python.org/3/library/subprocess.html#subprocess.Popen

将程序名称和参数分开,它期望列表中的第一项是要运行的程序的名称。

就您而言,subprocess.Popen(command.split(' '))就足够了。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...