pastebin - collaborative debugging tool
kpaste.net RSS


pythons!
Posted by Anonymous on Sat 1st Dec 2012 22:14
raw | new post

  1. import os, gzip, socket, pickle, re, shutil, cPickle
  2. shutil.copy2('/home/pronto/logs/ssh_fails.p', '/home/pronto/logs/ssh_fails.old.p')
  3. def openfile(logfile):
  4.     if 'gz' in logfile:
  5.         celery = gzip.open(logfile, 'r')
  6.     else:
  7.         celery = open(logfile, 'r')
  8.     return celery
  9. ip_count=[]
  10. for files in os.listdir('/var/log/'):
  11.     if 'auth.log' in files:
  12.         logfile="/var/log/"+files
  13.         celery=openfile(logfile)
  14.         for line in celery:
  15.             if "Failed" in line:
  16.                 ip = re.findall( r'[0-9]+(?:\.[0-9]+){3}', line)
  17.                 if ip:
  18.                     if [x[0] for x in ip_count if x[0] == ip[0]]:
  19.                         index=[x[0] for x in ip_count].index(ip[0])
  20.                         count= ip_count[index][1]
  21.                         count += 1
  22.                         ip_count[index] = (ip[0],count, ip_count[index][2], ip_count[index][3])
  23.                     elif not [x[0] for x in ip_count if x[0] == ip[0]]:
  24.                         #type(ip)
  25.                         try:
  26.                             host = socket.gethostbyaddr(ip[0])[0]
  27.                         except:
  28.                             host = None
  29.                         ip_count.append((ip[0], 1, host , 0))
  30.  
  31. ip_count.sort(key=lambda tup: tup[1], reverse=True)
  32. old_list=cPickle.load(open("/home/pronto/logs/ssh_fails.old.p","rb"))
  33. for ip in [x[0] for x in ip_count]:
  34.     try:
  35.         index_new=[x[0] for x in ip_count].index(ip)
  36.         index_old=[x[0] for x in old_list].index(ip)
  37.         #print "ip: " + str(ip) + " attempts: " + str(ip_count[index_new][1])
  38.         #print "ip: " + old_list[index_old][0] + " attempts: " + str(old_list[index_old][1])
  39.         #print "==================="
  40.         if old_list[index_old][1] != ip_count[index_new][1]:
  41.             att_diff = old_list[index_old][1] - ip_count[index_new][1]
  42.             print "omg diff: " + str(att_diff)
  43.             ip_count[index_new]=(ip_count[index_new][0], ip_count[index_new][1], ip_count[index_new][2], att_diff)
  44.     except:
  45.         pie="pie"
  46.  
  47. pickle.dump(ip_count,open("/home/pronto/logs/ssh_fails.p", "wb"))

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at