LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   ping alive host in network (https://www.linuxquestions.org/questions/linux-newbie-8/ping-alive-host-in-network-4175433457/)

slowerogue 10-22-2012 04:05 AM

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

acid_kewpie 10-22-2012 04:27 AM

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

slowerogue 10-22-2012 04:34 AM

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

badger_fruit 10-22-2012 04:53 AM

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

acid_kewpie 10-22-2012 04:57 AM

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


chrism01 10-22-2012 05:14 AM

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

acid_kewpie 10-22-2012 05:33 AM

Quote:

Originally Posted by chrism01 (Post 4812022)
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

slowerogue 10-22-2012 09:46 PM

hi thank you guys!
the $? actually represent as a what

chrism01 10-22-2012 11:44 PM

@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/


All times are GMT -5. The time now is 04:40 AM.