<p>以下方法可以帮助您开始:</p>
<pre><code>import re
nmap = """Host: 127.0.0.1 () Status: Up
Host: 127.0.0.1 () Ports: 22/open/tcp//ssh///, 80/open/tcp//http///, 443/open/tcp//https/// Ignored State: closed (65532)
Host: 127.0.0.2 () Status: Up
Host: 127.0.0.2 () Ports: 21/open/tcp//ftp///, 22/open/tcp//ssh///, 25/open/tcp//smtp///, 53/filtered/tcp//domain///, 80/open/tcp//http///, 110/open/tcp//pop3///, 143/open/tcp//imap///, 443/open/tcp//https///, 465/filtered/tcp//smtps///, 993/open/tcp//imaps///, 995/open/tcp//pop3s///, 5222/filtered/tcp//xmpp-client/// Ignored State: closed (65523)
# Nmap done at Sat Nov 7 10:40:36 2015 2 IP addresses (2 hosts up) scanned in 32.07 seconds"""
entries = []
for line in nmap.split('\n'):
re_host = re.match(r'Host\: ([0-9.]+?)\s+', line)
if re_host:
host = re_host.group(1)
ports = re.findall('(\d+)\/open', line)
if len(ports):
entries.append((host, ports))
for host, ports in entries:
print '{:16} {}'.format(host, ports)
</code></pre>
<p>这将显示以下输出:</p>
^{pr2}$
<p>您需要用从文件中读取来替换<code>split</code>。在</p>