External IP Check

Overview

This script will check the external IP and warn if it changes.

check_external_ip.py

#!/usr/bin/pythonfrom urllib2 import Request, urlopen, URLError, HTTPErrorfrom sys import exit url = Request("http://www.icanhazip.com")try:    external_ip_address = urlopen(url).read().rstrip('\n')except HTTPError, e:     print "Server couldn\'t fulfill the request. Error code: %s" % e.code     exit(3)except URLError, e:     print "Failed to reach server. Reason: %s" % e.reason     exit(3) try:    open('/tmp/external_ip_address')except IOError:    f = open('/tmp/external_ip_address','w')    f.write(external_ip_address)    f.close    print "WARNING - New File Created with IP Address: %s" % external_ip_address    exit(1)else:    f = open('/tmp/external_ip_address','r+')    current_ip_address = f.read()    f.close    if current_ip_address == external_ip_address:         print "OK - Current IP Address: %s" % external_ip_address         exit(0)    else:         f = open('/tmp/external_ip_address','w')         f.write(external_ip_address)         f.close()         print "CRITICAL - IP Address has changed to %s" % external_ip_address         exit(2)

Thanks to Robert V Bolton for this script: http://blog.robertvbolton.com/nagios-plugin-check-external-ip-version-2-0