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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-23-2008, 03:55 PM
|
#1
|
Member
Registered: Mar 2005
Location: Iowa
Distribution: Fedora Core 9
Posts: 41
Rep:
|
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!
|
|
|
07-23-2008, 04:45 PM
|
#2
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
Have you considered using nmap?
|
|
|
07-23-2008, 05:10 PM
|
#3
|
Senior Member
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Rep: 
|
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.
|
|
|
07-23-2008, 05:11 PM
|
#4
|
Member
Registered: Mar 2005
Location: Iowa
Distribution: Fedora Core 9
Posts: 41
Original Poster
Rep:
|
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.
|
|
|
07-23-2008, 05:12 PM
|
#5
|
Member
Registered: Mar 2005
Location: Iowa
Distribution: Fedora Core 9
Posts: 41
Original Poster
Rep:
|
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...
|
|
|
07-23-2008, 05:33 PM
|
#6
|
Senior Member
Registered: Jun 2008
Posts: 2,529
Rep:
|
Quote:
Originally Posted by adymroxx
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?
|
|
|
07-23-2008, 05:55 PM
|
#7
|
Member
Registered: Mar 2005
Location: Iowa
Distribution: Fedora Core 9
Posts: 41
Original Poster
Rep:
|
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?
|
|
|
07-23-2008, 06:16 PM
|
#8
|
Senior Member
Registered: Jun 2008
Posts: 2,529
Rep:
|
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}'
|
|
|
07-23-2008, 10:10 PM
|
#9
|
Member
Registered: Mar 2005
Location: Iowa
Distribution: Fedora Core 9
Posts: 41
Original Poster
Rep:
|
Quote:
Originally Posted by Mr. C.
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!
|
|
|
All times are GMT -5. The time now is 06:30 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|