{"id":120320,"date":"2018-09-21T08:10:08","date_gmt":"2018-09-21T00:10:08","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=120320"},"modified":"2018-09-17T09:19:47","modified_gmt":"2018-09-17T01:19:47","slug":"linux-shell-ip","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-shell-ip.html","title":{"rendered":"Linux Shell\u5982\u4f55\u5224\u65adIP\u7684\u5408\u6cd5\u6027\uff1f"},"content":{"rendered":"
\u8fd0\u7ef4\u89d2\u5ea6\u6765\u8bf4\uff0c\u5199shell\u811a\u672c\u7ecf\u5e38\u4f1a\u9047\u5230\u5224\u65ad\u8f93\u5165\u7684\u503c\u662f\u5426\u5408\u6cd5\uff0c\u6bd4\u5982IP\u3001\u90ae\u4ef6\u5730\u5740\u7b49\u3002\u90a3\u4e48\uff0c\u6839\u636e\u81ea\u8eab\u5199\u811a\u672c\u4e2d\u603b\u7ed3\u7684\u5224\u65adIP\u5408\u6cd5\u6027\u811a\u672c\u5206\u4eab\u7ed9\u7f51\u53cb\uff0c\u9047\u5230\u65f6\u80fd\u6709\u6240\u53c2\u8003\u3002<\/p>\n
\u601d\u8def\uff1aIP\u7531\u56db\u4f4d\u6570\u5b57\u7ec4\u6210\uff0c\u4ee5\u70b9\u5206\u5272\uff0c\u6bcf\u4e2a\u5b57\u6bb5\u4e0d\u80fd\u5927\u4e8e255\uff0c\u5fc5\u987b\u7b26\u5408\u8fd9\u79cd\u683c\u5f0f\u3002<\/p>\n
\u65b9\u6cd51\uff1a<\/strong><\/p>\n \u65b9\u6cd52\uff1a<\/strong><\/p>\n \u52a0\u4e2a\u5faa\u73af\uff0c\u5982\u679c\u9519\u8bef\u5219\u91cd\u65b0\u8f93\u5165\uff0c\u76f4\u5230\u6b63\u786e\uff1a<\/strong><\/p>\n \u539f\u6587\u6765\u81ea\uff1ahttp:\/\/lizhenliang.blog.51cto.com\/7876557\/1736160<\/a><\/p>\n#!\/bin\/bash \u00a0 \u00a0 \r\n<\/span>function check_ip<\/span>() { \u00a0 \r\n \u00a0 \u00a0IP=$1<\/span> \u00a0 \r\n \u00a0 \u00a0VALID_CHECK=$(echo<\/span> $IP<\/span>|awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print \"yes\"}'<\/span>) \u00a0 \r\n \u00a0 \u00a0if<\/span> echo<\/span> $IP<\/span>|grep -E \"^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$\"<\/span>>\/dev\/null; then<\/span> \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0if<\/span> [ ${VALID_CHECK:-no}<\/span> == \"yes\"<\/span> ]; then<\/span> \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0echo<\/span> \"IP $IP available.\"<\/span> \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0else<\/span> \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0echo<\/span> \"IP $IP not available!\"<\/span> \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0fi<\/span> \u00a0 \r\n \u00a0 \u00a0else<\/span> \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0echo<\/span> \"IP format error!\"<\/span> \u00a0 \r\n \u00a0 \u00a0fi<\/span> \u00a0 } \u00a0 \r\n# Example \u00a0 \r\n<\/span>check_ip 192.168<\/span>.1.1<\/span> \u00a0 \r\ncheck_ip 256.1<\/span>.1.1<\/span><\/pre>\n
#!\/bin\/bash \u00a0 \u00a0 \r\n<\/span>function check_ip<\/span>() { \u00a0 \r\n \u00a0 \u00a0IP=$1<\/span> \u00a0 \r\n \u00a0 \u00a0if<\/span> [[ $IP<\/span> =~ ^[0<\/span>-9<\/span>]{1<\/span>,3<\/span>}\\.[0<\/span>-9<\/span>]{1<\/span>,3<\/span>}\\.[0<\/span>-9<\/span>]{1<\/span>,3<\/span>}\\.[0<\/span>-9<\/span>]{1<\/span>,3<\/span>}$ ]]; then<\/span> \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0FIELD1=$(echo<\/span> $IP<\/span>|cut -d. -f<\/span>1) \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0FIELD2=$(echo<\/span> $IP<\/span>|cut -d. -f<\/span>2) \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0FIELD3=$(echo<\/span> $IP<\/span>|cut -d. -f<\/span>3) \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0FIELD4=$(echo<\/span> $IP<\/span>|cut -d. -f<\/span>4) \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0if<\/span> [ $FIELD1<\/span> -le 255<\/span> -a<\/span> $FIELD2<\/span> -le 255<\/span> -a<\/span> $FIELD3<\/span> -le 255<\/span> -a<\/span> $FIELD4<\/span> -le 255<\/span> ]; then<\/span> \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0echo<\/span> \"IP $IP available.\"<\/span> \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0else<\/span> \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0echo<\/span> \"IP $IP not available!\"<\/span> \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0fi<\/span> \u00a0 \r\n \u00a0 \u00a0else<\/span> \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0echo<\/span> \"IP format error!\"<\/span> \u00a0 \r\n \u00a0 \u00a0fi<\/span> \u00a0 } \u00a0 \r\n# Example \u00a0 \r\n<\/span>check_ip 192.168<\/span>.1.1<\/span> \u00a0 \r\ncheck_ip 256.1<\/span>.1.1<\/span><\/pre>\n
#!\/bin\/bash \u00a0 \u00a0 \r\n<\/span>function check_ip<\/span>() { \u00a0 \r\n \u00a0 \u00a0local IP=$1<\/span> \u00a0 \r\n \u00a0 \u00a0VALID_CHECK=$(echo<\/span> $IP<\/span>|awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print \"yes\"}'<\/span>) \u00a0 \r\n \u00a0 \u00a0if<\/span> echo<\/span> $IP<\/span>|grep -E \"^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$\"<\/span> >\/dev\/null; then<\/span> \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0if<\/span> [ $VALID_CHECK<\/span> == \"yes\"<\/span> ]; then<\/span> \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0 echo<\/span> \"IP $IP \u00a0available!\"<\/span> \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return<\/span> 0<\/span> \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0else<\/span> \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0echo<\/span> \"IP $IP not available!\"<\/span> \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return<\/span> 1<\/span> \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0fi<\/span> \u00a0 \r\n \u00a0 \u00a0else<\/span> \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0echo<\/span> \"IP format error!\"<\/span> \u00a0 \r\n \u00a0 \u00a0 \u00a0 \u00a0return<\/span> 1<\/span> \u00a0 \r\n \u00a0 \u00a0fi<\/span> \u00a0 } \u00a0 \r\nwhile<\/span> true<\/span>; do<\/span> \u00a0 \r\n \u00a0 \u00a0read<\/span> -p \"Please enter IP: \"<\/span> IP \u00a0 \r\n \u00a0 \u00a0check_ip $IP<\/span> \u00a0 \r\n \u00a0 \u00a0[ $? -eq<\/span> 0<\/span> ] && break<\/span> \u00a0 \r\ndone<\/span><\/pre>\n
\n