LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-20-2014, 06:11 AM   #1
dappa
LQ Newbie
 
Registered: Aug 2011
Posts: 15

Rep: Reputation: Disabled
Question extract IP from secure log


Hi All,

I need to extract every ip only once from the /var/log/secure. I would like to keep the username and IP address and also put a count on how many times it was used.

sample from the log

Jun 15 05:08:19 dropbox sshd[17365]: Accepted publickey for user_name from xxx.xxx.xxx.xxx port xxxx ssh2
 
Old 06-20-2014, 07:47 AM   #2
dappa
LQ Newbie
 
Registered: Aug 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
import sys
import re

if sys.argv[1:]:
f = open(sys.argv[1], 'r')
data = f.read()
f.close()
else:
print 'Please provide a log file'
sys.exit()

pattern = r'\b(??:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b'
ips = {}
for match in re.findall(pattern, data):
count = ips.get(match)
if count:
ips.update({match: count+1})
else:
ips.update({match: 1})

print 'IP\t\t\tCount'
for ip, count in ips.items():
print '%s\t\t%s' % (ip, count)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract text from messages log brianmcgee Programming 1 09-17-2008 09:35 AM
Can Samhain log my entries in /var/log/secure and /var/log/mesage to a central server abefroman Linux - Software 2 04-13-2008 05:13 PM
Extract SU attempts to separate log beammeup Linux - Security 4 09-01-2007 10:01 AM
Extract log FROM - TO with shell commands overpeer Linux - General 1 03-13-2006 12:00 PM
how to extract certain lines from a log file Avatar Linux - Newbie 3 02-11-2005 10:51 AM


All times are GMT -5. The time now is 07:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration