BASH script that tests for IP
Is there an elegant way to test an IP address in a BASH script? I could do ifconfig and narrow down the IP using sed/awk, but I think there is a simpler solution.
My application is basically using SSH/SCP scripts when I'm in my intranet and while I'm not. So, I want this type of flow
if IP=192.168.1.1
then do this
else
then do that
Solution 1:
Something fairly simple that you can modify to suit your needs:
ip addr show dev eth0 | fgrep -q 'inet 192.168.1.1'
if [ $? -eq 0 ]; then
echo 'IP found'
else
echo 'IP not found'
fi
EDIT: forgot the fgrep :)
Solution 2:
If its a matter of differentiating if you are connected within your Intranet or not,
you could check with a quick short ping or arp attempt to a known internal server IP.
If the IP fails to respond, you are likely to be outside the Intranet.
There could be slight glitches (like the server being temporarily down).
These can be allowed (you mistakenly switch to the Internet mode),
Or, tested further with multiple checks (over different internal resources).