LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-23-2013, 03:07 PM   #1
grahamb314
LQ Newbie
 
Registered: Apr 2011
Posts: 16

Rep: Reputation: Disabled
Lightbulb Ping all the IP addresses in a file


I have a file that looks like this:

Code:
 1:  192.168.1.1 (192.168.1.1)                      0.691ms
 1:  192.168.1.2 (192.168.1.2)                      0.279ms
 2:  12.213.12.123 (12.213.12.123)                          1.385ms
 3:  45.213.12.221 (45.213.12.221)                         11.334ms
I want to run a script which will ping all the ip addresses in the brackets, one at a time and save to a file.

I know how to ping and save to a file, but I dont know how I can get the IP addresses from this file into an array or do some kind of regex loop?

Thanks for your help
 
Old 08-23-2013, 03:40 PM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

You may try something like this:
Code:
$ while  IFS='()' read _ IP _; do echo IP=$IP; done < in
IP=192.168.1.1
IP=192.168.1.2
IP=12.213.12.123
IP=45.213.12.221
where `in' -- is the input file.
 
Old 09-04-2013, 06:21 AM   #3
bonnydeal
Member
 
Registered: Feb 2006
Posts: 47

Rep: Reputation: 29
To get all the ip addresses
Code:
awk '{print $2}' $INPUTFILE
You could then put the output in a loop
Code:
awk '{print $2}' $INPUTFILE | while read ip
do
     ping -c 3 $ip
done
don't forget the -c to specify a ping count...
I suggest you specify a max wait time too.

Last edited by bonnydeal; 09-04-2013 at 06:23 AM. Reason: updated
 
Old 09-05-2013, 06:32 PM   #4
hybby
LQ Newbie
 
Registered: Jun 2013
Location: scotland
Distribution: centos
Posts: 2

Rep: Reputation: 0
in perl. also selects the value in the brackets just for fun

Code:
#!/usr/bin/perl

use strict;
use warnings;

use Net::Ping;

my $pingtest  = Net::Ping->new();
my $inputfile = $ARGV[0];

open(my $fh,"<",$inputfile);

while (<$fh>) {
  if ($_ =~ /\((\d+\.\d+\.\d+\.\d+)\)/) {
    my $ip = $1;
    if ($pingtest->ping($ip)) {
      print "$ip is up!\n";      
    } else {
      print "$ip is down...\n";
    }   
  }
}
output:

Code:
drew@balveda$ /tmp/mping /tmp/ips
192.168.1.1 is down...
192.168.1.2 is down...
12.213.12.123 is up!
45.213.12.221 is down...
192.168.0.1 is up!

Last edited by hybby; 09-05-2013 at 06:54 PM.
 
  


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
i need to write a c code to ping IP addresses which are read from a text file vinothtitan Linux - Newbie 5 10-12-2014 11:23 PM
Script to ping multiple IP addresses one by one peddip Linux - Server 14 09-15-2011 11:44 PM
[SOLVED] Ping IP addresses using bash and linux ericasass Programming 3 06-14-2011 10:31 PM
Cannot ping virtual ip addresses (VIP) mixxalot Linux - Networking 3 11-25-2007 01:50 PM
ping and IP addresses BACTRATE Mandriva 3 06-16-2004 06:49 PM

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

All times are GMT -5. The time now is 06:14 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