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 02-05-2014, 11:55 AM   #1
linux833
Member
 
Registered: Jul 2011
Posts: 40

Rep: Reputation: Disabled
ping script to count live hosts


Dears,

i would need your help to get this script some modification, this script will print
the live hosts, i need only to count the word "up" then i will know how many hosts are up on the network:

Code:
#!/bin/bash

is_alive_ping()
{
  ping -c 1 $1 > /dev/null
  [ $? -eq 0 ] && echo Node with IP: $i is up.
}

for i in 192.168.0.{1..254} 
do
is_alive_ping $i & disown 
done
appreciate any help.
 
Old 02-05-2014, 01:09 PM   #2
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
I would recommend
Code:
nmap -sP 192.168.0.0/24
 
1 members found this post helpful.
Old 02-05-2014, 01:52 PM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
ping a LOT of hosts for up|down
 
Old 02-06-2014, 04:31 AM   #4
linux833
Member
 
Registered: Jul 2011
Posts: 40

Original Poster
Rep: Reputation: Disabled
Thanks teckk and Habitual for your respond, what i need is just to add "count" option so the output will show only the numeric of "up" hosts like 60 or 10 base on
counting "up" keyword and print only the number.

can you help me on this.


Mohamed.
 
Old 02-06-2014, 08:34 AM   #5
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Sure, what do you have so far?
 
Old 02-06-2014, 03:58 PM   #6
linux833
Member
 
Registered: Jul 2011
Posts: 40

Original Poster
Rep: Reputation: Disabled
what i need, is only to count the number of "up" hosts, the scrip that i post output:

Node with IP: 192.168.0.3 is up.
Node with IP: 192.168.0.1 is up.
Node with IP: 192.168.0.12 is up.
Node with IP: 192.168.0.10 is up.
Node with IP: 192.168.0.15 is up.
Node with IP: 192.168.0.20 is up.
Node with IP: 192.168.0.45 is up.
Node with IP: 192.168.0.90 is up.
Node with IP: 192.168.0.60 is up.
Node with IP: 192.168.0.16 is up.
Node with IP: 192.168.0.11 is up.
Node with IP: 192.168.0.18 is up.
Node with IP: 192.168.0.21 is up.
Node with IP: 192.168.0.4 is up.

i need the script to count the "up" keyword and give me the output as only:

14

even no need for the full output like "Node with IP: 192.168.0.4 is up" main purpose is to count up hosts at a given time of running the script.i need to run the script multiple time against subnet for troubleshooting purpose not monitoring or administration.

any help will be appreciated.

Mohamed.

Last edited by linux833; 02-06-2014 at 04:00 PM.
 
Old 02-06-2014, 05:27 PM   #7
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by linux833 View Post
i need the script to count the "up" keyword and give me the output as only
What script is that exactly?
 
Old 02-07-2014, 06:52 AM   #8
linux833
Member
 
Registered: Jul 2011
Posts: 40

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Habitual View Post
What script is that exactly?
Hi, this script:
Code:
#!/bin/bash

is_alive_ping()
{
  ping -c 1 $1 > /dev/null
  [ $? -eq 0 ] && echo Node with IP: $i is up.
}

for i in 192.168.0.{1..254} 
do
is_alive_ping $i & disown 
done
i need to modified it to count only up keyword, then i will know how many up host on the network.
 
Old 02-07-2014, 06:57 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
you can modify this line:
[ $? -eq 0 ] && echo Node with IP: $i is up.
to (not only print the node, but) count them
 
Old 02-07-2014, 07:15 AM   #10
linux833
Member
 
Registered: Jul 2011
Posts: 40

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
you can modify this line:
[ $? -eq 0 ] && echo Node with IP: $i is up.
to (not only print the node, but) count them
Hi pan64,
could you please help me to get needed change, appreciated.
 
Old 02-07-2014, 08:32 AM   #11
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by linux833 View Post
Hi, this script:
Code:
#!/bin/bash

is_alive_ping()
{
  ping -c 1 $1 > /dev/null
  [ $? -eq 0 ] && echo Node with IP: $i is up.
}

for i in 192.168.0.{1..254} 
do
is_alive_ping $i & disown 
done
i need to modified it to count only up keyword, then i will know how many up host on the network.
looks familiar.
http://www.linuxquestions.org/questi...script-578420/
 
Old 02-07-2014, 09:52 AM   #12
linux833
Member
 
Registered: Jul 2011
Posts: 40

Original Poster
Rep: Reputation: Disabled
Hi Habitual,

let check this, thanks alot for your help.
 
Old 02-07-2014, 11:57 AM   #13
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
You're very welcome.
 
Old 02-10-2014, 06:20 PM   #14
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
https://github.com/mina86/tinyapps/b...r/pingrange.pl
 
  


Reply

Tags
count



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
does PING resolve only once or each count to same address? plisken Linux - Networking 6 09-27-2013 02:57 PM
[SOLVED] Can ping Ubuntu hosts but not Debian hosts by hostnames garyozzy Linux - Networking 6 03-09-2012 10:32 AM
how to stop ping response/count manuleka Linux - Newbie 1 07-02-2009 05:08 AM
can't ping local IP address but can ping remote hosts rob_xx17 Linux - Networking 4 12-02-2006 08:39 AM
dhcp client can't ping gateway but can ping other local hosts dirty_forks Linux - Networking 7 10-08-2004 10:54 AM

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

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