Check for IP validity

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

    i tweaked all the codes and found this to be helpful.

    #!/bin/bash
    
    ip="256.10.10.100"
    
    if [[ "$ip" =~ (([01]{,1}[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.([01]{,1}[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.([01]{,1}[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.([01]{,1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]))$ ]]; then
      echo "success"
    else
      echo "fail"
    fi
    

提交回复
热议问题