Last active
July 25, 2017 18:11
-
-
Save thales17/490c3bb50dc1836a97783b6c7ecfcd19 to your computer and use it in GitHub Desktop.
Bash find ip with timeout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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