Check for IP validity

后端 未结 13 1415
庸人自扰
庸人自扰 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: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.

提交回复
热议问题