shell 用shell判断IP是否私有地址

2026年09月26日 09:34
有1个网友回答
网友(1):

可以通过正则表达式进行判断:
#!/bin/bash
ip='111.111.111.111'
#ip='111.111.111.111'
echo $ip | perl -ne 'exit 1 unless /\b(?:(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5]))\b/'
if [ $? -eq 1 ]
then
echo "true"
else
echo "false"
fi