Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 06-20-2014, 05:11 AM   #1
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 port xxxx ssh2
Old 06-20-2014, 06:47 AM   #2
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 =
print 'Please provide a log file'

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})
ips.update({match: 1})

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


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 08: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 04:13 PM
Extract SU attempts to separate log beammeup Linux - Security 4 09-01-2007 09:01 AM
Extract log FROM - TO with shell commands overpeer Linux - General 1 03-13-2006 11:00 AM
how to extract certain lines from a log file Avatar Linux - Newbie 3 02-11-2005 09:51 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:55 AM.

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