{"id":2707,"date":"2016-05-25T22:58:04","date_gmt":"2016-05-25T14:58:04","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2707"},"modified":"2016-05-25T22:58:04","modified_gmt":"2016-05-25T14:58:04","slug":"python%e7%9a%84netaddr%e6%a8%a1%e5%9d%97%e4%bd%bf%e7%94%a8%e8%ae%b0%e5%bd%95","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2707.html","title":{"rendered":"Python\u7684netaddr\u6a21\u5757\u4f7f\u7528\u8bb0\u5f55"},"content":{"rendered":"<p class=\"p1\"><span class=\"s1\">=Start=<\/span><\/p>\n<h5 class=\"p1\"><span class=\"s1\">\u7f18\u7531\uff1a<\/span><\/h5>\n<p class=\"p1\"><span class=\"s1\">\u524d\u6bb5\u65f6\u95f4\u56e0\u4e3a\u5de5\u4f5c\u4e0a\u7684\u9700\u6c42\uff0c\u9700\u8981\u5199\u4e00\u4e2a\u5c0f\u5de5\u5177\u5904\u7406IP\u5730\u5740\u76f8\u5173\u7684\u95ee\u9898\uff0c\u7528shell\u5199\u7684\u8bdd\u592a\u590d\u6742\u800c\u4e14\u7a0b\u5e8f\u4e0d\u6613\u7ef4\u62a4\u548c\u79fb\u690d\uff0c\u6240\u4ee5\u51b3\u5b9a\u7528Python\u6765\u5904\u7406\u3002\u8003\u8651\u5230Python\u4e2d\u5df2\u7ecf\u6709\u5f88\u591a\u73b0\u6210\u7684\u6a21\u5757\u53ef\u4ee5\u7528\u6765\u8fdb\u884cIP\u5730\u5740\u7684\u5904\u7406\uff0c\u5728\u641c\u7d22\u6bd4\u8f83\u4e86\u51e0\u4e2a\u4e4b\u540e\u9009\u62e9\u4e86netaddr\u6a21\u5757\u3002\u4e0b\u9762\u5c31\u662f\u4f7f\u7528\u8fc7\u7a0b\u7684\u8bb0\u5f55\uff0c\u8bb0\u5f55\u81f3\u6b64\u505a\u4e2a\u5907\u5fd8\u3002<\/span><\/p>\n<h5 class=\"p1\"><span class=\"s1\">\u00a0<\/span>\u6b63\u6587\uff1a<\/h5>\n<h6>0.\u5b89\u88c5<\/h6>\n<p><a class=\"external-link\" href=\"https:\/\/pypi.python.org\/pypi\/netaddr\" target=\"_blank\" rel=\"nofollow\">https:\/\/pypi.python.org\/pypi\/netaddr<\/a><\/p>\n<p>$ pip search netaddr<br \/>\n$ sudo -H pip install netaddr<\/p>\n<h6>1.\u7b80\u5355\u7684\u4f7f\u7528<\/h6>\n<ul>\n<li><a class=\"external-link\" href=\"https:\/\/pythonhosted.org\/netaddr\/index.html\" target=\"_blank\" rel=\"nofollow\">https:\/\/pythonhosted.org\/netaddr\/index.html<\/a><\/li>\n<li><a class=\"external-link\" href=\"https:\/\/pythonhosted.org\/netaddr\/tutorial_01.html\" target=\"_blank\" rel=\"nofollow\">https:\/\/pythonhosted.org\/netaddr\/tutorial_01.html<\/a><\/li>\n<li><a class=\"external-link\" href=\"http:\/\/stackoverflow.com\/questions\/6708272\/list-of-ip-addresses-in-python-to-a-list-of-cidr\" target=\"_blank\" rel=\"nofollow\">http:\/\/stackoverflow.com\/questions\/6708272\/list-of-ip-addresses-in-python-to-a-list-of-cidr<\/a><\/li>\n<li><a class=\"external-link\" href=\"http:\/\/stackoverflow.com\/questions\/24214441\/ip-range-to-cidr-conversion-in-python\" target=\"_blank\" rel=\"nofollow\">http:\/\/stackoverflow.com\/questions\/24214441\/ip-range-to-cidr-conversion-in-python<\/a><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">In [1]: from netaddr import *\n\nIn [2]: ip = IPNetwork('192.0.2.5\/24')\n\nIn [3]: ip.\n#\u4e0b\u9762\u4ec5\u5217\u51fa\u4e00\u4e9b\u6211\u5e38\u7528\u7684\u65b9\u6cd5\/\u51fd\u6570\nip.broadcast \u00a0#IP\u6bb5\u7684\u5e7f\u64ad\u5730\u5740\nip.cidr \u00a0#IP\u6bb5\u7684CIDR\u5f62\u5f0f\nip.netmask \u00a0#IP\u6bb5\u7684\u7f51\u7edc\u63a9\u7801\nip.first \u00a0#IP\u6bb5\u7684\u8d77\u59cbIP\u5730\u5740\u2014\u2014\u4ee5\u6574\u6570\u5f62\u5f0f\u8868\u793a\nip.last \u00a0#IP\u6bb5\u7684\u7ed3\u675fIP\u5730\u5740\u2014\u2014\u4ee5\u6574\u6570\u5f62\u5f0f\u8868\u793a\nip.size \u00a0#IP\u6bb5\u7684\u5927\u5c0f(\u5305\u542b\u591a\u5c11\u4e2aIP\u5730\u5740)\nip.value \u00a0#IP\u5730\u5740\u2014\u2014\u4ee5\u6574\u6570\u5f62\u5f0f\u8868\u793a\nip.ip \u00a0#IP\u5730\u5740\nip.network \u00a0#IP\u6240\u5728\u7f51\u6bb5\n\nIn [3]: type(ip), ip\nOut[3]: (netaddr.ip.IPNetwork, IPNetwork('192.0.2.5\/24'))\n\nIn [4]: ip.first, ip.last\nOut[4]: (3221225984, 3221226239)\n\nIn [5]: ip.broadcast, ip.network\nOut[5]: (IPAddress('192.0.2.255'), IPAddress('192.0.2.0'))\n\nIn [7]: ip.broadcast, ip.network, ip.netmask, ip.hostmask\nOut[7]:\n(IPAddress('192.0.2.255'),\nIPAddress('192.0.2.0'),\nIPAddress('255.255.255.0'),\nIPAddress('0.0.0.255'))\n\nIn [8]: ip.ip\nOut[8]: IPAddress('192.0.2.5')\n\nIn [9]: ip.value\nOut[9]: 3221225989\n\nIn [11]: ip.ip.bits()\nOut[11]: '11000000.00000000.00000010.00000101'\n\nIn [12]: ip.size\nOut[12]: 256\n\nIn [14]: ip.netmask.value \u00a0#IP\u6bb5\u5b50\u7f51\u63a9\u7801\u503c\u7684\u5341\u8fdb\u5236\u6574\u6570\u683c\u5f0f\nOut[14]: 4294967040\n\nIn [15]: ip.netmask.words\nOut[15]: (255, 255, 255, 0)<\/pre>\n<h6 class=\"p1\"><span class=\"s1\">2.netaddr\u6a21\u5757\u63d0\u4f9b\u7684\u9ad8\u7ea7\u529f\u80fd<\/span><\/h6>\n<p><a href=\"https:\/\/pythonhosted.org\/netaddr\/tutorial_03.html\">https:\/\/pythonhosted.org\/netaddr\/tutorial_03.html<\/a><\/p>\n<ul>\n<li>IP\u6bb5\u5408\u5e76\uff08cidr_merge\uff09<\/li>\n<li>IP\u533a\u95f4\uff08IPRange\u3001IPRange.cidrs()\u3001IPSet\uff09<\/li>\n<li>IP\u533a\u95f4\u7684 <a href=\"https:\/\/pythonhosted.org\/netaddr\/tutorial_03.html#unions-intersections-and-differences\">\u4ea4\u96c6\u3001\u5e76\u96c6\u3001\u5dee\u96c6<\/a><\/li>\n<li><a href=\"https:\/\/pythonhosted.org\/netaddr\/tutorial_03.html#convert-an-ip-set-to-an-ip-range\">\u5c06IP\u96c6\u5408\u8f6c\u6362\u6210IP\u533a\u95f4<\/a>\uff08IPSet.iprange()\u3001IPSet.iter_ipranges()\uff09<\/li>\n<\/ul>\n<h6 class=\"p1\"><span class=\"s1\">3.\u4f7f\u7528\u5b9e\u4f8b<\/span><\/h6>\n<p class=\"p1\"><span class=\"s1\">\u4ece\u67d0\u4e00IP\u6bb5\u4e2d\u6392\u9664\u6307\u5b9a\u7684IP\u5730\u5740\u3001IP\u533a\u95f4(\u540c\u65f6\u652f\u6301\u300cCIDR\u300d\u548c\u300c192.168.1.1-192.168.1.5\u300d\u8fd9\u4e24\u79cd\u683c\u5f0f)\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/span><\/p>\n<pre class=\"lang:default decode:true\">#!\/usr\/bin\/env python\n# coding=utf-8\nimport sys, time\nfrom netaddr import *\n\ndef main():\n    ipset = IPSet()\n    ipset.add(sys.argv[1].strip())\n    for item in sys.argv[2:]:\n        if \"-\" in item:\n            ipset.remove(IPRange(item.split(\"-\")[0], item.split(\"-\")[1]))\n        else:\n            ipset.remove(item)\n    for item in ipset.iter_ipranges():\n        print item\n\nif __name__ == '__main__':\n    time_start = time.time()\n    try:\n        main()\n    except KeyboardInterrupt:\n        print 'Killed by user'\n        sys.exit(0)\n    print \"\\nSpend {0} seconds.\\n\".format(time.time() - time_start)<\/pre>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u524d\u6bb5\u65f6\u95f4\u56e0\u4e3a\u5de5\u4f5c\u4e0a\u7684\u9700\u6c42\uff0c\u9700\u8981\u5199\u4e00\u4e2a\u5c0f\u5de5\u5177\u5904\u7406IP\u5730\u5740\u76f8\u5173\u7684\u95ee\u9898\uff0c\u7528shell\u5199\u7684\u8bdd\u592a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7,12],"tags":[614,8,50],"class_list":["post-2707","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-tools","tag-netaddr","tag-python","tag-scripts"],"views":11911,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2707"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2707\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}