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:
@@ -33,17 +33,18 @@ withLoop(){
|
||||
|
||||
echo "Pinging ${site} until a connection is reached"
|
||||
${pingcmd} ${site} &>/dev/null
|
||||
if [ $? = "2" ];then
|
||||
ping_result=$?
|
||||
if [ $ping_result = "2" ];then
|
||||
echo
|
||||
echo "The internet is not working:("
|
||||
sleep 1
|
||||
|
||||
elif [ $? = "1" ];then
|
||||
elif [ $ping_result = "0" ];then
|
||||
if [ "${play_sound}" = "true" ];then
|
||||
${sound_player} ${player_opts[*]} ${sound_file} "${sound_message}" &
|
||||
${sound_player} "${player_opts[@]}" ${sound_file} "${sound_message}" &
|
||||
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)
|
||||
|
||||
clear
|
||||
@@ -79,20 +80,21 @@ withoutLoop(){
|
||||
|
||||
echo "Pinging ${site}"
|
||||
${pingcmd} ${site} &>/dev/null
|
||||
ping_result=$?
|
||||
|
||||
if [ $? = "2" ];then
|
||||
if [ $ping_result = "2" ];then
|
||||
echo
|
||||
echo "The internet is not working :("
|
||||
|
||||
elif [ $? = "1" ];then
|
||||
elif [ $ping_result = "0" ];then
|
||||
|
||||
if [ "${play_sound}" = "true" ];then
|
||||
${sound_player} "${player_opts}" "${sound_file}" "${sound_message}" &
|
||||
${sound_player} "${player_opts[@]}" "${sound_file}" "${sound_message}" &
|
||||
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)
|
||||
|
||||
clear
|
||||
|
||||
Reference in New Issue
Block a user