{"id":478,"date":"2021-09-20T07:58:00","date_gmt":"2021-09-20T07:58:00","guid":{"rendered":"https:\/\/262235.xyz\/?p=478"},"modified":"2021-09-20T07:58:00","modified_gmt":"2021-09-20T07:58:00","slug":"478","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2021\/09\/20\/478\/","title":{"rendered":"Python\u811a\u672c\u548cBash\u547d\u4ee4\u7edf\u8ba1\u8bbf\u95ee\u722c\u866b\u7684IP\u5730\u5740\u548c\u57ce\u5e02\u4fe1\u606f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/09\/1832544416.png\" alt=\"ipinfo.png\" title=\"ipinfo.png\"><\/p>\n<h2>\u9996\u5148\u90e8\u7f72 IP\u5730\u5740\u4fe1\u606f\u5e93\uff0c\u53c2\u8003 <a href=\"https:\/\/github.com\/hongwenjun\/ip\">https:\/\/github.com\/hongwenjun\/ip<\/a><\/h2>\n<h2>\u7edf\u8ba1\u65e5\u5fd7\u4e2dIP\u6392\u540d\u5199\u5230iplist.txt\uff0c\u6279\u91cf\u67e5IP\u5730\u5740\u4fe1\u606f\uff0c\u6700\u65b0\u65e5\u5fd7100\u6761<\/h2>\n<ul>\n<li>\u53c2\u8003\u5982\u4e0b <code>Bash<\/code> \u547d\u4ee4\uff0c\u7b2c\u4e00\u884c\u4e2d\u662f\u67e5\u770b \u4e00\u4e2a\u5bb9\u5668\u7684\u65e5\u5fd7<\/li>\n<li>\n<p>\u5982\u679c\u4e0d\u662f <code>Docker<\/code> \u73af\u5883\u90e8\u7f72 <code>Nginx<\/code>\uff0c\u65e5\u5fd7\u6587\u4ef6\u662f <code>\/var\/log\/nginx\/access.log<\/code><\/p>\n<pre><code>docker logs nginx-php 2&gt;\/dev\/null \\r\n| grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' \\r\n| sort | uniq -c| sort -nrk 1 | head -n 100 | tee iplist.txt<\/code><\/pre>\n<h2>\u4f7f\u7528 <code>python3 ipinfo.py<\/code> \u547d\u4ee4\u8bfb\u53d6 <code>iplist.txt<\/code> \u8f93\u51fa IP\u5730\u5740\u548c\u57ce\u5e02<\/h2>\n<pre><code># ipinfo.py\nimport re, ipdb, ipaddress\ndb = ipdb.BaseStation(\"\/app\/qqwry.ipdb\")\nwith open(\"\/app\/iplist.txt\", \"r\") as f:\n  data = f.read()\n\niplist = re.findall(r\"\b(?:[0-9]{1,3}.){3}[0-9]{1,3}\b\", data)\nfor ip in iplist:\n try:\n    ipaddress.ip_address(ip).is_global\n    city = db.find(ip, \"CN\")\n    print(ip + \" @\" + city[0] + city[1] + city[2] + city[3])\n except:\n    pass\n\n# \u7edf\u8ba1\u65e5\u5fd7\u4e2dIP\u6392\u540d\u5199\u5230iplist.txt\uff0c\u6279\u91cf\u67e5IP\u5730\u5740\u4fe1\u606f\uff0c\u6700\u65b0\u65e5\u5fd7100\u6761<\/code><\/pre>\n<p>docker logs nginx-php 2&gt;\/dev\/null \\r<br \/>| grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' \\r<br \/>| sort | uniq -c| sort -nrk 1 | head -n 100 | tee iplist.txt<\/p>\n<\/li>\n<\/ul>\n<p>docker exec -it python3 python3 ipinfo.py<\/p>\n<p>docker logs nginx-php 2&gt;\/dev\/null | tail -100  \\r<br \/>  | grep -oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' \\r<br \/>  | sort | uniq -c| sort -nrk 1 | head -n 100 | tee iplist.txt<\/p>\n<h3>\u6211\u7684\u73af\u5883\u90fd\u662fDocker\u90e8\u7f72\u7684\uff0c\u6240\u4ee5\u4f7f\u7528\u811a\u672c\u6ce8\u91ca\u4e2d\u7684 \u547d\u4ee4\u884c\u5c31\u53ef\u4ee5\u5f88\u65b9\u83b7\u5f97\u4fe1\u606f\u3002<\/h3>\n<ul>\n<li>\u5982\u679c\u4f60\u7684\u73af\u5883\u4e0d\u540c\uff0c\u8bf7\u4fee\u6539\u5bf9\u5e94\u6587\u4ef6\u7684\u5b9e\u9645\u76ee\u5f55<\/li>\n<li>\u5b9e\u9645\u4f7f\u7528\u6548\u679c\uff0c\u4e24\u4e2a\u547d\u4ee4\u67e5\u8be2\u6700\u65b0\u8bbf\u95ee\u670d\u52a1\u5668\u7684iP<br \/><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/09\/2089587698.png\" alt=\"ip.png\" title=\"ip.png\"><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\u90e8\u7f72 IP\u5730\u5740\u4fe1\u606f\u5e93\uff0c\u53c2\u8003 https:\/\/github.com\/hongwenjun\/ip \u7edf [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,12],"tags":[23,35,45],"class_list":["post-478","post","type-post","status-publish","format-standard","hentry","category-vps","category-learn","tag-linux","tag-nginx","tag-python"],"_links":{"self":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/comments?post=478"}],"version-history":[{"count":0,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/478\/revisions"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}