Track availability of computers

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    I must write a program that records the availability of computers. For the argument i have to give him a file containing a list of computers, the program should check every five minutes if they are available. If a computer is not reachable, then the program should write into the file the name of computer and the time when it was't available.

Example usage:

$ Check list_of_computers.txt

The print in the file:

io.something.si not reachable at 15:31
io.something.si not reachable at 15:36
io.something.si not available at the 15:41
verbena.something.si not reachable at 15:41

  1. Relevant commands, code, scripts, algorithms:
    ?

  2. The attempts at a solution (include all code and scripts):
    The command "ping" is given as a hint

Any ideas?

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    University of Ljubljana, Ljubljana, Slovenia, Janez Novak, ID63709

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

#!/bin/sh

while true; do
 while read line; do
  ping -c2 $line >/dev/null
   if [ $? != 0 ]; then
   echo $line not reachable at $(date "+%H:%M") >> heartbeat.log
   fi
 done < "$1"
sleep 300
done

Thank for the answer!

i have a question, what does the: -c2 switch do and: >/dev/null ?
the 2 means two packets transmitted right, what if i type 3 ?
how does this: done < "$1" work?

 -c count
         Stop after sending \(and receiving\) count ECHO_RESPONSE packets.
         If this option is not specified, ping will operate until inter-
         rupted.  If this option is specified in conjunction with ping
         sweeps, each sweep will consist of count packets.

If you type 3, then ping will obviously send 3 echo_response packets.
Simply try to manually run this command and you will immediately know what it does.

It simply prevents all that ping output to be shown on the terminal.
Try to remove it, and you will see the difference.

Thats the argument you submit with this script, namely the file with computer names/ip's.
When you run this script like ./script computerlist.txt the $1 will become computerlist.txt

1 Like

Hey thanks for the answer :wink: !