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

353 views Asked by At

I have written a python script to search through shodan, my script code returns a file containing an ip list that each line contains single ip. here is my code:

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

I was wondering if there is any way to automate the task of running my code and then scanning the ip list by external script or something that work on OSX and Linux ?

2

There are 2 answers

2
Ashkan On BEST ANSWER

You can simply use a bash script like the following one:

#!/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
0
NeelD On

As an alternative to the solution above, you could also execute nmap using the python os module to execute shell commands within your python script, or the now preferred method is with the subprocess module, haven’t personally used the latter, but it can definitely do what you want.