-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShodanPing.py
60 lines (54 loc) · 1.73 KB
/
ShodanPing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
###This program takes a user input search query 'SEARCH'
###and scans Shodan.io for matching IP addresses over
### 'PAGES' number of pages. Once a list is compiled
### we then ping every aIP in the list to ensure it is
### actually alive. Then return list of alive IP's to user.
#!/usr/bin/python
import shodan
import pyping
import time
###User Settings
APIkey = "API KEY HERE"
SEARCH = "SEARCH TERM HERE"
PAGES = 2
### Required variables - Dont change
ipList = []
api = shodan.Shodan(APIkey)
###Function for scanning 'x' pages of shodan for a string
def Shodan_Search():
counter = 0
kill = 0
print "Searching Shodan now for {}.".format(SEARCH)
try:
while counter < PAGES:
results = api.search(SEARCH,page=counter,limit=None)
for item in results['matches']:
ip = item['ip_str']
if ip not in ipList:
ipList.append(ip)
else:
print "Reached end of list on page {}.".format(counter)
kill = 1
break
if kill is 1 :
break
else:
counter = counter + 1
print "Page {} scanned.".format(counter)
time.sleep(1)
except shodan.APIError, e:
print e
def Ping_Address_List():
pingCounter = 0
print "Pinging each address to verify its up."
for address in ipList:
pingresponse = pyping.ping(address)
if pingresponse.ret_code == 0:
pingCounter = pingCounter + 1
print address
else:
pass
print "Total results {}".format(len(ipList))
print "Total alive results: {}".format(pingCounter)
Shodan_Search()
Ping_Address_List()