Skip to content

Instantly share code, notes, and snippets.

@spookyahell
Last active August 19, 2019 19:21
Show Gist options
  • Select an option

  • Save spookyahell/b65f98b39b55bc439865db8dc0801035 to your computer and use it in GitHub Desktop.

Select an option

Save spookyahell/b65f98b39b55bc439865db8dc0801035 to your computer and use it in GitHub Desktop.
Finds IPs for DNS name if randomly changes - most credit to @nyuszika7h
#!/usr/bin/env python3
import argparse
import logging
import random
import socket
import sys
import _thread
import threading
import time
import os
import requests
def handle_timeout():
logger.info(f'No new IPs found in {args.timeout} seconds, exiting')
_thread.interrupt_main()
parser = argparse.ArgumentParser(prog='gettgips')
parser.add_argument(
'filename',
help='file to write IPs to',
)
parser.add_argument(
'hostnames',
help='comma-separated list of hostnames to look up',
)
parser.add_argument(
'-d',
'--delay',
type=float,
default=0.2,
help='seconds between lookups',
)
parser.add_argument(
'-s',
'--host-suffix',
default='.torguardvpnaccess.com',
help='suffix to append after the listed hostnames',
)
parser.add_argument(
'-gi',
'--getIPinfo',
action = 'store_true',
help='suffix to append after the listed hostnames',
)
parser.add_argument(
'-t',
'--timeout',
type=int,
default=300,
help='exit if no new IPs found after this many seconds',
)
parser.add_argument(
'--debug',
action='store_true',
help='enable debug logging',
)
args = parser.parse_args()
logging.basicConfig(
level=logging.DEBUG if args.debug else logging.INFO,
format='[%(asctime)s.%(msecs)03d] %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
)
logger = logging.getLogger()
try:
fd = open(args.filename, 'a+')
fd.seek(0)
ips = [x for x in fd.read().split('\n') if x]
logger.info(
f'Loaded {len(ips)} known IP{"" if len(ips) == 1 else "s"} from {args.filename}',
)
if args.timeout > 0:
logger.debug(f'Starting timer ({args.timeout}s)')
timer = threading.Timer(args.timeout, handle_timeout)
timer.start()
hostnames = args.hostnames.split(',')
n = len(hostnames)
logger.info(f'Testing {n} hostname{"" if n == 1 else "s"}')
while True:
host = random.choice(hostnames)
host += args.host_suffix
#~ print(host)
ip = socket.gethostbyname(host)
logger.debug(f'gethostbyname({host}) => {ip}')
if ip not in ips:
ips.append(ip)
fd.write(f'{ip}\n')
fd.flush()
logger.info(f'Found new IP #{len(ips)}: {ip} ({host})')
if args.getIPinfo:
r = requests.get(f'https://ipinfo.io/{ip}/json')
info = r.json()
os.makedirs('IPinfo', exist_ok = True)
ipffn = ip.replace('.','-')
with open(f'IPinfo{os.sep}\{ipffn}-info.json','w') as f:
f.write(r.text)
city = info['city']
region = info['region']
country = info['country']
loc = info['loc']
org = info['org']
postal = info['postal']
IPtext = f'Country: {country}\nCity: {city}\n'
if region != city:
IPtext += f'Region: {region}\n'
IPtext += f'Coordinates: {loc}\nISP: {org}\nPostal: {postal}'
logger.info(f'Following details are known about the IP:\n{IPtext}')
if args.timeout > 0:
logger.debug('Cancelling timer')
timer.cancel()
t = args.timeout + args.delay
logger.debug(f'Starting timer ({t}s)')
timer = threading.Timer(t, handle_timeout)
timer.start()
time.sleep(args.delay)
except KeyboardInterrupt:
sys.exit(0)
finally:
fd.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment