Skip to content

Instantly share code, notes, and snippets.

@wheresjames
Created October 16, 2024 09:36
Show Gist options
  • Save wheresjames/f237a8b0b630e5a7416d9d2f4c147a6a to your computer and use it in GitHub Desktop.
Save wheresjames/f237a8b0b630e5a7416d9d2f4c147a6a to your computer and use it in GitHub Desktop.
This script pings an IP address at regular intervals and plays a sound indicating the IP address is reachable or unreachable.
#!/bin/bash
# This script pings an IP address at regular intervals and plays a sound
# indicating the IP address is reachable or unreachable.
IP=$1
DELAY=10
TIMEOUT=3
# Check if an IP address was provided as an argument
if [ -z "$IP" ]; then
IP="8.8.8.8"
echo "No IP address provided. Using 8.8.8.8"
# echo "Usage: $0 <IP>"
# exit 1
fi
# Check if sox is installed, and install if necessary
check_and_install_sox() {
if ! command -v play &> /dev/null; then
echo "sox is not installed. Attempting to install it..."
if command -v apt-get &> /dev/null; then
sudo apt-get update
sudo apt-get install -y sox
elif command -v apt &> /dev/null; then
sudo apt update
sudo apt install -y sox
elif command -v yum &> /dev/null; then
sudo yum install -y sox
elif command -v brew &> /dev/null; then
brew install sox
elif command -v dnf &> /dev/null; then
sudo dnf install -y sox
elif command -v pacman &> /dev/null; then
sudo pacman -S sox
elif command -v zypper &> /dev/null; then
sudo zypper install sox
else
echo "Package manager not found. Please install sox manually."
exit 1
fi
else
echo "sox is already installed."
fi
}
check_and_install_sox
happy_sound() {
play -nq synth 0.1 sine 400 2>/dev/null
sleep 0.1
play -nq synth 0.1 sine 500 2>/dev/null
sleep 0.1
play -nq synth 0.1 sine 600 2>/dev/null
}
sad_sound() {
play -nq synth 0.1 sine 600 2>/dev/null
sleep 0.1
play -nq synth 0.1 sine 500 2>/dev/null
sleep 0.1
play -nq synth 0.1 sine 400 2>/dev/null
}
while true; do
ping -c 1 -W $TIMEOUT "$IP" &> /dev/null
if [ $? -eq 0 ]; then
echo "$IP is reachable."
happy_sound
else
echo "$IP is not reachable."
sad_sound
fi
sleep $DELAY
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment