Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. 2.1
  2. import telnetlib
  3.  
  4. router_ip = "192.168.122.169"
  5. username = "admin"
  6. password = "cisco"
  7.  
  8. tn = telnetlib.Telnet(router_ip)
  9. tn.read_until("Username: ")
  10. tn.write(username + "\n")
  11. tn.read_until("Password: ")
  12. tn.write(password + "\n")
  13. tn.write("terminal length 0\n")
  14.  
  15. def getConfigFromRouter(config_type):
  16. tn.write("show " + config_type + "\n")
  17. tn.read_until("version")
  18. return set(("version" + tn.expect([".+#$"])[2]).splitlines())
  19.  
  20. startup_cfg = getConfigFromRouter("startup-config")
  21. running_cfg = getConfigFromRouter("running-config")
  22.  
  23. diff = running_cfg.difference(startup_cfg)
  24. if(len(diff)>0):
  25. print("startup-config modified lines:")
  26. for l in diff:
  27. print l
  28. print("\nWas before:")
  29. for l in startup_cfg.difference(running_cfg):
  30. print l
  31. else:
  32. print("startup-config and running-config are the same.")
  33.  
  34. tn.write("exit\n")
  35. tn.read_all()
  36.  
  37. 2.2**************
  38.  
  39. import telnetlib
  40.  
  41. router_ip = "192.168.122.169" # Cisco IOSv15.6(1)T-1 router IP
  42. username = "admin" # telnet login
  43. password = "cisco" # telnet password
  44.  
  45. cfg_filename = "old_cfg.txt" # old config file
  46.  
  47. tn = telnetlib.Telnet(router_ip) # Connecting to a host (router)
  48. tn.read_until("Username: ") # Reading telnet output until there is Username text
  49. tn.write(username + "\n") # Sending username and new line symbol (as enter key)
  50. tn.read_until("Password: ") # Reading telnet output until there is Password text
  51. tn.write(password + "\n") # Sending pass and new line symbol (as enter key)
  52. tn.write("terminal length 0\n") # (to not to pause terminal output)
  53.  
  54. def getConfigFromRouter(config_type): # function for reading config from the router
  55. tn.write("show " + config_type + "\n") # sending show startup-config or running-config
  56. tn.read_until("version") # skipping text until version is displayed
  57. return set(("version" + tn.expect([".+#$"])[2]).splitlines()[:-1])
  58.  
  59. def compareConfigs(file_config, running):
  60. diff = file_config.difference(running) # comparing 2 configs
  61. if(len(diff)>0): # if theres is a diffrence
  62. print("running-config modified lines:")
  63. for l in diff: # looping and printing diff
  64. print l
  65. print("\nLines in " + cfg_filename + ":")
  66. for l in running.difference(file_config):
  67. print l
  68. else: # if theres no diff printing that theres none
  69. print("config from file (" + cfg_filename + ") and running-config are the same.")
  70.  
  71. running_cfg = getConfigFromRouter("running-config")
  72. file_cfg = set(open(cfg_filename, "r").read().splitlines()) # opening and reading whole file converting it to lines and to then to set
  73. compareConfigs(file_cfg, running_cfg)
  74.  
  75. tn.write("exit\n") #sending exit to log out out of the router
  76. tn.read_all()
  77.  
  78.  
  79. 3**************************************************************************
  80.  
  81.  
  82. version 15.6
  83. service timestamps debug datetime msec
  84. service timestamps log datetime msec
  85. no service password-encryption
  86. !
  87. hostname R1
  88. !
  89. boot-start-marker
  90. boot-end-marker
  91. !
  92. !
  93. !
  94. no aaa new-model
  95. ethernet lmi ce
  96. !
  97. !
  98. !
  99. mmi polling-interval 60
  100. no mmi auto-configure
  101. no mmi pvc
  102. mmi snmp-timeout 180
  103. !
  104. !
  105. !
  106. !
  107. !
  108. no ip icmp rate-limit unreachable
  109. !
  110. !
  111. !
  112. !
  113. !
  114. !
  115. no ip domain lookup
  116. ip domain name cisco.com
  117. ip cef
  118. no ipv6 cef
  119. !
  120. multilink bundle-name authenticated
  121. !
  122. !
  123. !
  124. !
  125. username admin privilege 15 password 0 cisco
  126. !
  127. redundancy
  128. !
  129. no cdp log mismatch duplex
  130. !
  131. ip tcp synwait-time 5
  132. !
  133. !
  134. !
  135. !
  136. !
  137. !
  138. !
  139. !
  140. !
  141. !
  142. !
  143. !
  144. !
  145. interface GigabitEthernet0/0
  146. ip address dhcp
  147. duplex auto
  148. speed auto
  149. media-type rj45
  150. !
  151. interface GigabitEthernet0/1
  152. no ip address
  153. shutdown
  154. duplex auto
  155. speed auto
  156. media-type rj45
  157. !
  158. interface GigabitEthernet0/2
  159. no ip address
  160. shutdown
  161. duplex auto
  162. speed auto
  163. media-type rj45
  164. !
  165. interface GigabitEthernet0/3
  166. no ip address
  167. shutdown
  168. duplex auto
  169. speed auto
  170. media-type rj45
  171. !
  172. ip forward-protocol nd
  173. !
  174. !
  175. no ip http server
  176. no ip http secure-server
  177. ip ssh version 2
  178. !
  179. !
  180. !
  181. !
  182. control-plane
  183. !
  184. banner exec ^C
  185. **************************************************************************
  186. * IOSv is strictly limited to use for evaluation, demonstration and IOS *
  187. * education. IOSv is provided as-is and is not supported by Cisco's *
  188. * Technical Advisory Center. Any use or disclosure, in whole or in part, *
  189. * of the IOSv Software or Documentation to any third party for any *
  190. * purposes is expressly prohibited except as otherwise authorized by *
  191. * Cisco in writing. *
  192. **************************************************************************^C
  193. banner incoming ^C
  194. **************************************************************************
  195. * IOSv is strictly limited to use for evaluation, demonstration and IOS *
  196. * education. IOSv is provided as-is and is not supported by Cisco's *
  197. * Technical Advisory Center. Any use or disclosure, in whole or in part, *
  198. * of the IOSv Software or Documentation to any third party for any *
  199. * purposes is expressly prohibited except as otherwise authorized by *
  200. * Cisco in writing. *
  201. **************************************************************************^C
  202. banner login ^C
  203. **************************************************************************
  204. * IOSv is strictly limited to use for evaluation, demonstration and IOS *
  205. * education. IOSv is provided as-is and is not supported by Cisco's *
  206. * Technical Advisory Center. Any use or disclosure, in whole or in part, *
  207. * of the IOSv Software or Documentation to any third party for any *
  208. * purposes is expressly prohibited except as otherwise authorized by *
  209. * Cisco in writing. *
  210. **************************************************************************^C
  211. !
  212. line con 0
  213. exec-timeout 0 0
  214. privilege level 15
  215. logging synchronous
  216. line aux 0
  217. exec-timeout 0 0
  218. privilege level 15
  219. logging synchronous
  220. line vty 0 3
  221. login
  222. transport input none
  223. line vty 4
  224. login local
  225. transport input all
  226. !
  227. no scheduler allocate
  228. !
  229. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement