Skip to content

Instantly share code, notes, and snippets.

@thales17
Last active July 25, 2017 18:11
Show Gist options
  • Save thales17/490c3bb50dc1836a97783b6c7ecfcd19 to your computer and use it in GitHub Desktop.
Save thales17/490c3bb50dc1836a97783b6c7ecfcd19 to your computer and use it in GitHub Desktop.
Bash find ip with timeout
#!/bin/bash
TIMEOUTSEC=10
function findIP {
startTime=$(date +%s)
ip=$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1')
iplen=${#ip}
elapsedTime=$(( $(date +%s) - startTime ))
echo "Checking for IPv4 address..."
while [ $iplen -eq 0 ] && [ $elapsedTime -lt $TIMEOUTSEC ]
do
ip=$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1')
iplen=${#ip}
elapsedTime=$(( $(date +%s) - startTime ))
done
if [ $iplen -eq 0 ]
then
echo "Unable to find ip in $TIMEOUTSEC seconds"
else
echo "Your IP is $ip"
fi
}
sleep 20s
findIP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment