{"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

#!\/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

\u65b9\u6cd52\uff1a<\/strong><\/p>\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

\u52a0\u4e2a\u5faa\u73af\uff0c\u5982\u679c\u9519\u8bef\u5219\u91cd\u65b0\u8f93\u5165\uff0c\u76f4\u5230\u6b63\u786e\uff1a<\/strong><\/p>\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

\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/lizhenliang.blog.51cto.com\/7876557\/1736160<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttps:\/\/lrxjmw.cn\/linux-shell-ip.html<\/a>\u7f16\u8f91\uff1a\u6731\u57f9\u68cb\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"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 […]<\/p>\n","protected":false},"author":63,"featured_media":120322,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-120320","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/120320","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=120320"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/120320\/revisions"}],"predecessor-version":[{"id":120538,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/120320\/revisions\/120538"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/120322"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=120320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=120320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=120320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}