Advertisement
Izya12

Untitled

Apr 29th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 18.64 KB | None | 0 0
  1. root@Node1:~# cat sa/profiles/Nateks/NetXpert/get_version.py
  2. # -*- coding: utf-8 -*-
  3. # ---------------------------------------------------------------------
  4. # Nateks.flexgain.get_version
  5. # ---------------------------------------------------------------------
  6. # Copyright (C) 2007-2016 The NOC Project
  7. # See LICENSE for details
  8. # ---------------------------------------------------------------------
  9. """
  10. """
  11. # Python modules
  12. import re
  13. # NOC modules
  14. from noc.core.script.base import BaseScript
  15. from noc.sa.interfaces.igetversion import IGetVersion
  16.  
  17.  
  18. class Script(BaseScript):
  19.  
  20.     name = "Nateks.NetXpert.get_version"
  21.     cache = True
  22.     interface = IGetVersion
  23.  
  24.     rx_ver = re.compile(
  25.         r".*Series Software, Version (?P<version>[\S\s]*)\, RELEASE SOFTWARE"
  26.         r".*ROM: System Bootstrap, Version (?P<bootrom>\d+\.\d+\.\d+).*"
  27.         r"Serial num:(?P<sn>[^ ,]+),.*\n"
  28.         r"Nateks (?P<platform>.*) RISC",
  29.         re.MULTILINE | re.DOTALL | re.IGNORECASE)
  30.  
  31.     def execute(self):
  32.         v = self.cli("show version", cached=True)
  33.         match = self.re_search(self.rx_ver, v)
  34.  
  35.         return {
  36.             "vendor": "Nateks",
  37.             "platform": match.group("platform"),
  38.             "version": match.group("version"),
  39.             "attributes": {
  40.                 "Boot PROM": match.group("bootrom"),
  41.                 "SN": match.group("sn"),
  42.             }
  43.         }
  44.  
  45.  
  46.  
  47. root@Node1:/opt/noc# ./noc script --debug --pretty get_version "K1-SW"
  48. 2018-04-29 16:40:56,512 [noc.core.dcs.base] Resolve near service postgres
  49. 2018-04-29 16:40:56,513 [noc.core.http.client] HTTP GET http://consul:8500/v1/health/service/postgres?passing=1&near=_agent
  50. 2018-04-29 16:40:56,515 [noc.core.http.client] HTTP Response 200
  51. 2018-04-29 16:40:56,516 [noc.core.dcs.base] Resolved near service postgres to ['10.220.21.122:5432']
  52. 2018-04-29 16:40:57,404 [noc.core.dcs.base] Running resolver for service mongo
  53. 2018-04-29 16:40:57,405 [noc.core.dcs.base] [mongo] Starting resolver
  54. 2018-04-29 16:40:57,406 [noc.core.http.client] HTTP GET http://consul:8500/v1/health/service/mongo?passing=1
  55. 2018-04-29 16:40:57,407 [noc.core.http.client] HTTP Response 200
  56. 2018-04-29 16:40:57,407 [noc.core.dcs.base] [mongo] Set active services to: mongo: 10.220.21.122:27017
  57. 2018-04-29 16:40:57,408 [noc.core.http.client] HTTP GET http://consul:8500/v1/health/service/mongo?index=2244467&passing=1
  58. 2018-04-29 16:40:57,409 [noc.lib.nosql] Connecting to MongoDB {'username': 'noc', 'authentication_source': 'noc', 'readPreference': 'secondaryPreferred', 'replicaSet': 'noc', 'db': 'noc', 'socketKeepAlive': True, 'host': 'mongodb://noc:********@10.220.21.122:27017/noc', 'password': '********'}
  59. 2018-04-29 16:40:57,474 [noc.core.cache.base] Using cache backend: noc.core.cache.mongo.MongoCache
  60. 2018-04-29 16:40:57,537 [noc.main.models.doccategory] Registering Capability
  61. 2018-04-29 16:40:57,538 [noc.main.models.doccategory] Registering MetricType
  62. 2018-04-29 16:40:57,575 [noc.main.models.doccategory] Registering ConnectionType
  63. 2018-04-29 16:40:57,579 [noc.main.models.doccategory] Registering ObjectModel
  64. 2018-04-29 16:40:57,589 [noc.main.models.textindex] Adding FTS index for sa.managedobject
  65. 2018-04-29 16:40:57,607 [noc.main.models.textindex] Adding FTS index for ip.vrf
  66. 2018-04-29 16:40:57,610 [noc.main.models.textindex] Adding FTS index for vc.vc
  67. 2018-04-29 16:40:57,612 [noc.main.models.textindex] Adding FTS index for ip.prefix
  68. 2018-04-29 16:40:57,617 [noc.main.models.textindex] Adding FTS index for ip.address
  69. 2018-04-29 16:40:57,710 [noc.core.script.loader] Loading script Nateks.NetXpert.get_version
  70. 2018-04-29 16:40:57,739 [noc.core.profile.loader] Loading profile Nateks.NetXpert
  71. 2018-04-29 16:40:57,740 [script] [Nateks.NetXpert.get_version|10.20.52.102] Running. Input arguments: {}, timeout 3600
  72. 2018-04-29 16:40:57,740 [script] [Nateks.NetXpert.get_version|10.20.52.102] Open telnet CLI
  73. 2018-04-29 16:40:57,747 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Creating IOLoop
  74. 2018-04-29 16:40:57,747 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Connecting (u'10.20.52.102', 23)
  75. 2018-04-29 16:40:57,778 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Connected
  76. 2018-04-29 16:40:57,778 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] State: <START>
  77. 2018-04-29 16:40:57,778 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Setting timeout: 60s
  78. 2018-04-29 16:40:57,811 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Received WILL ECHO
  79. 2018-04-29 16:40:57,812 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Send DO ECHO
  80. 2018-04-29 16:40:57,839 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Received WILL SGA
  81. 2018-04-29 16:40:57,839 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Send DO SGA
  82. 2018-04-29 16:40:57,839 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Received DO TTYPE
  83. 2018-04-29 16:40:57,839 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Send WILL TTYPE
  84. 2018-04-29 16:40:57,839 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Received DO NAWS
  85. 2018-04-29 16:40:57,839 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Send WILL NAWS
  86. 2018-04-29 16:40:57,840 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Send IAC SB '\x1f' '\x00\x80\x00\x80' IAC SE
  87. 2018-04-29 16:40:57,840 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Received: 'User Access Verification\r\n\r\nUsername: '
  88. 2018-04-29 16:40:57,840 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Match: ([Uu]ser ?[Nn]ame|[Ll]ogin): ?
  89. 2018-04-29 16:40:57,840 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] State: <USERNAME>
  90. 2018-04-29 16:40:57,840 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Send: 'admin\n'
  91. 2018-04-29 16:40:57,841 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Setting timeout: 30s
  92. 2018-04-29 16:40:57,866 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Received IAC SB 1801 SE
  93. 2018-04-29 16:40:57,866 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Received: '\xf0'
  94. 2018-04-29 16:40:57,907 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Received: 'a'
  95. 2018-04-29 16:40:57,973 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Received: 'dmin\r\n'
  96. 2018-04-29 16:40:58,007 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Received: 'Password: '
  97. 2018-04-29 16:40:58,007 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Match: [Pp]ass[Ww]ord: ?
  98. 2018-04-29 16:40:58,008 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] State: <PASSWORD>
  99. 2018-04-29 16:40:58,008 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Send: 'admin\n'
  100. 2018-04-29 16:40:58,008 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Setting timeout: 30s
  101. 2018-04-29 16:40:58,207 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Received: '\r\n'
  102. 2018-04-29 16:40:58,280 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Received: '\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n                 Welcome to Nateks NetXpert NX-3408 V1 Ethernet Switch\r\n\r\n\r\n\r\n\r\n\r\nK1-SW>'
  103. 2018-04-29 16:40:58,280 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Match: ^\S+?>
  104. 2018-04-29 16:40:58,280 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] State: <UNPRIVILEGED_PROMPT>
  105. 2018-04-29 16:40:58,280 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Send: 'enable\n'
  106. 2018-04-29 16:40:58,281 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Setting timeout: 10s
  107. 2018-04-29 16:40:58,310 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Received: 'e'
  108. 2018-04-29 16:40:58,385 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Received: 'nable\r\nK1-SW#'
  109. 2018-04-29 16:40:58,385 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Match: ^(?P<hostname>[a-zA-Z0-9/.]\S{0,35})(?:[-_\d\w]+)?(?:\(_config[^\)]*\))?#
  110. 2018-04-29 16:40:58,385 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] State: <PROMT>
  111. 2018-04-29 16:40:58,385 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Refining pattern prompt to '^K1\\-SW(?:[-_\\d\\w]+)?(?:\\(_config[^\\)]*\\))?#'
  112. 2018-04-29 16:40:58,386 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Resetting timeouts
  113. 2018-04-29 16:40:58,386 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Send: 'show version\n'
  114. 2018-04-29 16:40:58,415 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Received: 's'
  115. 2018-04-29 16:40:58,415 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Received: 'how version\r\nNateks Networks Co.Ltd Internetwork Operating System Software\r\nNetXpert NX-3408 V1 Series Software, Version 2.2.0C Build 41423, RELEASE SOFTWARE\r\nCopyright (c) 2017 by Nateks Networks Co.Ltd\r\nCompiled: 2017-3-13 12:8:26 by SYS, Image text-base: 0x80008000\r\nROM: System Bootstrap, Version 0.3.8,hardware version:V1.0\r\nSerial num:20013217075, ID num:20013217075\r\nSystem image file is "Switch.bin"\r\nNateks NetXpert NX-3408 V1 RISC\r\n131072K bytes of memory,8192K bytes of flash\r\nBase ethernet MAC Addres'
  116. 2018-04-29 16:40:58,447 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Received: 's: 00:0f:d9:0a:85:e8\r\nsnmp info:\r\n  vend_ID:4249   product_ID:146   system_ID:1.3.6.1.4.1.4249.21.146\r\nK1-SW uptime is 13:08:09:12, The current time: 1970-1-14 8:9:12\r\nK1-SW#'
  117. 2018-04-29 16:40:58,447 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Match: ^K1\-SW(?:[-_\d\w]+)?(?:\(_config[^\)]*\))?#
  118. 2018-04-29 16:40:58,447 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] State: <PROMT>
  119. 2018-04-29 16:40:58,448 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Command: show version
  120. show version
  121. Nateks Networks Co.Ltd Internetwork Operating System Software
  122. NetXpert NX-3408 V1 Series Software, Version 2.2.0C Build 41423, RELEASE SOFTWARE
  123. Copyright (c) 2017 by Nateks Networks Co.Ltd
  124. Compiled: 2017-3-13 12:8:26 by SYS, Image text-base: 0x80008000
  125. ROM: System Bootstrap, Version 0.3.8,hardware version:V1.0
  126. Serial num:20013217075, ID num:20013217075
  127. System image file is "Switch.bin"
  128. Nateks NetXpert NX-3408 V1 RISC
  129. 131072K bytes of memory,8192K bytes of flash
  130. Base ethernet MAC Address: 00:0f:d9:0a:85:e8
  131. snmp info:
  132.   vend_ID:4249   product_ID:146   system_ID:1.3.6.1.4.1.4249.21.146
  133. K1-SW uptime is 13:08:09:12, The current time: 1970-1-14 8:9:12
  134.  
  135. 2018-04-29 16:40:58,453 [script] [Nateks.NetXpert.get_version|10.20.52.102] [telnet] Closing IOLoop
  136. 2018-04-29 16:40:58,624 [noc.core.debug] UNHANDLED EXCEPTION (2018-04-29 16:40:58.466004)
  137. PROCESS: ./commands/script.py
  138. VERSION: 15.05.1+microservices.9559.0ce3c50f
  139. BRANCH: microservices CHANGESET: 0ce3c50f
  140. ERROR FINGERPRINT: 2d8e17a7-7a24-5ea7-9808-8b6f6d78da59
  141. WORKING DIRECTORY: /opt/noc
  142. EXCEPTION: <class 'noc.core.script.error.UnexpectedResultError'> Unexpected result
  143. START OF TRACEBACK
  144. ------------------------------------------------------------------------
  145. File: core/script/base.py (Line: 643)
  146. Function: re_search
  147.   636             Returns match object.
  148.   637             rx can be string or compiled regular expression
  149.   638             """
  150.  639             if isinstance(rx, six.string_types):
  151.  640                 rx = re.compile(rx, flags)
  152.  641             match = rx.search(s)
  153.  642             if match is None:
  154.  643 ==>             raise UnexpectedResultError()
  155.  644             return match
  156.  645
  157.  646         def re_match(self, rx, s, flags=0):
  158.  647             """
  159.   648             Match s against regular expression rx using re.match
  160.   649             Raise UnexpectedResultError if regular expression is not matched.
  161. Variables:
  162.                   rx = <_sre.SRE_Pattern object at 0x26d2650>
  163.                    s =
  164. 'Nateks Networks Co.Ltd Internetwork Operating System Software\nNetXpert NX-3408 V1 Series Software, Version 2.2.0C Build 41423, RELEASE SOFTWARE\nCopyright (c) 2017 by Nateks Networks Co.Ltd\nCompiled: 2017-3-13 12:8:26 by SYS, Image text-base: 0x80008000\nROM: System Bootstrap, Version 0.3.8,hardware version:V1.0\nSerial num:20013217075, ID num:20013217075\nSystem image file is "Switch.bin"\nNateks NetXpert NX-3408 V1 RISC\n131072K bytes of memory,8192K bytes of flash\nBase ethernet MAC Address: 00:0f:d9:0a:85:e8\nsnmp info:\n  vend_ID:4249   product_ID:146   system_ID:1.3.6.1.4.1.4249.21.146\nK1-SW uptime is 13:08:09:12, The current time: 1970-1-14 8:9:12\n'
  165.                flags = 0
  166.                 self =
  167. <noc.sa.profiles.Nateks.NetXpert.get_version.Script object at 0x7fa43a953f50>
  168.                match = None
  169. ------------------------------------------------------------------------
  170. File: sa/profiles/Nateks/NetXpert/get_version.py (Line: 32)
  171. Function: execute
  172.    25             r".*ROM: System Bootstrap, Version (?P<bootrom>[^ ,]+)"
  173.    26             r"Serial num:(?P<sn>[^ ,]+),.*\n"
  174.    27             r"Nateks (?P<platform>.*) RISC",
  175.    28             re.MULTILINE | re.DOTALL | re.IGNORECASE)
  176.    29
  177.    30         def execute(self):
  178.    31             v = self.cli("show version", cached=True)
  179.    32 ==>         match = self.re_search(self.rx_ver, v)
  180.    33
  181.    34             return {
  182.    35                 "vendor": "Nateks",
  183.    36                 "platform": match.group("platform"),
  184.    37                 "version": match.group("version"),
  185.    38                 "attributes": {
  186. Variables:
  187.                 self =
  188. <noc.sa.profiles.Nateks.NetXpert.get_version.Script object at 0x7fa43a953f50>
  189.                    v =
  190. 'Nateks Networks Co.Ltd Internetwork Operating System Software\nNetXpert NX-3408 V1 Series Software, Version 2.2.0C Build 41423, RELEASE SOFTWARE\nCopyright (c) 2017 by Nateks Networks Co.Ltd\nCompiled: 2017-3-13 12:8:26 by SYS, Image text-base: 0x80008000\nROM: System Bootstrap, Version 0.3.8,hardware version:V1.0\nSerial num:20013217075, ID num:20013217075\nSystem image file is "Switch.bin"\nNateks NetXpert NX-3408 V1 RISC\n131072K bytes of memory,8192K bytes of flash\nBase ethernet MAC Address: 00:0f:d9:0a:85:e8\nsnmp info:\n  vend_ID:4249   product_ID:146   system_ID:1.3.6.1.4.1.4249.21.146\nK1-SW uptime is 13:08:09:12, The current time: 1970-1-14 8:9:12\n'
  191. ------------------------------------------------------------------------
  192. File: core/script/base.py (Line: 247)
  193. Function: run
  194.   240                         self.logger.info("Using cached result")
  195.   241                         cache_hit = True
  196.   242                     except KeyError:
  197.   243                         pass
  198.   244                 # Execute script
  199.   245                 if not cache_hit:
  200.   246                     try:
  201.   247 ==>                     result = self.execute(**self.args)
  202.   248                         if self.cache and self.parent and result:
  203.   249                             self.logger.info("Caching result")
  204.   250                             self.set_cache(self.name, self.args, result)
  205.   251                     finally:
  206.   252                         if not self.parent:
  207.   253                             # Close SNMP socket when necessary
  208. Variables:
  209.            cache_hit = False
  210.                 self =
  211. <noc.sa.profiles.Nateks.NetXpert.get_version.Script object at 0x7fa43a953f50>
  212. ------------------------------------------------------------------------
  213. File: commands/script.py (Line: 118)
  214. Function: handle
  215.   111                 capabilities=caps,
  216.   112                 args=args,
  217.   113                 version=version,
  218.   114                 timeout=3600,
  219.   115                 name=script,
  220.   116                 collect_beef=bool(beef)
  221.   117             )
  222.   118 ==>         result = scr.run()
  223.   119             if pretty:
  224.   120                 pprint.pprint(result)
  225.   121             elif yaml:
  226.   122                 import yaml
  227.   123                 import sys
  228.   124                 yaml.dump(result, sys.stdout)
  229. Variables:
  230.                  obj = <ManagedObject: K1-SW>
  231.                 beef = None
  232.              service = <__main__.ServiceStub object at 0x7fa43a953f10>
  233.               script = u'Nateks.NetXpert.get_version'
  234.         script_class = <class 'noc.sa.profiles.Nateks.NetXpert.get_version.Script'>
  235.                 self = <__main__.Command object at 0x7fa44f2f23d0>
  236.                 args = {}
  237.             use_snmp = True
  238.                 caps =
  239. {u'DB | Interfaces': 12,
  240.  u'Network | LLDP': True,
  241.  u'Network | STP': True,
  242.  u'SNMP': True,
  243.  u'SNMP | Bulk': True,
  244.  u'SNMP | IF-MIB': True,
  245.  u'SNMP | IF-MIB | HC': True,
  246.  u'SNMP | v1': True,
  247.  u'SNMP | v2c': True,
  248.  u'SNMP | v3': False}
  249.                 yaml = False
  250.          object_name = ['K1-SW']
  251.              version = None
  252.            arguments = []
  253.               pretty = True
  254.          credentials =
  255. {'access_preference': u'CS',
  256.  'address': u'10.20.52.102',
  257.  'http_protocol': 'http',
  258.  'password': u'admin',
  259.  'path': None,
  260.  'raise_privileges': True,
  261.  'snmp_ro': u'kondaro',
  262.  'snmp_version': 'v2c',
  263.  'super_password': None,
  264.  'user': u'admin'}
  265.    access_preference = None
  266.              options = {}
  267.                  scr =
  268. <noc.sa.profiles.Nateks.NetXpert.get_version.Script object at 0x7fa43a953f50>
  269. ------------------------------------------------------------------------
  270. File: core/management/base.py (Line: 76)
  271. Function: run_from_argv
  272.    69             enable_profiling = cmd_options.pop("enable_profiling", False)
  273.    70             show_metrics = cmd_options.pop("show_metrics", False)
  274.    71             if enable_profiling:
  275.    72                 # Start profiler
  276.    73                 import yappi
  277.    74                 yappi.start()
  278.    75             try:
  279.    76 ==>             return self.handle(*args, **cmd_options) or 0
  280.    77             except CommandError as e:
  281.    78                 self.print(str(e))
  282.    79                 return 1
  283.    80             except KeyboardInterrupt:
  284.    81                 self.print("Ctrl+C")
  285.    82                 return 3
  286. Variables:
  287.               parser =
  288. ArgumentParser(prog='script.py', usage=None, description=None, version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)
  289.             loglevel = 'debug'
  290.     enable_profiling = False
  291.                 args = ()
  292.          cmd_options =
  293. {'access_preference': None,
  294.  'arguments': [],
  295.  'beef': None,
  296.  'object_name': ['K1-SW'],
  297.  'pretty': True,
  298.  'script': ['get_version'],
  299.  'use_snmp': True,
  300.  'yaml': False}
  301.                 argv = ['--debug', '--pretty', 'get_version', 'K1-SW']
  302.                 self = <__main__.Command object at 0x7fa44f2f23d0>
  303.         show_metrics = False
  304.              options =
  305. Namespace(access_preference=None, arguments=[], beef=None, object_name=['K1-SW'], pretty=True, script=['get_version'], use_snmp=True, yaml=False)
  306. ------------------------------------------------------------------------
  307. END OF TRACEBACK
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement