比较Bash中的两个IP地址

时间:2014-07-30 18:06:49

标签: bash ubuntu ip-address

我试图找出两个ip地址是否相同。我承认我是bash的新手,但我认为没有理由不这样做:

if [[ "$IPAddress" != "$OLDIPAddress" ]]
then
  echo "IP Not the Same"
else
  echo "IP Same"
fi

出于测试目的,我甚至对两个变量的值进行了硬编码,但它们仍然没有显示为相同。我知道你并不总是看到自己的错别字,但我不明白为什么这不起作用。有什么想法吗?

2 个答案:

答案 0 :(得分:3)

快速演示脚本,用于比较不同格式的IP地址,"正常"例如,与DHCP总是3位八位字节。除了比较之外,您还可以使用一些有用的技巧进行解析和验证,以及来自社区的更多催化剂。 (这是我的第一篇带代码的帖子,所以希望它能通过确定 - 就像YMMV一样。)

#!/bin/bash
# demo.sh - parse, validate, compare ip addresses
# usage: demo.sh {ipaddress1} {ipaddress2}

#--- IP ADDRESS 1
#---   Is there anything besides digits and three dots?
#---   Parse out the octets
#---   There should be exactly 4 populated octets
#---   All 4 octets must not be > 255
[ "${1//[0-9]/}" != '...' ] && echo "invalid" && exit 1
O1=(${1//./ })
[ -z "${O1[3]}" -o -n "${O1[4]}" ] && echo "invalid" && exit 1
[ ${O1[0]} -gt 255 -o ${O1[1]} -gt 255 -o ${O1[2]} -gt 255 -o ${O1[3]} -gt 255 ] && echo "invalid" && exit 1
#--- IP ADDRESS 2
[ "${2//[0-9]/}" != '...' ] && echo "invalid" && exit 1
O2=(${2//./ })
[ -z "${O2[3]}" -o -n "${O2[4]}" ] && echo "invalid" && exit 1
[ ${O2[0]} -gt 255 -o ${O2[1]} -gt 255 -o ${O2[2]} -gt 255 -o ${O2[3]} -gt 255 ] && echo "invalid" && exit 1

#--- Numeric compares of each octet
echo "Comparing each octect"
if [ ${O1[0]} -eq ${O2[0]} ]
  then
    echo "  ${O1[0]} == ${O2[0]}"
  else
    echo "  ${O1[0]} != ${O2[0]}"
  fi
if [ ${O1[1]} -eq ${O2[1]} ]
  then
    echo "  ${O1[1]} == ${O2[1]}"
  else
    echo "  ${O1[1]} != ${O2[1]}"
  fi
if [ ${O1[2]} -eq ${O2[2]} ]
  then
    echo "  ${O1[2]} == ${O2[2]}"
  else
    echo "  ${O1[2]} != ${O2[2]}"
  fi
if [ ${O1[3]} -eq ${O2[3]} ]
  then
    echo "  ${O1[3]} == ${O2[3]}"
  else
    echo "  ${O1[3]} != ${O2[3]}"
  fi

#--- Numeric IP address compare
echo "Compare via long if"
if [ ${O1[0]} -eq ${O2[0]} -a ${O1[1]} -eq ${O2[1]} -a ${O1[2]} -eq ${O2[2]} -a ${O1[3]} -eq ${O2[3]} ]
  then
    echo "  ${1} == ${2}"
  else
    echo "  ${1} != ${2}"
  fi

#--- Loop for numeric compare
echo "Compare via loop"
SAME="Y"
for I in 0 1 2 3
  do
   echo "  loop ${I} compare ${O1[$I]} ${O2[$I]}"
   [ ${O1[$I]} -ne ${O2[$I]} ] && SAME="" && break
  done
echo "  result"
if [ -n "${SAME}" ]
  then
    echo "  ${1} == ${2}"
  else
    echo "  ${1} != ${2}"
  fi

exit


$ ./demo.sh 1.2.3.4 1.2.3
invalid
$ ./demo.sh 1.2.3.4 1.2.q.4
invalid
$ ./demo.sh 1.2.3.4 1.2.3.4.
invalid
$ ./demo.sh 1.2.3.4 1.2.3.4.5
invalid
$ ./demo.sh 1.2.3.4 1.2.300.4
invalid
$ ./demo.sh 1.02.3.004 01.2.003.4
Comparing each octect
  1 == 01
  02 == 2
  3 == 003
  004 == 4
Compare via long if
  1.02.3.004 == 01.2.003.4
Compare via loop
  loop 0 compare 1 01
  loop 1 compare 02 2
  loop 2 compare 3 003
  loop 3 compare 004 4
  result
  1.02.3.004 == 01.2.003.4
$ ./demo.sh 1.02.3.004 01.2.030.4
Comparing each octect
  1 == 01
  02 == 2
  3 != 030
  004 == 4
Compare via long if
  1.02.3.004 != 01.2.030.4
Compare via loop
  loop 0 compare 1 01
  loop 1 compare 02 2
  loop 2 compare 3 030
  result
  1.02.3.004 != 01.2.030.4
$

答案 1 :(得分:2)

虽然您的命令应该可行,但您可以使用简单的测试运算符(只需一个支架)。优点是它可以与任何(POSIX)shell一起使用。但是,[[运算符也应该起作用。

你能再现这个小例子吗? (应输出'是'):

IPAddress="127.0.0.1"
OLDIPAddress="127.0.0.1"

if [ "$IPAddress" != "$OLDIPAddress" ] ; then 
    echo "no"
else
    echo "yes"
fi