35 lines
1 KiB
Python
35 lines
1 KiB
Python
|
from typing import Optional
|
||
|
import subprocess
|
||
|
|
||
|
def query_txt(domain: str, fallback_ns:str='8.8.8.8') -> Optional[str]:
|
||
|
# get the authoritative nameserver first
|
||
|
# just the top domain
|
||
|
top_domain = '.'.join(domain.split('.')[-2:])
|
||
|
ns_response = subprocess.run(['dig', top_domain, 'ns', '+short'], capture_output=True)
|
||
|
# get the first one
|
||
|
ns_response = ns_response.stdout.decode('utf-8')
|
||
|
if ns_response == '':
|
||
|
ns = fallback_ns
|
||
|
else:
|
||
|
ns = ns_response.split('\n')[0]
|
||
|
|
||
|
# now actually query for the txt record
|
||
|
output = subprocess.run(['dig', f'@{ns}', domain, 'txt', '+short'], capture_output=True)
|
||
|
record = output.stdout.decode('utf-8').strip()
|
||
|
if record == '':
|
||
|
return None
|
||
|
else:
|
||
|
return record
|
||
|
|
||
|
def find_last_int(domain: str, max=50) -> int:
|
||
|
post_int = 1
|
||
|
|
||
|
while post_int < max:
|
||
|
test_domain = f'{post_int}.{domain}'
|
||
|
txt = query_txt(test_domain)
|
||
|
if txt is None:
|
||
|
break
|
||
|
post_int += 1
|
||
|
|
||
|
return post_int
|