-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomaintoipmap.py
62 lines (47 loc) · 1.57 KB
/
domaintoipmap.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
61
62
import argparse
import socket
def read_file(filename):
hosts = []
try:
with open(filename) as f:
hosts = f.readlines()
except Exception as e:
print("Error reading input file: {0}".format(e))
return hosts
def write_output(map, output_file, ip_file):
try:
with open(output_file, 'w') as m, open(ip_file, 'w') as i:
for item in map:
m.write(item["host"] + "," + item["ip"] + "\n")
if item["ip"] != "":
i.write(item["ip"] + "\n")
except Execption as e:
print("Error writing output: {0}".format(e))
def fetch_ips(hosts):
mapping = []
for host in hosts:
host = host.rstrip()
print("Resolving host: {0}".format(host), end="... ")
try:
s = socket.gethostbyname(host)
mapping.append({"host": host, "ip": s})
print(s)
except:
mapping.append({"host": host, "ip": ""})
print("failed")
continue
return mapping
def main():
parser = argparse.ArgumentParser(prog="Parse hosts for ips", description="Output two files, one with hosts and IPs and one with just IPs")
parser.add_argument('-i', '--input', help="Full path to the input file")
parser.add_argument('-oM', '--map', help="Full path to the output file for subdomains")
parser.add_argument('-oI', '--ips', help="Full path to the output file for IPs")
args = parser.parse_args()
if args.input is None or args.map is None or args.ips is None:
print("Please supply the required arguments: -i INPUT.txt -oM OUTPUT_MAP.txt -oI OUTPUT_IP.txt")
exit()
hosts = read_file(args.input)
mapping = fetch_ips(hosts)
write_output(mapping, args.map, args.ips)
if __name__ == '__main__':
main()