-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinvert.py
More file actions
executable file
·74 lines (65 loc) · 2.4 KB
/
Copy pathbinvert.py
File metadata and controls
executable file
·74 lines (65 loc) · 2.4 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python2
import requests
import argparse
import subprocess
from lxml import html
from urlparse import urlparse
import sys
import re
def get_args():
parser = argparse.ArgumentParser(description='Return the hostname of the supplied IP addresses using Bing')
parser.add_argument('-i', '--ips',
help='Comma separated list of ip addresses')
parser.add_argument('-f', '--filename',
help='File with one IP address per line')
parser.add_argument('-p', '--pages', type=int, default=1,
help='Number of Bing result pages to search through')
return parser
def search(url,results):
r = requests.get(url)
tree = html.fromstring(r.content)
for node in tree.xpath("//div[@class='b_attribution']/cite"):
results.append(node.xpath("normalize-space()"))
return tree
if __name__ == '__main__':
parser = get_args()
args = parser.parse_args()
ip_addresses = []
if args.ips:
ip_addresses += args.ips.split(',')
if args.filename:
with open(args.filename, 'r') as f:
for line in f.readlines():
ip_addresses.append(line.strip())
if not ip_addresses:
print "No IP addresses specified\n"
print parser.print_help()
sys.exit(1)
pages = 1 if args.pages < 1 else args.pages
for ip_address in ip_addresses:
urls = []
base = 'https://www.bing.com/search?q=ip%3a'+ip_address
tree = search(base,urls)
counter = 2
while counter <= pages:
next_page = tree.xpath("//a[@aria-label='Page {0}']/@href".format(counter))
if not next_page:
break
result_index = re.findall('&first=[0-9][0-9]*',next_page[0])[0].split('=')[1]
tree = search(base+'&first='+result_index,urls)
counter += 1
print ip_address
valid_net = []
urls = list(set(urls))
for url in urls:
scheme = re.findall('^http(s|)://', url)
if not scheme:
url = 'http://' + url.split()[0]
url = urlparse(url)
ps = subprocess.Popen(('host', url.netloc), stdout=subprocess.PIPE)
out = subprocess.check_output(('awk',"/has address/ {print $4}"), stdin=ps.stdout)
ps.wait()
if ip_address == out.strip():
valid_net.append(url.netloc)
print '\n'.join(list(set(valid_net)))
print ''