Tip: Detect Network Connectivity in Bash
There are times that we are required to check hundreds of servers for network connectivity, and what command comes first in mind when testing network – ping. To help out other system and network administrators out there, here is a quick bash script to ping servers.
#!/bin/bash
SERVERS=server.txt
for i in $(cat $SERVERS)
do
ping -c2 $i > /dev/null
if [ $? -ne 0 ]
then
echo "$i is DOWN"
else
echo "$i is UP"
fi
sleep 3
done
Tip: How to Place Pause in Bash Scripts
There some scripts that need human interaction from time to time from confirming certain actions or to warn users that they are about to do something stupid ( “ rm -rf / “, perhaps?). In any case, putting a pause break in a Bash script is a very nifty trick to learn.
This is an example of how to use the Bash built-in command read to put pauses in scripts:
#!/bin/bash
find /home -type f -name "*.txt"
read -p "Press [enter] key to delete *.txt files in /home or CTRL+C to exit"
find /home -type f -name "*.txt" | xargs rm -v
This example script will run the find command and display the results. If user presses the Enter key, the script will remove all files that were found, while pressing CTRL+C will terminate the script.
Here is another variation of the the script:
#!/bin/bash
find /home -type f -name "*.txt"
echo "Press [enter] key to delete *.txt files in /home or CTRL+C to exit"
read contscr
find /home -type f -name "*.txt" | xargs rm -v
Search PinoyTux
Subscribe to Email Feeds
Blog Lounge
Popular Posts
Recent Posts
Drop your Card Here
Recent Comments
- Anidich1 on Tip: Add User and Generate Password Script
- Tom S on Cebu Pacific Sucks
- kadersardar on PinoyTux Spreads Some CommentLuv
- Steve on Creative Labs Threatens Third Party Driver Modder
- Barry on Free Laptops with Broadband Connection








