aping.sh: fix exit status checks and LAN IP lookup

Save ping's exit status before testing it (the second $? check read the
first test's result), treat 0 as success, pass player_opts as an array,
and use ip instead of the deprecated ifconfig.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-29 14:22:37 -04:00
co-authored by Claude Opus 5.5
parent 7aa71cb891
commit dd004108f2
+10 -8
View File
@@ -33,17 +33,18 @@ withLoop(){
echo "Pinging ${site} until a connection is reached" echo "Pinging ${site} until a connection is reached"
${pingcmd} ${site} &>/dev/null ${pingcmd} ${site} &>/dev/null
if [ $? = "2" ];then ping_result=$?
if [ $ping_result = "2" ];then
echo echo
echo "The internet is not working:(" echo "The internet is not working:("
sleep 1 sleep 1
elif [ $? = "1" ];then elif [ $ping_result = "0" ];then
if [ "${play_sound}" = "true" ];then if [ "${play_sound}" = "true" ];then
${sound_player} ${player_opts[*]} ${sound_file} "${sound_message}" & ${sound_player} "${player_opts[@]}" ${sound_file} "${sound_message}" &
fi fi
lanip=$(ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p') lanip=$(ip -o -4 addr show | awk '$2 != "lo" {sub(/\/.*/, "", $4); print $4}')
pubip=$(curl -s ifconfig.me) pubip=$(curl -s ifconfig.me)
clear clear
@@ -79,20 +80,21 @@ withoutLoop(){
echo "Pinging ${site}" echo "Pinging ${site}"
${pingcmd} ${site} &>/dev/null ${pingcmd} ${site} &>/dev/null
ping_result=$?
if [ $? = "2" ];then if [ $ping_result = "2" ];then
echo echo
echo "The internet is not working :(" echo "The internet is not working :("
elif [ $? = "1" ];then elif [ $ping_result = "0" ];then
if [ "${play_sound}" = "true" ];then if [ "${play_sound}" = "true" ];then
${sound_player} "${player_opts}" "${sound_file}" "${sound_message}" & ${sound_player} "${player_opts[@]}" "${sound_file}" "${sound_message}" &
fi fi
lanip=$(ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p') lanip=$(ip -o -4 addr show | awk '$2 != "lo" {sub(/\/.*/, "", $4); print $4}')
pubip=$(curl -s http://whatismijnip.nl |cut -d " " -f 5) pubip=$(curl -s http://whatismijnip.nl |cut -d " " -f 5)
clear clear