Advertisement
Guest User

Untitled

a guest
Apr 28th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. В /opt/noc/sa/profiles/MikroTik/RouterOS/__init__.py
  2.  
  3. Весь код заменить на вот этот
  4.  
  5. # -*- coding: utf-8 -*-
  6. ##----------------------------------------------------------------------
  7. ## Vendor: MikroTik
  8. ## OS: RouterOS
  9. ## Compatible: 3.14 and above
  10. ##----------------------------------------------------------------------
  11. ## Copyright (C) 2007-2011 The NOC Project
  12. ## See LICENSE for details
  13. ##----------------------------------------------------------------------
  14.  
  15. ## Python modules
  16. import re
  17. ## NOC modules
  18. import noc.sa.profiles
  19. from noc.sa.protocols.sae_pb2 import TELNET, SSH
  20.  
  21.  
  22. class Profile(noc.sa.profiles.Profile):
  23. name = "MikroTik.RouterOS"
  24. supported_schemes = [TELNET, SSH]
  25. command_submit = "\r"
  26. pattern_prompt = r"\[(?P<prompt>[^\]@]+@.+?)\] > "
  27. pattern_more = [
  28. ("Please press \"Enter\" to continue!", "\n"),
  29. (r"\[Q quit\|D dump\||up\|down\]", " ")
  30. ]
  31. pattern_syntax_error = r"bad command name"
  32. config_volatile = [r"^#.*?$", r"^\s?"]
  33.  
  34. def setup_script(self, script):
  35. """
  36. Starting from v3.14 we can specify console options
  37. during login process
  38. :param script:
  39. :return:
  40. """
  41. if (script.parent is None and
  42. not script.access_profile.user.endswith("+ct")):
  43. script.access_profile.user += "+ct"
  44. self.add_script_method(script, "cli_detail", self.cli_detail)
  45.  
  46. def cli_detail(self, script, cmd):
  47. """
  48. Parse RouterOS .... print detail output
  49. :param script:
  50. :param cmd:
  51. :return:
  52. """
  53. return self.parse_detail(script.cli(cmd))
  54.  
  55. rx_p_new = re.compile("^\s*(?P<line>\d+)\s+")
  56. rx_key = re.compile("([a-zA-Z\-]+)=")
  57.  
  58. def parse_detail(self, s):
  59. """
  60.  
  61. :param s:
  62. :return:
  63. """
  64. # Normalize
  65. ns = []
  66. flags = []
  67. for l in s.splitlines():
  68. if not l:
  69. continue
  70. if not flags and l.startswith("Flags:"):
  71. # Parse flags from line like
  72. # Flags: X - disabled, I - invalid, D - dynamic
  73. flags = [f.split("-", 1)[0].strip()
  74. for f in l[6:].split(",")]
  75. continue
  76. match = self.rx_p_new.search(l)
  77. if match:
  78. # New item
  79. if ";;;" in l:
  80. ns += [l.partition(";;;")[0].strip()]
  81. else:
  82. ns += [l]
  83. elif ns:
  84. ns[-1] += " %s" % l.strip()
  85. # Parse
  86. f = "".join(flags)
  87. rx = re.compile(
  88. r"^\s*(?P<line>\d+)\s+"
  89. r"(?P<flags>[%s]+(?:\s+[%s]+)*\s+)?"
  90. r"(?P<rest>.+)$" % (f, f))
  91. r = []
  92. for l in ns:
  93. match = rx.match(l)
  94. if match:
  95. n = int(match.group("line"))
  96. f = match.group("flags")
  97. if f is None:
  98. f = ""
  99. else:
  100. f = f.replace(" ", "")
  101. # Parse key-valued pairs
  102. rest = match.group("rest")
  103. kvp = []
  104. while rest:
  105. m = self.rx_key.search(rest)
  106. if not m:
  107. if kvp:
  108. kvp[-1] += [rest]
  109. break
  110. if kvp:
  111. kvp[-1] += [rest[:m.start()]]
  112. kvp += [[m.group(1)]]
  113. rest = rest[m.end():]
  114. # Convert key-value-pairs to dict
  115. d = {}
  116. for k, v in kvp:
  117. v = v.strip()
  118. if v.startswith("\"") and v.endswith("\""):
  119. v = v[1:-1]
  120. d[k] = v
  121. r += [(n, f, d)]
  122. return r
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement