LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-22-2012, 05:05 AM   #1
slowerogue
Member
 
Registered: Oct 2012
Posts: 96

Rep: Reputation: Disabled
ping alive host in network


hi guys
i m trying to do a shellscript to find out any alive host in the network

Code:
for n in {1-254}; do
 host= 192.168.1.$n
 ping -c2 $host
 if [$?=0];then
  echo "$host is up"
else
  echo "$host is down"
fi
done
any help? it didnt work out properly
ty
 
Old 10-22-2012, 05:27 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
remove the space after "host=", and change {1-254} to {1..254}. or use a tool designed for the job, like fping

Last edited by acid_kewpie; 10-22-2012 at 05:29 AM.
 
Old 10-22-2012, 05:34 AM   #3
slowerogue
Member
 
Registered: Oct 2012
Posts: 96

Original Poster
Rep: Reputation: Disabled
oh thank you so much, DAT space

also, i was trying ..like save the output into another file with $date, so that they will not overwrite each other everytime i run the script
any helps? thx
 
Old 10-22-2012, 05:53 AM   #4
badger_fruit
Member
 
Registered: Dec 2008
Posts: 43

Rep: Reputation: 1
NOW=$(date +"%d-%m-%Y")
LOGFILE="$NOW.log"
for n in {1..254}; do
host=192.168.1.$n
ping -c2 $host
if [$?=0];then
echo "$host is up" >> $LOGFILE
else
echo "$host is down" >> $LOGFILE
fi
done
 
1 members found this post helpful.
Old 10-22-2012, 05:57 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Hmm, bit overkill I'd say.

Code:
for n in {1..254}; do
host=192.168.1.$n
ping -c2 $host
if [$?=0];then
echo "$host is up"
else
echo "$host is down"
fi
done > $(date +"%d-%m-%Y").log
 
1 members found this post helpful.
Old 10-22-2012, 06:14 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
I'd change this line
Code:
if [$?=0];then

if [[ $? -eq 0 ]]
then
Needs spaces around tokens, double-brackets better http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS, numeric comparison operator http://tldp.org/LDP/abs/html/comparison-ops.html
 
1 members found this post helpful.
Old 10-22-2012, 06:33 AM   #7
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by chrism01 View Post
I'd change this line
Code:
if [$?=0];then

if [[ $? -eq 0 ]]
then
Needs spaces around tokens, double-brackets better http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS, numeric comparison operator http://tldp.org/LDP/abs/html/comparison-ops.html
wtf did I miss that? Ugh
 
Old 10-22-2012, 10:46 PM   #8
slowerogue
Member
 
Registered: Oct 2012
Posts: 96

Original Poster
Rep: Reputation: Disabled
hi thank you guys!
the $? actually represent as a what
 
Old 10-23-2012, 12:44 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
@acid_kewpie: yeah, I was surprised; actually re-read that a few times to make sure before I posted

@slowerogue: $? is that final exit status of the last ie preceding cmd run.
Generally its 0 (zero) if ok, anything else is an error, but the actual error value and meaning is cmd dependent.

I recommend you bookmark/read
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
  


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
Forwarding ping packets from one 1st host to 2nd host via 2rd host sachee Linux - Networking 1 09-25-2011 02:51 PM
Please help me setup Local Area Network. I cannot ping Windows XP host from Ubuntu 9 avarashnon Linux - Newbie 1 05-10-2009 08:59 PM
Keep Alive Ping Script Apollo77 Linux - Networking 8 05-19-2006 01:29 PM
small network - can't ping by host, only IP jneider23 Linux - Networking 16 03-10-2005 09:40 PM
A network client can't ping a foreign host via a ppp connection Leandro Linux - Networking 1 11-15-2002 05:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:17 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration