Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. """
  2. lets assume,
  3.  
  4. 1.
  5. we have file elements.json in same directory as script below
  6.  
  7. structure of elements.json is :
  8. [{"vendor": "HUAWEI Technology Co.,Ltd", "DisplayName": "AAAAAAAAA", "IP": "1.1.1.1."},]
  9.  
  10. element is NE40
  11.  
  12. 2. we have jumpserver (ubuntu)
  13.  
  14. 3. we have module paramiko & paramiko-expect installer ( pip install paramiko && pip install paramiko-expect)
  15.  
  16. 4. following credentials are filled properly:
  17. """
  18.  
  19. routerusername="solsddsdm"
  20. routeruserpassword="casdadsa"
  21. routerprompt = ".*>$"
  22.  
  23. jumpserverip="5.5.5.5"
  24. jumpserverprompt="\$ $"
  25. jumpserverusername="sasdasdas"
  26. jumpserverpassword="Atasdasd5"
  27.  
  28.  
  29. """
  30.  
  31. then we can call script python inter.py -e AAAAAAAAA -f ".*test.*"
  32.  
  33. and as result we get interfaces, where description will match regexp ".*test.*"
  34.  
  35.  
  36. """
  37.  
  38. import sys, getopt
  39. import json
  40. import os
  41. import re
  42. import traceback
  43. import paramiko
  44. from paramiko_expect import SSHClientInteraction
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. def main(ele="KIR-IPR-9306-02",fil="1/0/0"):
  52. dir_path = os.path.dirname(os.path.realpath(__file__))
  53. with open(dir_path+os.sep+'elements.json') as json_data:
  54. d = json.load(json_data)
  55. for el in d:
  56. name=el['DisplayName']
  57. if re.match(ele,name)!=None:
  58. print("element {el} filter {fe} {na}".format(el=el['IP'],na=el['DisplayName'],fe=fil))
  59. printInterfaces(el['IP'],fil)
  60.  
  61.  
  62. def printInterfaces(ip,fil):
  63. ip = ip
  64.  
  65. try:
  66. # Create a new SSH client object
  67. client = paramiko.SSHClient()
  68.  
  69. # Set SSH key parameters to auto accept unknown hosts
  70. client.load_system_host_keys()
  71. client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  72.  
  73. # Connect to the host
  74. client.connect(hostname=jumpserverip, username=jumpserverusername, password=jumpserverpassword)
  75.  
  76. # Create a client interaction class which will interact with the host
  77. interact = SSHClientInteraction(client, timeout=20, display=False)
  78. interact.expect(jumpserverprompt, timeout=40)
  79. interact.send("telnet {ipp}".format(ipp=ip))
  80. interact.expect("Username:", timeout=30)
  81. interact.send(routerusername)
  82. interact.expect("Password:.*", timeout=40)
  83. interact.send(routeruserpassword)
  84. interact.expect(routerprompt, timeout=40)
  85.  
  86.  
  87. interact.send("screen-length 0 temporary")
  88. interact.expect(routerprompt, timeout=40)
  89. interact.send("dis int desc")
  90. interact.expect(routerprompt, timeout=30)
  91. output = interact.current_output_clean
  92. lines=output.split("\n")
  93. for ll in lines:
  94.  
  95. rowItems=ll.split(" ")
  96. if rowItems[0]=='' :
  97. continue
  98. interfaceid=""
  99. interfacestatusphy=""
  100. interfacestatusadm = ""
  101. tmptt=[]
  102. for tt in rowItems:
  103. if tt !='':
  104. tmptt.append(tt)
  105.  
  106. if(len(tmptt)==3):
  107. tmptt.append("")
  108.  
  109. if(len(tmptt)<4):
  110. continue
  111. desc= tmptt[3].lower()
  112. fil=fil.lower()
  113. #print(tmptt,desc,fil)
  114. if (re.match(fil,desc)!=None):
  115. print(tmptt)
  116.  
  117.  
  118.  
  119. except Exception as es:
  120. traceback.print_exc()
  121. finally:
  122. try:
  123. client.close()
  124. except:
  125. traceback.print_exc()
  126.  
  127.  
  128. if __name__ == '__main__':
  129. myopts, args = getopt.getopt(sys.argv[1:], "e:f:")
  130. element=""
  131. filter=""
  132. for o, a in myopts:
  133. if o == '-e':
  134. element = a
  135. elif o == '-f':
  136. filter = a
  137. else:
  138. print("Usage: %s -i input -o output" % sys.argv[0])
  139.  
  140. #element="KIR-IPR-9306-02"
  141. #filter=".*kir.*"
  142. main(ele=element,fil=filter)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement