-
Notifications
You must be signed in to change notification settings - Fork 0
/
host_resolver.py
executable file
·43 lines (34 loc) · 1.21 KB
/
host_resolver.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
#!/usr/bin/env python
# coding: utf-8
#
# By Leon Johnson - twitter.com/sho_luv
#
# This program resolves hostnames in parallel,
# using multiple threads to improve performance.
import argparse
import socket
import threading
def resolve_hostname(hostname):
try:
ip_address = socket.gethostbyname(hostname)
print(f"{hostname} resolved to {ip_address}")
except socket.error as e:
print(f"{hostname} failed to resolve: {e}")
def main():
parser = argparse.ArgumentParser(description="Resolve hostnames in parallel")
parser.add_argument("filename", help="file containing hostnames, one per line")
parser.add_argument("-t", "--threads", type=int, default=10, help="number of threads to use")
args = parser.parse_args()
with open(args.filename) as f:
hostnames = f.read().splitlines()
threads = []
for hostname in hostnames:
thread = threading.Thread(target=resolve_hostname, args=(hostname,))
threads.append(thread)
for i in range(0, len(threads), args.threads):
for thread in threads[i:i+args.threads]:
thread.start()
for thread in threads[i:i+args.threads]:
thread.join()
if __name__ == "__main__":
main()