Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
share|improve this question
    
your question isn't very clear. do you want an easy way to get the current ip address of a particular interface, or do you want to know how to test if an IP address matches a pattern (e.g. is it within a particular subnet) –  cas Aug 26 '09 at 2:09
    
First one, however I neglected to mention that my IP in the intranet is always the same. –  devin Aug 26 '09 at 2:20
1  
there aren't any "simpler" solutions. ifconfig or 'ip addr show' along with awk/sed/grep/cut/perl/etc are the simplest ways. there are numerous other ways but they're all more complicated. –  cas Aug 26 '09 at 2:33

6 Answers 6

up vote 7 down vote accepted

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 :)

share|improve this answer
    
that regexp needs backslashes before the dots. it will match many other addresses including 192.168.101, 192.168.111, 192.168.121 and so on as well as 192.168.1. so "grep -q 'inet 192\.168\.1\.1'" –  cas Aug 26 '09 at 2:11
    
Alternative to backslashes: use grep -F (or fgrep) which matches exact patterns, not regexes. –  David Z Aug 26 '09 at 5:50
    
That's a useless use of test. You could just put the pipe with grep at the end between the if and the then. –  chris Aug 26 '09 at 14:20
    
No guarantee that eth0 is your networking device, or even if it is a network device, that it is the primary one. –  kmarsh Aug 26 '09 at 14:23
    
@chris: True, however it's easily modifiable for his particular application. I like to think that people are smart enough to actually understand what I'm doing and be able to modify it themselves. @kmarsh: Yes it's a UUOT, however for demonstration code I think it's a little easier to understand for someone with not much experience with scripting. In this case, avoiding test with a one-liner is a useless optimization. –  MikeyB Aug 26 '09 at 17:22

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).

share|improve this answer
    
This also has the benefit of working across multiple interfaces, ie: wireless and wired, without extra code. Thanks for the idea! –  Jacob Hume Mar 31 '12 at 22:55

This is more a comment on the mikeB answer, but comments don't allow code so I'll post it as an answer:

You can use something like grep to find what you're looking for, such as

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

This can actually be simplified getting rid of the test ( [ is an alias for test) on the exit status, because if already checks exit status directly, which is how test communicates with if already:

if 
  ifconfig | fgrep -q 'inet 192.168.1.1'
then
  echo "IP Found"
else
  echo "IP Not found"
fi

And you can further simplify this by using the regex searches built into most bourne shells by using a case statement:

case $(ifconfig)
in 
  *"inet 192.168.1.1"*) 
     echo "IP found" 
   ;;
  *) 
    echo "IP not found" 
   ;; 
esac
share|improve this answer

one (IMO, more complicated, and more prone to failure) method is to configure your dhcp client to save the assigned IP address to a convenient file somewhere.

e.g. with dhclient, you can just drop a script in /etc/dhcp3/dhclient-enter-hooks.d/ to save the current IP to a file, say /var/run/current-IP.txt

then your other scripts can just cat that file to get the current IP.

the problem with doing something like this is that it only tells you what your IP address was when it was last assigned by dhcp. It doesn't actually tell you what your IP address is now. they may be the same, or they may not.

share|improve this answer

ip addr show <DEVICE> | grep inet | grep -v inet6 | awk '{print $2}'

This should output your IP, hopefully. There's probably a more elegant way to do it, but this gets the job done regardless of elegance.

share|improve this answer


#!/bin/bash
address="192.168.0.1"
interface="eth0"

ip=$(ifconfig -a $interface | awk -F: '/inet/ {print $2}' | head -n 1 | awk '{print $1}') if [ ${ip} == ${address} ]; then #Do something fi

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.