Check for IP validity

后端 未结 13 1337
庸人自扰
庸人自扰 2020-12-05 07:13

How do I check the validity of an IP address in a shell script, that is within the range 0.0.0.0 to 255.255.255.255?

相关标签:
13条回答
  • 2020-12-05 07:29

    I prefer to use ipcalc to do this, as long as my script doesn't have to be portable.

    ipcalc 1.1.1.355                                                                         
    INVALID ADDRESS: 1.1.1.355
    
    Address:   192.168.1.1          11000000.10101000.00000001. 00000001
    Netmask:   255.255.255.0 = 24   11111111.11111111.11111111. 00000000
    Wildcard:  0.0.0.255            00000000.00000000.00000000. 11111111
    =>
    Network:   192.168.1.0/24       11000000.10101000.00000001. 00000000
    HostMin:   192.168.1.1          11000000.10101000.00000001. 00000001
    HostMax:   192.168.1.254        11000000.10101000.00000001. 11111110
    Broadcast: 192.168.1.255        11000000.10101000.00000001. 11111111
    Hosts/Net: 254                   Class C, Private Internet
    

    There is a great page showing how to use it in scripting, etc, here: SleeplessBeastie's Notes

    0 讨论(0)
  • 2020-12-05 07:30

    If you're using bash, you can do a simple regex match for the pattern, without validating the quads:

    #!/usr/bin/env bash
    
    ip=1.2.3.4
    
    if [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
      echo "success"
    else
      echo "fail"
    fi
    

    If you're stuck with a POSIX shell, then you can use expr to do basically the same thing, using BRE instead of ERE:

    #!/bin/sh
    
    ip=1.2.3.4
    
    if expr "$ip" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then
      echo "success"
    else
      echo "fail"
    fi
    

    Note that expr assumes that your regex is anchored to the left-hand-side of the string, so the initial ^ is unnecessary.

    If it's important to verify that each quad is less than 256, you'll obviously require more code:

    #!/bin/sh
    
    ip=${1:-1.2.3.4}
    
    if expr "$ip" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then
      for i in 1 2 3 4; do
        if [ $(echo "$ip" | cut -d. -f$i) -gt 255 ]; then
          echo "fail ($ip)"
          exit 1
        fi
      done
      echo "success ($ip)"
      exit 0
    else
      echo "fail ($ip)"
      exit 1
    fi
    

    Or perhaps even with fewer pipes:

    #!/bin/sh
    
    ip=${1:-1.2.3.4}
    
    if expr "$ip" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then
      IFS=.
      set $ip
      for quad in 1 2 3 4; do
        if eval [ \$$quad -gt 255 ]; then
          echo "fail ($ip)"
          exit 1
        fi
      done
      echo "success ($ip)"
      exit 0
    else
      echo "fail ($ip)"
      exit 1
    fi
    

    Or again, if your shell is bash, you could use a cumbersome regular expression for quad validation if you're not fond of arithmetic:

    #!/usr/bin/env bash
    
    ip=${1:-1.2.3.4}
    
    re='^(0*(1?[0-9]{1,2}|2([0-4][0-9]|5[0-5]))\.){3}'
     re+='0*(1?[0-9]{1,2}|2([‌​0-4][0-9]|5[0-5]))$'
    
    if [[ $ip =~ $re ]]; then
      echo "success"
    else
      echo "fail"
    fi
    

    This could also be expressed in BRE, but that's more typing than I have in my fingers.

    And lastly, if you like the idea of putting this functionality ... in a function:

    #!/usr/bin/env bash
    
    ip=${1:-1.2.3.4}
    
    ipvalid() {
      # Set up local variables
      local ip=${1:-1.2.3.4}
      local IFS=.; local -a a=($ip)
      # Start with a regex format test
      [[ $ip =~ ^[0-9]+(\.[0-9]+){3}$ ]] || return 1
      # Test values of quads
      local quad
      for quad in {0..3}; do
        [[ "${a[$quad]}" -gt 255 ]] && return 1
      done
      return 0
    }
    
    if ipvalid "$ip"; then
      echo "success ($ip)"
      exit 0
    else
      echo "fail ($ip)"
      exit 1
    fi
    

    There are many ways you could do this. I've shown you just a few.

    0 讨论(0)
  • 2020-12-05 07:30

    Use ipcalc

    $ ipcalc -cs 10.10.10.257 && echo vail_ip || echo invalid_ip
    invalid_ip
    
    0 讨论(0)
  • 2020-12-05 07:30

    This single regex should validate only those addresses between 0.0.0.0 and 255.255.255.255:

    #!/bin/bash
    
    ip="1.2.3.4"
    
    if [[ "$ip" =~ ^(([1-9]?[0-9]|1[0-9][0-9]|2([0-4][0-9]|5[0-5]))\.){3}([1-9]?[0-9]|1[0-9][0-9]|2([0-4][0-9]|5[0-5]))$ ]]; then
      echo "success"
    else
      echo "fail"
    fi
    
    0 讨论(0)
  • 2020-12-05 07:31

    How about this?

    # ip route get 10.10.10.100 > /dev/null 2>&1  ; echo $?
    0
    
    # ip route get 10.10.10.300 > /dev/null 2>&1  ; echo $?
    1
    

    Since the "ip" command checks the validity of IP in itself.

    0 讨论(0)
  • 2020-12-05 07:32
    #!/bin/bash
    read -p " ip: " req_ipadr
    #
    ip_full=$(echo $req_ipadr | sed -n 's/^\(\(\([1-9][0-9]\?\|[1][0-9]\{0,2\}\|[2][0-4][0-9]\|[2][5][0-4]\)\.\)\{3\}\([1-9][0-9]\?\|[1][0-9]\{0,2\}\|[2][0-4][0-9]\|[2][5][0-4]\)\)$/\1/p')
    #
    [ "$ip_full" != "" ] && echo "$req_ipadr vaild ip" || echo "$req_ipadr invaild ip"
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题