-3

I am trying to create a ping monitoring for OpenVPN client devices.

I want to ping all devices, from a IP list.

The .txt file is looking like this:

client1,10.8.0.2
client2,10.8.0.3
client3,10.8.0.3
.. and so on 

I want the bash script to create a .txt file which contains the following:

client1: online
client2: offline
client3: online
and so on.

How is this possible?

Thanks in advance!

jww
  • 97,681
  • 90
  • 411
  • 885
tom318
  • 33
  • 1
  • 5
  • 2
    Start with `while IFS=, read -r name ip; do ping "$ip"; done < infile > outfile`, google what `$?` means in a shell script, and get back to us if you have a problem later AFTER you've tried to figure out the rest for yourself. – Ed Morton Jul 09 '19 at 14:03
  • 1
    thank you my friend!! i will have a look at it and come back to you if i have trouble. – tom318 Jul 09 '19 at 14:07

1 Answers1

0

For my needs I use this script:

#! /bin/bash
while read -r client address
do
    ping -c 1 $address
    ret=$?
    if [[ 0 -eq $ret ]] 
    then
        echo "$client: online"
    else
        echo "$client: off line"
    fi
done < clients

With:

IFS=,

Set comma as field separator

while read -r foo bar

Read a file and store fields in client and adrress variables

pinc -c 1 address

Ping the address only once

ret=$?

Get ping command returned value (0 when all goes right)

if [[ 0 -eq $ret ]] ....

test if ping succedded

echo "$client: online"

Given ping result, print online or off line

done < clients

Idiomatic construction to read a file line by line

Mathieu
  • 8,840
  • 7
  • 32
  • 45
  • 1
    No need to explicitly reference `$?`. It is cleaner to write: `if ping -c 1 "$address"; then status=online; else status=offline; fi; printf "%s: %s" "$client" "$status";` – William Pursell Jul 09 '19 at 14:44