LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-23-2008, 03:55 PM   #1
adymroxx
Member
 
Registered: Mar 2005
Location: Iowa
Distribution: Fedora Core 9
Posts: 41

Rep: Reputation: 15
Discover and Parse Active IP Addresses


Is there a way to easily discover active hosts on a subnet and parse their IPs into a file? I'm thinking about a bash script which follows the following psuedo code:
Code:
Obtain system IP through ifconfig and parsing out the IP using sed
Ping sweep subnet using nmap
Parse through output, looking for IP addresses returned
Add IPs to an array or CSV file
I'm just now learning about regex and I'm having trouble parsing the IP addresses. Is there an easy way to do this? Is there some much more obvious way of discovering active IPs that I'm just not thinking of?

Thanks!
 
Old 07-23-2008, 04:45 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Have you considered using nmap?
 
Old 07-23-2008, 05:10 PM   #3
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Large problems are made easier by breaking them into smaller problems.

Quote:
Ping sweep subnet using nmap
Read the manpages for nmap(1). Search for ping sweep / ping scan.

Quote:
Parse through output, looking for IP addresses returned
Actually look at the output from nmap. You can't parse anything until you understand the data and its format.

Also, there are many ways to use regular expressions to match an IP address (some are more precise than others). I'd start by searching the forums and google.
 
Old 07-23-2008, 05:11 PM   #4
adymroxx
Member
 
Registered: Mar 2005
Location: Iowa
Distribution: Fedora Core 9
Posts: 41

Original Poster
Rep: Reputation: 15
Well, nmap is part of it, but it returns something like this:
Code:
[root@redshirt ~]# nmap -sP 192.168.1.0/24

Starting Nmap 4.53 ( http://insecure.org ) at 2008-07-22 22:49 CDT
Host boxA (192.168.1.100) appears to be up.
MAC Address: 00:11:22:33:44:55 (Cisco-Linksys)
Host boxB (192.168.1.105) appears to be up.
MAC Address: 00:21:12:34:43:01 (Tivo)
etc.
.
.
My question is on how to yank those IP addresses out of there so I can put them into an array or CSV.
 
Old 07-23-2008, 05:12 PM   #5
adymroxx
Member
 
Registered: Mar 2005
Location: Iowa
Distribution: Fedora Core 9
Posts: 41

Original Poster
Rep: Reputation: 15
The regex guide I've been working out of is at http://regular-expressions.info and they have an example for matching IPs, listed as
Code:
\b(?:\d{1,3}\.){3}\d{1,3}\b
but this doesn't seem to work...
 
Old 07-23-2008, 05:33 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Quote:
Originally Posted by adymroxx View Post
The regex guide I've been working out of is at http://regular-expressions.info and they have an example for matching IPs, listed as
Code:
\b(?:\d{1,3}\.){3}\d{1,3}\b
but this doesn't seem to work...
Give some context - "doesn't seem to work" in what?
 
Old 07-23-2008, 05:55 PM   #7
adymroxx
Member
 
Registered: Mar 2005
Location: Iowa
Distribution: Fedora Core 9
Posts: 41

Original Poster
Rep: Reputation: 15
Code:
# nmap -sP 192.168.1.0/24 | grep '\b(?:\d{1,3}\.){3}\d{1,3}\b'
Returns nothing. I realize though that by using grep, I would be returned the lines that contain the IPs rather than just the IPs. Is there a way to get just the IPs?
 
Old 07-23-2008, 06:16 PM   #8
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Grep doesn't understand PCREs. Use perl if you want those:

Code:
perl -ne 'print "$1\n" if /(\b(?:\d{1,3}\.){3}\d{1,3})\b/'
For grep, you must simplify and use character classes (vs. \d):

Code:
grep -oE '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'
 
Old 07-23-2008, 10:10 PM   #9
adymroxx
Member
 
Registered: Mar 2005
Location: Iowa
Distribution: Fedora Core 9
Posts: 41

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Mr. C. View Post
Grep doesn't understand PCREs. Use perl if you want those:

Code:
perl -ne 'print "$1\n" if /(\b(?:\d{1,3}\.){3}\d{1,3})\b/'
For grep, you must simplify and use character classes (vs. \d):

Code:
grep -oE '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'
That did it. Thanks!
 
  


Reply



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
Binding 2 NICs (MAC addresses) to 2 IP Addresses in same Subnet RedHat EL4.0 skhira Linux - Networking 13 02-24-2008 08:16 PM
Binding 2 NICs (MAC addresses) to 2 IP Addresses in same Subnet RedHat EL4.0 skhira Linux - Networking 1 02-09-2008 07:17 AM
Parse error: parse error, unexpected $ in /home/content/d/o/m/domain/html/addpuppy2.p Scooby-Doo Programming 3 10-25-2007 09:41 AM
mechanics of mapping process memory addresses to physical addresses on amd64 Tischbein Linux - Kernel 2 02-01-2007 08:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:45 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
Open Source Consulting | Domain Registration