## ********** WARNING: ********** ## This script is provided free on an as-is basis. ## ## Use at your own risk. We assume no responsibility for damages or losses ## related to the use of this script, nor do we offer any guarantee that this ## script will perform as expected, even under ideal laboratory conditions ## in a sealed dustproof room staffed with engineers in immaculate white lab coats. ''' by Peter Dines peterd@spe-edi.com Quickie script to check for bTrade AS2 server availability and email an admin if it is down Usage: assuming Windows, call from task scheduler hourly after setting up the mail variables... you may want to create a batch file to ensure that the path to the Python interpreter is called from the right directory, and calls this script from the right directory. tested with Python 2.3.1 but will probably work with any 2.X Python release ''' import time import smtplib import urllib ## _____START VARIABLE SETUP_____ ## ## you must change these variables ## to your own email, AS2 server address etc. ## this is the address of your AS2 server ## we're assuming you're using port 8080 with no ## connection encryption, a la Wal-Mart testPage = 'http://as2server.url:port' mailServer = 'pop.mailserver.com' fromAddress = 'AS2Alert@youremail.com' toAddress = 'edi.contact@youremail.com' subject = 'AS2 server alert: ' + testPage ## This is the string to search for ## write it in ALL CAPS so we're comparing apples and apples searchString = 'BTRADE' ## _____END VARIABLE SETUP_____ ## ## don't edit below here unless you can program in Python! ## try to retrieve the default server page, convert to all caps, ## see if it contains our test string try: testString = urllib.urlopen(testPage).read() except: ## couldn't get any string, so email will be sent testString = '' if testString.upper().find(searchString) >= 0: ## do nothing if the server is fine pass else: ## this creates a time and date stamped email body body = 'AS2 server ' + testPage + ' offline at ' + time.ctime(time.time()) ## send an email to the admin msg = 'From: ' + fromAddress + '\r\n' + 'To: '+ toAddress + '\r\n'\ + 'Subject: ' + subject + '\r\n\r\n' + body Outbox = smtplib.SMTP(mailServer) Outbox.sendmail(fromAddress, toAddress, msg) Outbox.quit() print('message sent')