#!/usr/bin/python # Auteur : Belgotux # Site : www.monlinux.net # Adresse : belgotux@monlinux.net # Version : 1.0 # Date : 10-03-2015 # Licence : GPLv3 # Description : check the public IP address of the connexion for Nagios/Centreon from urllib import urlopen import re,sys #INSERT HERE YOUR IP HERE IF YOU NOT USE ARGUMENTS myip = "1.2.3.4" #inactif right now http://curlmyip.com sites_list = ('http://curlmyip.com','http://icanhazip.com','http://ifconfig.me/ip','http://myip.monlinux.net/') # Return codes expected by Nagios/Centreon OK = 0 WARNING = 1 CRITICAL = 2 ip='None' #MAIN if (len(sys.argv)==2): myip = sys.argv[1] if (len(sys.argv)>2): print "Only put one argument (or not) : the expected IP" raise SystemExit, 4 for site in sites_list: try: ip = urlopen(site).read() p=re.match('([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}|(\d{1,3}\.){3}\d{1,3}', ip) if p: break except IOError: continue if ip == myip+'\n': print "CheckPublicIP: OK - public ip from site %r : %r" % (site,str(ip)[:-1]) raise SystemExit, OK elif re.match('([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}|(\d{1,3}\.){3}\d{1,3}', ip): print 'CheckPublicIP: Warning - not on the right public IP %r ! Maybe you are on your failover Gateway - public IP: %r' % (myip,str(ip)[:-1]) raise SystemExit, WARNING else: print "CheckPublicIP: Critical - Internet connection seems to have a problem!" raise SystemExit, CRITICAL