Skip to content

Instantly share code, notes, and snippets.

@gmzi
Last active May 13, 2022 14:29
Show Gist options
  • Save gmzi/c3c8a92a49d04626b35029ee237a3294 to your computer and use it in GitHub Desktop.
Save gmzi/c3c8a92a49d04626b35029ee237a3294 to your computer and use it in GitHub Desktop.
Find the IP of the local machine and make a url to localhost server
#!/bin/sh
IP=""
# find all the active connections of the machine,
# and find the IP address of the first one
for i in $(ifconfig -lu) ; do
if (ifconfig $i | grep -q "status: active") ; then
IP=$(ifconfig $i | grep -i "inet ")
break
fi ;
done
# Finish process if no active connection:
if [ "$IP" = "" ] ; then
echo "no active connection"
else
# trim the IP address to use only the first part:
INET=$(awk '{split($0, array); print array[2]}' <<< $IP)
# parse it to the url:
URL="http://$INET:3000"
# if other port is passed as an argument, parse it instead of the
# default one:
if [ "$1" != "" ] ; then
URL="http://$INET:$1"
fi
echo $URL
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment