I got a task to do. Every morning check if a cluster of servers is up and restart those, which aren’t. To check if a server is up we’d use:
ping -c 1 IP
which pings the server once.
Ok, that’s over. But like I said I got a list of servers, which changed every morning so it requires to write a simple script. I decided to use linux Bash scripting. Assuming the input data is a list of IPs in csv format, we may create a new file, let’s name it checkifup.sh
touch checkifup.sh chmod +x checkifup.sh vi checkifup.sh
and copy and paste this contents
#!/bin/bash # Program name: checkifup.sh # author: Picios # Usage: ./checkifup.sh list.csv FILE=$1 while read p; do IPDIRTY=$(echo $p | tr '\r' ' ') IP=$(echo $IPDIRTY) ping -c 1 $IP &> /dev/null if [ $? -eq 0 ]; then echo "$IP is up" else echo "$IP is down" fi; done <$FILE
IPDIRTY=$(echo $p | tr '\r' ' ') IP=$(echo $IPDIRTY)
part is only to clean input line, because sometimes it happens to get some white spaces.
Hit ESCAPE and double SHIFT+Z to save file. Next create a list of servers you want to check. I won’t show you mine, but it would look like this:
127.0.0.1 18.104.22.168 nonexisting.org.net 22.214.171.124
Save to a file called eg. list.csv. And now run our test:
You will get a nice list of servers. Use it well nerds.