{"id":6528,"date":"2014-07-31T23:49:30","date_gmt":"2014-07-31T15:49:30","guid":{"rendered":"http:\/\/jpuyy.com\/?p=6528"},"modified":"2014-08-04T14:34:16","modified_gmt":"2014-08-04T06:34:16","slug":"python-netsnmp-cisco","status":"publish","type":"post","link":"https:\/\/jpuyy.com\/?p=6528","title":{"rendered":"\u901a\u8fc7python netsnmp\u8bfb\u53d6cisco\u4ea4\u6362\u673a\u4fe1\u606f"},"content":{"rendered":"<p>\u9996\u5148\u4f7f\u7528snmpwalk\u8dd1\u4e00\u904d\u770b\u4e00\u4e0b\u6709\u6ca1\u6709\u95ee\u9898<\/p>\n<pre>snmpwalk -v 2c -c public 10.103.33.1<\/pre>\n<p>\u8fd9\u91cc\u6d4b\u8bd5\u7528\u4ea4\u6362\u673a\u662f WS-C2960G-24TC-L\uff0c\u4ee5\u4e0b\u811a\u672c\u7528\u4e8e\u8bfb\u53d6\u7ba1\u7406ip,\u5e8f\u5217\u53f7,\u578b\u53f7,\u4e3b\u673a\u540d\u3002\u601d\u79d1\u7684\u4ea4\u6362\u673asnmp oid\u4fe1\u606f\u90fd\u53ef\u901a\u8fc7\u5982\u4e0b\u7f51\u5740\u67e5\u8be2http:\/\/tools.cisco.com\/Support\/SNMP\/do\/BrowseOID.do<\/p>\n<p>\u9996\u5148\u5b89\u88c5python\u7684snmp\u4f9d\u8d56\u5305<\/p>\n<pre>yum install net-snmp-python<\/pre>\n<p>\u83b7\u53d6\u4fe1\u606f\u7684\u811a\u672c<\/p>\n<pre>\r\n#!\/usr\/bin\/env python\r\n# by yangyang89\r\n# using snmp get switch serial, model, manage ip ..\r\nimport netsnmp\r\nimport sys\r\nimport urllib\r\nimport urllib2\r\n\r\n# reference python for linux and unix administration page 209\r\nclass Snmp(object):\r\n    \"\"\"A basic SNMP session\"\"\"\r\n    def __init__(self,oid=\"sysDescr\", Version=2):\r\n        self.oid = oid\r\n        self.version = Version\r\n        self.destHost = sys.argv[1]\r\n        self.community = sys.argv[2]\r\n\r\n    def query(self):\r\n        \"\"\"Creates SNMP query session\"\"\"\r\n        try:\r\n            result = netsnmp.snmpwalk(self.oid, Version = self.version, DestHost = self.destHost, Community = self.community)\r\n        except Exception, err:\r\n            print err\r\n            result = None\r\n        return result\r\n\r\nprint sys.argv[1] + sys.argv[2]\r\nif sys.argv[1] and sys.argv[2]:\r\n    s = Snmp()\r\n    #print s.query()\r\n    #s.oid = \"2.47.1.1.1.1.11.1001\"\r\n    #http:\/\/tools.cisco.com\/Support\/SNMP\/do\/BrowseOID.do\r\n    s.oid = \".1.3.6.1.2.1.4.20.1.1\" # manage ip ipAdEntAddr\r\n    ip = s.query()\r\n    telnet = ip[0]\r\n    print \"ip: \" + telnet\r\n    \r\n    s.oid = \".1.3.6.1.4.1.9.3.6.3\" # serial numbers chassisId\r\n    serial = s.query()\r\n    serial = serial[0]\r\n    print \"serial: \" + serial\r\n    \r\n    s.oid = \".1.3.6.1.2.1.47.1.1.1.1\" # product_model entPhysicalEntry\r\n    product_model = s.query()\r\n    product_model = product_model[1].split(' ')[0]\r\n    print \"product_model: \" + product_model\r\n    #print s.query()\r\n    \r\n    s.oid = \".1.3.6.1.4.1.9.2.1.3\" # hostname hostName\r\n    hostname = s.query()\r\n    hostname = hostname[0]\r\n    print \"hostname: \" + hostname\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\u4f7f\u7528snmpwalk\u8dd1\u4e00\u904d\u770b\u4e00\u4e0b\u6709\u6ca1\u6709\u95ee\u9898 snmpwalk -v 2c -c public 10.103.33.1 \u8fd9\u91cc\u6d4b\u8bd5\u7528\u4ea4\u6362\u673a\u662f WS-C2960G-24TC-L\uff0c\u4ee5\u4e0b\u811a\u672c\u7528\u4e8e\u8bfb\u53d6\u7ba1\u7406ip,\u5e8f\u5217\u53f7,\u578b\u53f7,\u4e3b\u673a\u540d\u3002\u601d\u79d1\u7684\u4ea4\u6362\u673asnmp oid\u4fe1\u606f\u90fd\u53ef\u901a\u8fc7\u5982\u4e0b\u7f51\u5740\u67e5\u8be2http:\/\/tools.cisco.com\/Support\/SNMP\/do\/BrowseOID.do \u9996\u5148\u5b89\u88c5python\u7684snmp\u4f9d\u8d56\u5305 yum install net-snmp-python \u83b7\u53d6\u4fe1\u606f\u7684\u811a\u672c #!\/usr\/bin\/env python # by yangyang89 # using snmp get switch serial, model, manage ip .. import netsnmp import sys import urllib import urllib2 # reference python for linux and unix administration page 209 class Snmp(object): &#8220;&#8221;&#8221;A basic SNMP session&#8221;&#8221;&#8221; def [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[],"class_list":["post-6528","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/6528","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6528"}],"version-history":[{"count":5,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/6528\/revisions"}],"predecessor-version":[{"id":6584,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/6528\/revisions\/6584"}],"wp:attachment":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}