Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. """
  2. Router 1 config
  3. 100.2.101.1
  4. """
  5. import getpass
  6. import telnetlib
  7. import os
  8. import subprocess
  9.  
  10.  
  11. router_info = {
  12. "r1":{
  13. "ip": '100.2.101.1'
  14. }
  15. }
  16. # router_info = {
  17. # "r1":{
  18. # "ip": '100.2.101.1'
  19. # },
  20. # "r2":{
  21. # "ip": '100.2.102.1'
  22. # },
  23. # "r3":{
  24. # "ip": '100.2.103.1'
  25. # },
  26. # "r4":{
  27. # "ip": '100.2.104.1'
  28. # },
  29. # "r5":{
  30. # "ip": '100.2.105.1'
  31. # },
  32. # "r6":{
  33. # "ip": '100.2.106.1'
  34. # }
  35. # }
  36. # subnet_info = {
  37. # 'sub_a': router_info['r1']['ip'][:-1]+'2',
  38. # 'sub_b': router_info['r2']['ip'][:-1]+'2',
  39. # 'sub_a_dat' : router_info['r3']['ip'][:-1]+'2',
  40. # 'sub_b_dat' : router_info['r4']['ip'][:-1]+'2',
  41. # 'sub_a_dat_dat' : router_info['r5']['ip'][:-1]+'2',
  42. # 'sub_b_dat_dat' : router_info['r6']['ip'][:-1]+'2'
  43. # }
  44. Device = "10.30.7.45"
  45. user = "cisco"
  46. password = "cisco"
  47. en_password = "cisco"
  48.  
  49. def connect():
  50. # print(router_info)
  51. #Pre-config
  52.  
  53.  
  54. #Connect to device
  55. print("[-] Connecting to ",Device)
  56. tn = telnetlib.Telnet(Device)
  57.  
  58. #Enter user ID
  59. tn.read_until(b"Username: ")
  60. tn.write(user.encode('ascii') + b"\n")
  61.  
  62. #Router access authentication
  63. if password:
  64. tn.read_until(b"Password: ")
  65. tn.write(password.encode('ascii') + b"\n")
  66.  
  67. #Disable pause btw pages
  68. tn.read_until(b">")
  69. tn.write(b"terminal length 0\n")
  70.  
  71. #Exec "xxxxx" command and get its results
  72. tn.read_until(b">")
  73. tn.write(b"xxxxx\n")
  74. device_config=tn.read_until(b">").decode('utf-8')
  75.  
  76. #Close connection
  77. print("[-] Disconnecting from ",Device)
  78.  
  79.  
  80. #Record to text file
  81. filename = "Device_" + Device + '.txt'
  82. saveoutput = open(filename,"w")
  83. saveoutput.write(device_config+"\n")
  84. saveoutput.close()
  85. print("Output File: " + filename)
  86. # connect()
  87. def exec(cmd):
  88. text = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,universal_newlines='\r\n')
  89. return text.stdout
  90.  
  91. def traceroute():
  92. """ Do traceroute"""
  93. # for subnet in subnet_info:
  94. # print(subnet)
  95. # for subnet in subnet_info:
  96. # print('[-] Now traceroute on '+subnet+' '+subnet_info[subnet])
  97. # os.system('tracert '+subnet_info[subnet]+' >tracert/tracert_from_outside/'+subnet+".txt")
  98. for subnet in subnet_info:
  99. text = exec('ping -n 1 '+subnet_info[subnet])
  100. if '0% loss' in str(text):
  101. print('[-] Ping '+subnet_info[subnet]+'Complete')
  102. print('[-] Traceroute ',subnet,subnet_info[subnet])
  103. trace_result = exec('tracert '+subnet_info[subnet])
  104. saveoutput = open('tracert/tracert_from_outside/'+subnet+'-'+subnet_info[subnet]+'.txt',"w")
  105. saveoutput.write(str(trace_result))
  106. saveoutput.close()
  107. else:
  108. print("[!] Ping "+subnet_info[subnet]+'is failed!')
  109. # traceroute()
  110.  
  111. def router_grep():
  112. """
  113. get router infomation
  114. """
  115. for router in router_info:
  116. traget = router_info[router]['ip']
  117.  
  118. #Login phase
  119. print('[-] Traget is '+traget)
  120. print("[-] Connecting to ",traget)
  121. tn = telnetlib.Telnet(traget)
  122. print("[-] ",traget,"Connecting Complete ")
  123. tn.read_until(b"Username: ")
  124. tn.write(user.encode('ascii') + b"\n")
  125. tn.read_until(b"Password: ")
  126. tn.write(password.encode('ascii') + b"\n")
  127. tn.read_until(b">")
  128. tn.write('ena'.encode('ascii') + b"\n")
  129. tn.read_until(b"Password:")
  130. tn.write(en_password.encode('ascii') + b"\n")
  131. tn.read_until(b"#")
  132. print("[-] ",traget, "Login and Enable Complete ")
  133.  
  134. #get router name
  135. tn.write(b'sh running-config | include host\n')
  136. text = tn.read_until(b'#').decode('utf-8')
  137. text = text.split('\r\n')[1]
  138. print("[-] ",traget, "hostname is "+text)
  139.  
  140. #get router spec
  141.  
  142. print("[-] Router "+router_info[router]['ip']+" Spec.")
  143. tn.write(b'show version\n')
  144. text = tn.read_until(b'#').decode('utf-8')
  145. text = text.split('\r\n')[1:]
  146. tn.write(b"exit \n")
  147. tn.read_all()
  148. router_grep()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement