Skip to content

Commit

Permalink
Make ping work on IPv4 for Mac and Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
chaen committed Apr 9, 2024
1 parent a39f7d1 commit e0463ca
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion run_demo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,18 @@ function space_monitor(){
}

function check_hostname(){

# Force the use of ipv4.
# MacOS does not have the -4 option....
if [[ "$(uname -s)" = "Linux" ]]; then
ping_cmd="ping -4";
else
ping_cmd="ping";
fi

# Check that the hostname resolves to an IP address
# dig doesn't consider the effect of /etc/hosts so we use ping instead
if ! ip_address=$(ping -4 -c 1 "$1" | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' | head -n 1); then
if ! ip_address=$($ping_cmd -c 1 "$1" | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' | head -n 1); then
printf "%b ping command exited with a non-zero exit code\n" ${SKULL_EMOJI}
return 1
fi
Expand Down

0 comments on commit e0463ca

Please sign in to comment.