如何 运行 一个 python 文件并通过 nmap 扫描生成的 ips

How to run a python file and scan the generated ips by nmap

我写了一个 python 脚本来搜索 shodan,我的脚本代码 returns 一个包含 ip 列表的文件,每行包含一个 ip。 这是我的代码:

import shodan
SHODAN_API="YOUR_SHODAN_API"
api = shodan.Shodan(SHODAN_API)
try:
    # Search Using Shodan
    results = api.search('EXAMPLE')

    # Showing the results
    print 'Results found: %s' % results['total']
    for result in results['matches']:
        print '%s' % result['ip_str']
    ''' following lines could be uncommented due to more information
    Don't Uncomment if you are using scanning methods with the results '''
        #print result['data']
        #print ''
except shodan.APIError, e:
    print 'Error: %s' % e

我想知道是否有任何方法可以自动执行 运行 我的代码的任务,然后通过外部脚本或在 OSX 和 Linux 上工作的东西扫描 ip 列表?

您可以简单地使用如下所示的 bash 脚本:

#!/bin/bash
python ShodanSearch.py >> IPResult.txt
cat IPResult.txt | while read line
do
sudo nmap -n -Pn -sV -p 80,8080 -oG - $line >> NResult.txt
done

作为上述解决方案的替代方案,您还可以使用 python os 模块执行 nmap 以在 python 脚本中执行 shell 命令,或者现在首选的方法是用subprocess模块​​,后者没有亲自用过,但绝对可以做你想做的。