LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 09-15-2014, 03:06 PM   #1
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Rep: Reputation: 49
How to generate list of unique IPs out of access_log


I'm looking at an access_log for an old lampp install, where all the lines in it are similar to this

Code:
12.34.56.78 - - [15/Sep/2014:15:48:09 -0400] "GET /dir/file.png HTTP/1.1" 200 41219
Is there a clever command I could run to just give me a list of all the unique IP addresses in it?

I'm trying to kill this old server, so I want to know which machines i need to visit to point to the new server, and this list would save me a lot of time.
 
Old 09-15-2014, 03:36 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
Code:
grep -o "^[^ ]*" access_log | sort -nu
 
1 members found this post helpful.
Old 09-15-2014, 03:57 PM   #3
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
Thanks. That only lists the first IP in the file it appears though. Did I maybe re-type it wrong?
 
Old 09-15-2014, 09:49 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Do a search on how to use GNU sort to sort IP addresses. Simple fix.
 
Old 09-15-2014, 10:06 PM   #5
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Assuming that your log file is called logfile,.. then you could do something like this:

Code:
grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' logfile > only_ips.unformatted
uniq only_ips.unformatted > only_ips.uniq
sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 only_ips.uniq > only_ips.sorted
Which I cobbled together after I became frustrated trying to use awk. It's generally less expensive processor/memory wise if you can streamline these kinds of things into one program, but I'm having a dumb and can't brain.

For a good explanation of the sort, see here: https://www.madboa.com/geek/sort-addr/
 
1 members found this post helpful.
Old 09-16-2014, 09:04 AM   #6
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
Thanks sz. Looks like there's a couple dupes in here, but this list is a TON shorter and will work perfectly for what I need. Thanks a lot!
 
Old 09-16-2014, 09:25 AM   #7
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
No problem. I wonder why there would be dupes...
 
1 members found this post helpful.
Old 09-16-2014, 09:26 AM   #8
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
There's only a couple of each, which is better than the 100s of each in the original log file. no complaints here!
 
Old 09-16-2014, 09:32 AM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Quote:
Originally Posted by szboardstretcher View Post
No problem. I wonder why there would be dupes...
Uniq requires sorted input - better to use "sort -nu" as per the earlier response.
 
  


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
[SOLVED] Create populated files with unique file names from a list geokker Linux - Server 6 05-16-2012 03:50 PM
counting the number of unique items in a list UnixKiwi Programming 4 11-29-2008 02:49 PM
pls help on how i can generate 4 digit random number that are unique within the set icylicious Programming 7 08-22-2006 01:00 PM
i found a way to random generate 4 unique numbrs but prob is sometimes it doesn't run icylicious Programming 1 08-21-2006 06:35 AM
generate the update list sunhui Linux - Software 0 02-17-2006 04:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 03:55 PM.

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
Open Source Consulting | Domain Registration