Help with Ping Script and AWK

Hey Guys,

First time posting here. I keep getting back here on my google searches to help me in my scripting quest.
Can i ask for some guidance please?

I acquired a script, from these forums in fact that pings a list of IPs/Hostnames. Works a treat. Echos results to a file with the value (IP/Hostname) pinged.

Before

device1      1.1.1.1
device2      2.2.2.2

Script

#! /bin/sh
IPLIST=`less ./devices.txt | awk '{print $2}'`
for ip in $IPLIST
do
  echo $ip
  ping -c 2 $ip >>device-log.txt
  if [ $? -eq 0 ]
  then
     echo $ip "PINGS">>pingresultsace-device.txt
  else
    echo $ip "DOESN'T PING">>pingresultsace-device.txt
 fi
done

The output is

1.1.1.1 PINGS
2.2.2.2 DOESN'T PING

What i would really like is to put the entire line, or parts of it to produce. By parts of it i mean, using the AWK command or similar to select the fields to echo.

device1      1.1.1.1 PINGS
device2      2.2.2.2 DOESN'T PING

Thanks All

Maybe something like this?

#! /bin/sh

while read host ip
do
  echo $ip
  ping -c 2 $ip >>device-log.txt
  if [ $? -eq 0 ]
  then
    echo $host $ip "PINGS">>pingresultsace-device.txt
  else
    echo $host $ip "DOESN'T PING">>pingresultsace-device.txt
  fi
done < ./devices.txt

PERFECT !!! :smiley:

I did try the while read line, but was getting no where.

Can you tell me how this, using host and ip gets the column information.
i.e. suppose there was more columns?

Thanks for the fast reply

Excerption from the man page of sh: