Ping shell script - need urgent help

Hi friends,

i have a file contains IP address like below
cat file.txt
10.223.20.1
10.223.20.2
10.223.20.3
10.223.20.4
10.223.10.5
.
.
.
like this

Now i want to make a script which gives output whether each ip is pinging or not...

the result will be like this

10.223.20.1 up
10.223.20.2 up
10.223.20.3 down
10.223.20.4 up
10.223.20.5 down

please friends please give me quick reply

please please

---------- Post updated at 03:51 AM ---------- Previous update was at 03:36 AM ----------

Hi friends, this is seriously something urgent task to do...

could you please help me on urgent basis?

I'm not responding fast because of that wording. Please do not use "urgent" or anything like that in your posts. You can get demerits and penalties for that, and we wouldn't want that to happen. :eek: You will make the moderators very unhappy when you use that wording. There is a separate emergency forum where you can spend all those bits you have earned, to get the fastest possible response. Anyway, here is a way to do it, and I hope it helps:

$ cat file.txt
10.223.20.1
205.171.3.25
10.223.20.3
206.190.36.45
10.223.10.5
198.105.251.23
$ cat ping.sh
while read ip; do
  ping -c 1 $ip > /dev/null 2> /dev/null
  if [ $? -eq 0 ]; then
    status=up
  else
    status=down
  fi
  echo $ip $status
done < file.txt
$ ./ping.sh
10.223.20.1 down
205.171.3.25 up
10.223.20.3 down
206.190.36.45 up
10.223.10.5 down
198.105.251.23 down

I will follow ur suggestions, i dont use these words once again.

but your script is executing properly..
really thank you very much.

You're very welcome.

1 Like

The

  ping -c 1 $ip > /dev/null 2> /dev/null
  if [ $? -eq 0 ]; then

can be simplified

  if ping -c 1 $ip > /dev/null
  then

A more professional solution is

fping < file.txt

Google for download fping

1 Like
cat inputfile | xargs -n1 ping 2>&1 | nawk '{if($0~/alive/){print $1" up"}else{print $1" down"}}'

Thank you for your reply,

But when I tried to execute from command prompt, showing the below error

"Ambiguous output redirect."

could you please lookinto this...

it should work , what OS you are using ?

---------- Post updated at 07:13 AM ---------- Previous update was at 07:06 AM ----------

Try this ,

cat inputfile |xargs -n1 ping 2>&1 | awk '{if($0~/alive/){print $1" up"}else{print $NF" down"}}'

x86_64 x86_64 x86_64 GNU/Linux

---------- Post updated at 06:14 AM ---------- Previous update was at 06:13 AM ----------

For the above one also same error

Oops.. Didnt realise its GNU Linux until now . Sorry the oneliner would work well on Solaris but not on linux. I will see if i can figure out some oneliner for Linux :slight_smile: