f 是一个文件,如下所示:
+++++192.168.1.1+++++
Port Number: 80
......
product: Apache httpd
IP Address: 192.168.1.1
+++++192.168.1.2+++++
Port Number: 80
......
product: Apache http
IP Address: 192.168.1.2
+++++192.168.1.3+++++
Port Number: 80
......
product: Apache httpd
IP Address: 192.168.1.3
+++++192.168.1.4+++++
Port Number: 3306
......
product: MySQL
IP Address: 192.168.1.4
+++++192.168.1.5+++++
Port Number: 22
......
product: Open SSH
IP Address: 192.168.1.5
+++++192.168.1.6+++++
Port Number: 80
......
product: Apache httpd
IP Address: 192.168.1.6
预期的输出是:
These hosts have Apache services:
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.6
我试过的代码:
for service in f:
if "product: Apache httpd" in service:
for host in f:
if "IP Address: " in host:
print(host[5:], service)
它只是给了我所有的 IP 地址,而不是安装了 Apache 的特定主机。
我怎样才能做出预期的输出?