Skip to content

Instantly share code, notes, and snippets.

@mnemocron
Created June 21, 2018 12:01
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 mnemocron/0bae0f561396f4bfe64ca92afdac6ae7 to your computer and use it in GitHub Desktop.
Save mnemocron/0bae0f561396f4bfe64ca92afdac6ae7 to your computer and use it in GitHub Desktop.
Script that compares the first input argument (the domain) to the current external IP. If they do not match it attempts to send a Telegram message.
#!/usr/bin/python
# python 2.7
#_*_ coding: utf-8 _*_
"""
@file dynip-alert.py
@author Simon Burkhardt - simonmartin.ch
@date 2018-03-01
@version 1.1
@copyright CC0 - https://creativecommons.org/publicdomain/zero/1.0
@brief compare dns ip of given domain against the public ip of the host and send a telegram message
@details none
@note originally to compare two dns entries against each other
"""
# dynip-alert [domain] [telegram-name]
try:
import sys
import os
except:
print ("error: library missing")
exit()
# check argument length
if ( len(sys.argv) < 3 ):
print ( "error: too few arguments" )
exit()
domain_ns = sys.argv[1]
#domain_dyn = sys.argv[2]
# main code
try:
resp_ip_ns = os.popen("nslookup " + domain_ns + " | grep Address").read()
resp_ip_dyn = os.popen("curl -s checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//'").read()
# resp_ip_dyn = os.popen("nslookup " + domain_dyn + " | grep Address").read()
# print resp_ip_dyn
if (resp_ip_ns.count("Address") < 2) :
print ("error: could not find name")
exit()
else :
ip_ns = resp_ip_ns.split("\n")[1].replace("Address: ", "")
ip_dyn = resp_ip_dyn
if not (ip_ns in ip_dyn):
message = "DYN-DNS Alert\nthe resolved IP addresses do not match!\n" + domain_ns + "\t" + ip_ns + "\nExternal IP \t" + ip_dyn
print(message)
# os.system("telegram-send " + sys.argv[2] + " \"" + message.replace("\"", "\\\"").replace("'", "\\\'") + "\"")
os.system("telegram-bot -u " + sys.argv[2] + " -t \"" + message.replace("\"", "\\\"").replace("'", "\\\'") + "\"")
else:
print("the ip addresses match: " + ip_ns)
# enables abortion of the program by CTRL + C
except KeyboardInterrupt:
print("")
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment