Skip to content

Instantly share code, notes, and snippets.

@ian-weisser
Last active November 4, 2020 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ian-weisser/dced876cf165844feff3fb3b36abee16 to your computer and use it in GitHub Desktop.
Save ian-weisser/dced876cf165844feff3fb3b36abee16 to your computer and use it in GitHub Desktop.
Python3 - Am I on the right network?
#!/usr/bin/python3
import subprocess
def right_network (fqdn):
"""
# Example of using 'dig' to find the IP address of a network IP address
$ dig -4 +short www.example.com
24.209.187.117
Example of using 'dig' to find the current public IP address
$ dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com
"24.209.187.117"
This function returns True if you are on the 'fqdn" network.
It returns false if you are on any other network
Example:
>>>print(right_network("ddns.example.com"))
>>>True
"""
output = subprocess.run(["dig", "-4", "+short", fqdn ], capture_output=True)
network_ip = output.stdout.decode('utf-8').strip('\n')
output = subprocess.run(["dig", "-4", "TXT", "+short", "o-o.myaddr.l.google.com", "@ns1.google.com" ], capture_output=True)
my_ip = output.stdout.decode('utf-8').strip('"\n')
#print(store, my_ip)
if network_ip == my_ip:
return True
return False
network_fqdn = "ddns.example.com"
print(right_network(network_fqdn))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment