子进程运行命令直到第一个管道,然后停止
subprocess runs command up to first pipe, then stops
如果我 运行 这个命令本身:
nmap -PN -p 22 --open -oG - 10.15.86.0/24 | awk '$NF~/ssh/{print}' > sshopen.txt
我收到了我想要的输出:
10.15.86.4
10.15.86.5
10.15.86.9
10.15.86.11
etc...
我网络上的所有主机都打开了 ssh 端口。
但是,当我尝试在 Python 子进程中 运行 相同的命令时,它似乎跳过了 awk。 Python 以下:
import subprocess
subnet = raw_input("Enter subnet to scan: ")
command1 = "nmap -PN -p 22 --open -oG - 10.15.86.0/24 | awk '$NF~/ssh /{print}' > sshopen.txt".split()
#command = ["nmap", "-PN", "-p", "22", "--open", "-oG", "-", subnet, "|", "awk", "'$NF~/ssh/{print }'", ">", "sshopen.txt"]
nmap = subprocess.Popen(command1, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
nmap_info, out = nmap.communicate()
print nmap_info
您会注意到注释 'command',因为我已尝试使用 'command' 和 'command1'。 return 输出:
Host: 10.15.86.4 () Status: Up
Host: 10.15.86.4 () Ports: 22/open/tcp//ssh///
Host: 10.15.86.5 () Status: Up
Host: 10.15.86.5 () Ports: 22/open/tcp//ssh///
etc...
它输出好像没有管道 awk (nmap -PN -p 22 --open -oG - 10.15.86.0/24) 而不是 (nmap -PN -p 22 --open -oG - 10.15。 86.0/24 | awk '$NF~/ssh/{print$2}' > sshopen.txt)。
为什么不在脚本中应用 awk?
谢谢
尝试
p1 = Popen(["nmap", "-PN", "-p", "22", "--open", "-oG", "-", subnet], stdout=PIPE)
p2 = Popen(["awk", "$NF~/ssh/{print}"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close()
output = p2.communicate()[0]
或
check_output(command, shell=True)
来自python的官方subprocess doc.
虽然有一些 security considerations shell=True
。
Unlike some other popen functions, this implementation will never implicitly call a system shell. This means that all characters, including shell metacharacters, can safely be passed to child processes. If the shell is invoked explicitly, via shell=True, it is the application’s responsibility to ensure that all whitespace and metacharacters are quoted appropriately to avoid shell injection vulnerabilities.
如果我 运行 这个命令本身:
nmap -PN -p 22 --open -oG - 10.15.86.0/24 | awk '$NF~/ssh/{print}' > sshopen.txt
我收到了我想要的输出:
10.15.86.4
10.15.86.5
10.15.86.9
10.15.86.11
etc...
我网络上的所有主机都打开了 ssh 端口。 但是,当我尝试在 Python 子进程中 运行 相同的命令时,它似乎跳过了 awk。 Python 以下:
import subprocess
subnet = raw_input("Enter subnet to scan: ")
command1 = "nmap -PN -p 22 --open -oG - 10.15.86.0/24 | awk '$NF~/ssh /{print}' > sshopen.txt".split()
#command = ["nmap", "-PN", "-p", "22", "--open", "-oG", "-", subnet, "|", "awk", "'$NF~/ssh/{print }'", ">", "sshopen.txt"]
nmap = subprocess.Popen(command1, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
nmap_info, out = nmap.communicate()
print nmap_info
您会注意到注释 'command',因为我已尝试使用 'command' 和 'command1'。 return 输出:
Host: 10.15.86.4 () Status: Up
Host: 10.15.86.4 () Ports: 22/open/tcp//ssh///
Host: 10.15.86.5 () Status: Up
Host: 10.15.86.5 () Ports: 22/open/tcp//ssh///
etc...
它输出好像没有管道 awk (nmap -PN -p 22 --open -oG - 10.15.86.0/24) 而不是 (nmap -PN -p 22 --open -oG - 10.15。 86.0/24 | awk '$NF~/ssh/{print$2}' > sshopen.txt)。 为什么不在脚本中应用 awk?
谢谢
尝试
p1 = Popen(["nmap", "-PN", "-p", "22", "--open", "-oG", "-", subnet], stdout=PIPE)
p2 = Popen(["awk", "$NF~/ssh/{print}"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close()
output = p2.communicate()[0]
或
check_output(command, shell=True)
来自python的官方subprocess doc.
虽然有一些 security considerations shell=True
。
Unlike some other popen functions, this implementation will never implicitly call a system shell. This means that all characters, including shell metacharacters, can safely be passed to child processes. If the shell is invoked explicitly, via shell=True, it is the application’s responsibility to ensure that all whitespace and metacharacters are quoted appropriately to avoid shell injection vulnerabilities.