initial commit
This commit is contained in:
commit
9331bbe246
15 changed files with 314 additions and 0 deletions
36
src/ip.py
Normal file
36
src/ip.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import subprocess
|
||||
|
||||
SERVERS = [
|
||||
("resolver1.opendns.com", "myip.opendns.com"),
|
||||
("resolver2.opendns.com", "myip.opendns.com"),
|
||||
("resolver3.opendns.com", "myip.opendns.com"),
|
||||
("resolver4.opendns.com", "myip.opendns.com"),
|
||||
("ns1-1.akamaitech.net", "whoami.akamai.net"),
|
||||
]
|
||||
|
||||
|
||||
def get_public_ip():
|
||||
for server, host in SERVERS:
|
||||
try:
|
||||
result = subprocess.run(
|
||||
[
|
||||
"dig",
|
||||
"+short",
|
||||
host,
|
||||
f"@{server}",
|
||||
],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True,
|
||||
timeout=5,
|
||||
)
|
||||
|
||||
ip = result.stdout.strip()
|
||||
|
||||
if ip:
|
||||
return ip
|
||||
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
raise RuntimeError("cannot determine public IP")
|
||||
Loading…
Add table
Add a link
Reference in a new issue