Advertisement
Guest User

Untitled

a guest
Mar 25th, 2012
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1.   GNU nano 1.3.12                                       File: dl_handler.py                                                                                    
  2.  
  3. #!/usr/bin/env python26
  4. # -*- coding: utf-8 -*-
  5. """snmp handler for d-link 3028 ver 0.1"""
  6. import re
  7. import sys
  8. import string
  9. import MySQLdb
  10. db = MySQLdb.connect("localhost","login","pass","basename")
  11. cursor = db.cursor()
  12. data =  sys.stdin.read()
  13. to_str = data
  14. ip_switch = re.findall('10.40.[0-9]{1,3}.[0-9]{1,3}',  to_str)
  15. ips = ip_switch[0]
  16. mac_port_type = re.findall('".*?"',  to_str)
  17. pmt = mac_port_type[0][1:-1].replace(" ",'')
  18. change_type =  pmt[:2]
  19. mac_address = pmt[2:-6]
  20. port = int(pmt[16:-2],16)
  21. to_sql =  {'a':ips, 'b':change_type,'c':mac_address,'d':port}
  22. sql = """INSERT INTO main(SWITCH_IP,
  23.         CLIENT_MAC, CLIENT_PORT, CHANGE_TYPE)
  24.         VALUES ('%(a)s', '%(c)s', '%(d)s', '%(b)s')"""% to_sql
  25. try:
  26.    cursor.execute(sql)
  27.    db.commit()
  28. except:
  29.    db.rollback()
  30. db.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement