{"id":121131,"date":"2018-10-01T08:26:38","date_gmt":"2018-10-01T00:26:38","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=121131"},"modified":"2018-09-25T11:28:20","modified_gmt":"2018-09-25T03:28:20","slug":"shell-linux-ip","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/shell-linux-ip.html","title":{"rendered":"\u6559\u4f60\u7528shell\u811a\u672c\u5728Linux\u4e0a\u83b7\u53d6\u5916\u7f51IP\u5730\u5740"},"content":{"rendered":"
\u57fa\u4e8eLinux\u7cfb\u7edf\u7684\u83b7\u53d6\u5916\u7f51IP\u5730\u5740\u7684shell\u811a\u672c\uff0c\u811a\u672c\u5185\u5bb9\u5982\u4e0b\uff1a
\ngetIp.sh<\/p>\n
#!\/bin\/bash\r\n#\u68c0\u67e5IP\u7684\u5408\u6cd5\u6027\r\ncheck_ipaddr()\r\n{\r\necho $1|grep \"^[0-9]\\{1,3\\}\\.\\([0-9]\\{1,3\\}\\.\\)\\{2\\}[0-9]\\{1,3\\}$\" > \/dev\/null;\r\nif [ $? -ne 0 ]\r\nthen\r\n#echo \"IP\u5730\u5740\u5fc5\u987b\u5168\u90e8\u4e3a\u6570\u5b57\"\r\nreturn 1\r\nfi\r\nipaddr=$1\r\na=`echo $ipaddr|awk -F . '{print $1}'` #\u4ee5\".\"\u5206\u9694\uff0c\u53d6\u51fa\u6bcf\u4e2a\u5217\u7684\u503c\r\nb=`echo $ipaddr|awk -F . '{print $2}'`\r\nc=`echo $ipaddr|awk -F . '{print $3}'`\r\nd=`echo $ipaddr|awk -F . '{print $4}'`\r\nfor num in $a $b $c $d\r\ndo\r\nif [ $num -gt 255 ] || [ $num -lt 0 ] #\u6bcf\u4e2a\u6570\u503c\u5fc5\u987b\u57280-255\u4e4b\u95f4\r\nthen\r\n#echo $ipaddr \"\u4e2d\uff0c\u5b57\u6bb5\"$num\"\u9519\u8bef\"\r\nreturn 1\r\nfi\r\ndone\r\n#echo $ipaddr \"\u5730\u5740\u5408\u6cd5\"\r\nreturn 0\r\n}\r\n\r\nhost=ns1.dnspod.net\r\nport=16666\r\nip=`cat<\/dev\/tcp\/$host\/$port`\r\n\r\ncheck_ipaddr \"$ip\"\r\n\r\nif [ \"$?\"x = \"0\"x ]; then\r\necho \"\u5916\u7f51IP\u5730\u5740\uff1a$ip\"\r\nelse\r\necho \"\u83b7\u53d6IP\u5730\u5740\u5931\u8d25\uff01\"\r\nfi<\/pre>\n\u4f7f\u7528\u547d\u4ee4 chmod +x getIp.sh <\/span>\u7ed9\u811a\u672c\u6388\u6743\u540e\u5373\u53ef\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n
<\/p>\n
\n\u539f\u6587\u6765\u81ea\uff1ahttps:\/\/blog.csdn.net\/catoop\/article\/details\/81736239<\/a><\/p>\n