3

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
devin
  • 1,246
  • 3
  • 20
  • 27
  • 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 02:09
  • First one, however I neglected to mention that my IP in the intranet is always the same. – devin Aug 26 '09 at 02: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 02:33

6 Answers6

7

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

MikeyB
  • 39,291
  • 10
  • 105
  • 189
  • 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 02:11
  • Alternative to backslashes: use `grep -F` (or `fgrep`) which matches exact patterns, not regexes. – David Z Aug 26 '09 at 05: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
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).

nik
  • 7,100
  • 2
  • 25
  • 30
  • 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
1

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
chris
  • 11,944
  • 6
  • 42
  • 51
0

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.

cas
  • 6,783
  • 32
  • 35
0

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.

0


#!/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

Ali Mezgani
  • 3,850
  • 2
  • 24
  • 36