0
Here is my sample :
PING my.host.local (10.10.10.10): 56 data bytes
64 bytes from 10.10.10.10: icmp_seq=0 ttl=63 time=2.034 ms

--- my.host.local ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 2.034/2.034/2.034/0.000 ms

I would like to do something like : if /64 bytes/ then print something like : "'my.host.local' is up"

The problem is when I do a regex of /64 bytes/ then I'm losing the line showing the host name "my.host.local" and therefore I can't use that field to print my final message.

how would you do this ?

Cheers,

3 Answers 3

2

Use a variable to store the host name:

awk '/^PING/ { host = $2 } /^64 bytes/ { print host " is up" }'

To do something if the host is down, you need to note if a response was seen, and handle both cases at the end:

awk '/^PING/ { host = $2 }
     /^64 bytes/ { up = 1 }
     END {
       if (up) { print host " is up" }
       else { print host " is down" }
     }'
4
  • Thanks it works perfectly however I would like to do an "else" statement but It doesn't return anything. I tried the following : awk '$1 == "PING" { host = $2 } $2 == "bytes" { if ($1 == 64) {print host " is up " } else { print host " is down "} }' bad_ping.txt Oct 20, 2022 at 17:06
  • See the update. Oct 20, 2022 at 18:04
  • Its working, thanks a lot . I have some trouble to understand the logic though. So yesterday I tried to understand exactly what you did to make it works. I understand that with '/^PING/ { host = $2 } we assign the variable called "host" based on the 2nd column of the record starting by "PING". Then if we match a record starting by "64 bytes" , we assign the variable up to "1" . But then I don't understand the if statement ? If "up" is "true" then we print " host is up" otherwise we print "host is down" ? is that it ? Oct 20, 2022 at 18:33
  • Yes, if up is non-zero we print host " is up", i.e. the value of the host variable (stored when we matched /^PING/) followed by “is up”; otherwise, host " is down". Nov 7, 2022 at 16:10
0

You could do something like

ping -c 1 $machine | awk '
  $1 == "PING" {host = $2}
  $2 == "bytes" {
    if ($1 == 64)
      print host " is up"
    else
      print host " did not ping 64 bytes"
  }
'
0
0

You presumably know the host already (because you used it to run ping) and detecting the 64 bytes text is not necessary since you can use the exit status of ping:

myhost=my.host.local

if ping -c 1 -t 5 "$myhost" >/dev/null 2>&1
then
    printf 'host %s is up\n' "$myhost"
else
    printf 'host %s does not appear to be reachable\n' "$myhost"
fi

This tries to ping the given host with a timeout. It throws away any output because only the exit status of the utility is interesting. This is used by if to decide which branch to take.

On FreeBSD, -t sets the timeout in seconds. On Linux, -t sets the TTL, and ping waits for twice as many seconds.

2
  • Thanks a lot its pretty handy to handle it this way, however for hosts down I have this following error in the output : ./check_network.sh: line 25: 43244 Alarm clock: 14 ping -c 1 -t 5 "$i" > /dev/null 2>&1 host smttai.fsw.ca does not appear to be reachable . Why am I getting this "alarm clock" message ? Oct 20, 2022 at 18:55
  • @VincentAury I honestly don't know, as I don't see your script or know what sort of Unix you're on. Is this relevant: How to get rid of "Alarm clock" message in perl timeout?
    – Kusalananda
    Oct 20, 2022 at 19:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .