Comparing two IP Addresses in Bash

前端 未结 2 1298
小鲜肉
小鲜肉 2021-01-19 18:36

I am trying to find if two ip addresses are the same or not. I admit that I am new with bash, but I see no reason this would not work:

if [[ \"$IPAddress\" !         


        
相关标签:
2条回答
  • 2021-01-19 18:45

    While your command should work, you can use the simple test operator (just a single bracket). The advantage is that it will work with any (POSIX) shell. However, the [[ operator should work too.

    Can you reproduce this little example? (Should output 'yes'):

    IPAddress="127.0.0.1"
    OLDIPAddress="127.0.0.1"
    
    if [ "$IPAddress" != "$OLDIPAddress" ] ; then 
        echo "no"
    else
        echo "yes"
    fi
    
    0 讨论(0)
  • 2021-01-19 19:06

    Quick demo script for comparing differently formated ip addresses, "normal" vs. DHCP always 3 digit octets for instance. Hopefully a couple of useful tricks you can use for parsing and validation in addition to compares, and a catalyst for some more from the community. (This is my first post with code so hopefully it comes through ok - as always 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
    $
    
    0 讨论(0)
提交回复
热议问题