#!/bin/sh # # beep_ip_address_through_pc_speaker.sh, by Sebastiaan Giebels 2007+2013, sgiebels_beepscriptATpcprobleemloos.nl # # Linux sh shell script to beep the individual numbers of an IPv4 address through # the pc speaker. Perfect for use with headless systems which use a DHCP address. # Note that it will beep 10 times when signaling a zero ('0') # # Prerequisites: device with speaker, 'beep' package, 'iproute2' or 'net-tools' package. # Update 2013-12-01: Fixed bashisms when using real sh (like "Syntax error: Bad for loop variable" ) # Update 2013-12-07: added iproute2 'ip' command, improved error handling & comments) # Todo: find 'beep' command using 'which' and use this instead of the hardcoded /usr/bin/beep # If you don't hear any sound, make sure the 'beep' command works, and volume is not muted. # Check that the '/usr/bin/beep' command exists and is executable by user running this script. # Verify that the ETH_DEVICE points to the correct network interface (eth0, br0, ...). # Limitations: although an ethernet interface can have multiple IP addresses, it will only beep the first one it detects. ETH_DEVICE="eth0" #Get IPv4 address (including netmask if using 'ip' instead of 'ifconfig'): #Update 2013-12-07: ifconfig is being deprecated, and will be replaced by iproute2. #If the command 'ip' is not available, use the (deprecated) 'ifconfig' command: if which ip; then echo "Using iproute2 'ip' command..." IP_ADDRESS=`ip addr show dev $ETH_DEVICE | grep -m 1 "inet"| awk '{print $2}'` #(will result in IP_ADDRESS="192.168.1.1/24" or similar) else if which ifconfig; then echo "Using (deprecated) 'ifconfig' command..." # Retreive dot-separated ipv4 address from the ifconfig information, using grep and cut: IP_ADDRESS=`/sbin/ifconfig | grep -A1 "eth0" | grep -m 1 "inet" | cut -d: -f 2 |cut -d" " -f1` else echo "ERROR: Cannot find either 'ip'(iproute2 package) or 'ifconfig'(net-tools package), stopping!" exit 1 fi fi #Uncomment this for an example with zeroes in the ip address: #IP_ADDRESS="10.0.0.1/24" # Enable next line for debugging: echo "Using the IP address: $IP_ADDRESS" if [ -z "$IP_ADDRESS" ]; then echo "ERROR: parsing IP address failed (empty string)" exit 1 fi # For all characters in dotted ipv4 address (with optional '/##' netmask): for IP_CHAR_INDEX in $(seq 0 ${#IP_ADDRESS}); do # Get the next character to process: #with bash it would be: NUMBER=${IP_ADDRESS:$a:1}, with 'sh' as shell: IP_CHAR=`expr substr $IP_ADDRESS $IP_CHAR_INDEX 1` case $IP_CHAR in [0-9]) #echo number #$IP_CHAR # Beep 'IP_CHAR' times, except for 0 (=beep 10 times): if test $IP_CHAR -eq 0; then IP_CHAR=10 fi `/usr/bin/beep -l 45 -f 15 -d 150 -r $IP_CHAR` ;; [.]) #echo dot # Different note to signal a '.' (octet separator) in the address: `/usr/bin/beep -l 20 -f 25 -d 20` ;; [/]) #echo slash # Different note to signal a '/' (netmask separator) in the address: `/usr/bin/beep -l 20 -f 20 -d 10` `/usr/bin/beep -l 20 -f 22 -d 10` `/usr/bin/beep -l 20 -f 24 -d 10` `/usr/bin/beep -l 20 -f 26 -d 10` `/usr/bin/beep -l 20 -f 28 -d 10` `/usr/bin/beep -l 20 -f 30 -d 10` ;; esac sleep 1 done