Skip to content

Instantly share code, notes, and snippets.

@twr14152
Last active August 29, 2015 14:14
Show Gist options
  • Save twr14152/89bb69f00021eb04c5f0 to your computer and use it in GitHub Desktop.
Save twr14152/89bb69f00021eb04c5f0 to your computer and use it in GitHub Desktop.
Applied Python - Week 2 Exercises
# Applied Python
# Class 2 exercise 1
#
import snmp_helper
import time
IP = '#.#.#.#'
a_user = '####'
auth_key = '####'
encrypt_key = '####'
snmp_user = (a_user, auth_key, encrypt_key)
pynet_rtr2 = (IP, 8061)
sys_name = '1.3.6.1.2.1.1.5.0'
RunConfChange = '1.3.6.1.4.1.9.9.43.1.1.1.0'
snmp_rtr_name = snmp_helper.snmp_get_oid_v3(pynet_rtr2, snmp_user, oid=sys_name)
snmp_data = snmp_helper.snmp_get_oid_v3(pynet_rtr2, snmp_user, oid=RunConfChange)
localtime = time.asctime( time.localtime(time.time()) )
print "Local time:", localtime
output = int(snmp_helper.snmp_extract(snmp_data))
rtr_name = snmp_helper.snmp_extract(snmp_rtr_name)
print "Global - %s running config last changed: %s" % (rtr_name, output)
# Initial time value variable is created to use in the while loop
CurrentRunTime = int(output)
###Email Function Kirk provided in his email to the class
def send_mail(recipient, subject, message, sender):
'''
Simple function to help simplify sending SMTP email
Assumes a mailserver is available on localhost
'''
import smtplib
from email.mime.text import MIMEText
message = MIMEText(message)
message['Subject'] = subject
message['From'] = sender
message['To'] = recipient
# Create SMTP connection object to localhost
smtp_conn = smtplib.SMTP('localhost')
# Send the email
smtp_conn.sendmail(sender, recipient, message.as_string())
# Close SMTP connection
smtp_conn.quit()
return True
# Current time is the value pulled initially. As long as output and CurrentRunTime equal while loop will keep running
# Once router config is changed the values will not be equal and the else block goes into effect
while (output == CurrentRunTime):
snmp_data = snmp_helper.snmp_get_oid_v3(pynet_rtr2, snmp_user, oid=RunConfChange)
snmp_rtr_name = snmp_helper.snmp_get_oid_v3(pynet_rtr2, snmp_user, oid=sys_name)
output = int(snmp_helper.snmp_extract(snmp_data))
rtr_name = snmp_helper.snmp_extract(snmp_rtr_name)
print "while loop - %s running config last changed: %s" % (rtr_name, output)
time.sleep(10)
# This event will triggers the email with the output timers and system included.
else:
localtime = time.asctime( time.localtime(time.time()) )
print "Local time:", localtime
rtr_name = snmp_helper.snmp_extract(snmp_rtr_name)
output = int(snmp_helper.snmp_extract(snmp_data))
print '''else clause - %s running configuration just changed.
Local time: %s
Time pulled on router was: %s
''' % (rtr_name, localtime, output)
time.sleep(0)
recipient = '[email protected]'
subject = 'Test message'
message = ''' %s running configuration just changed.
Local time: %s
Time pulled on router: %s
''' % (rtr_name, localtime, output)
sender = '[email protected]'
send_mail(recipient, subject, message, sender)
@twr14152
Copy link
Author

Updated with:
localtime = time.asctime( time.localtime(time.time()) )
print "Local time:", localtime

Removed: time.localtime()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment