Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. # $language = "python"
  2. # $interface = "1.0"
  3. import sys
  4. import re
  5.  
  6. # Your hosts file should just be a list of IP's, each on its own line
  7. HOSTSFILE = 'path_toyour_hosts_file.txt'
  8. USER = 'username'
  9. PASS = 'password'
  10. ENABLEP = 'enablepassword'
  11. NEW_HELPER_CMD = 'ip helper-address x.x.x.x'
  12.  
  13. def Wait():
  14. # Wait for a prompt
  15. while True:
  16. if not crt.Screen.WaitForCursor(1):
  17. break
  18. def Get_prompt():
  19. row = crt.Screen.CurrentRow
  20. prompt = crt.Screen.Get(row, 0, row, crt.Screen.CurrentColumn - 1).strip()
  21. return prompt
  22. def Get_list_from_file(file_path):
  23. f = open(file_path, 'r')
  24. hostlist = f.readlines()
  25. f.close()
  26. return hostlist
  27. def Log_in(host):
  28. login_command = '/SSH2 /L {0} /PASSWORD {1} {2}'.format(USER, PASS, host)
  29. try:
  30. crt.Session.Connect(login_command)
  31. crt.Session.Synchronous = True
  32. Wait()
  33. return True
  34. except:
  35. # More specific except clauses needed
  36. # For specific error conditions, add corresponding error messages
  37. return False
  38. def Exec_mode():
  39. prompt = Get_prompt()
  40. tries = 0
  41. # Enter priveleged exec mode if not already there
  42. while tries > 4:
  43. if re.search(r'#$', prompt):
  44. break
  45. elif re.search(r'[>]$', prompt):
  46. crt.Screen.Send('enablen')
  47. Wait()
  48. prompt = Get_prompt()
  49. if re.search(r'Password:', prompt):
  50. crt.Screen.Send('{0}n'.format(ENABLEP))
  51. Wait()
  52. elif re.search(r')$', prompt):
  53. crt.Screen.Send('endn')
  54. Wait()
  55. tries += 1
  56. if tries >= 4:
  57. # Add an error code stating that exec_mode failed
  58. return False
  59. else:
  60. return True
  61. def Config_mode():
  62. prompt = Get_prompt()
  63. if re.search(r'#$', prompt):
  64. crt.Screen.Send('config tn')
  65. Wait()
  66. else:
  67. b = Exec_mode()
  68. if b:
  69. crt.Screen.Send('config tn')
  70. Wait()
  71. prompt = Get_prompt()
  72. if re.search(r')$', prompt):
  73. return True
  74. else:
  75. return False
  76. def Pull_and_parse_config():
  77. Wait()
  78. crt.Screen.Send('term len 0n')
  79. Wait()
  80. prompt = Get_prompt()
  81. # Output the runnning config
  82. crt.Screen.Send(r'sh running-confign')
  83. crt.Screen.WaitForString(r'n')
  84. # Capture config output to string var 'config'
  85. # Note: If there is any duplicate of the 'prompt' string in the config, it will stop the
  86. # capture prematurely
  87. config = crt.Screen.ReadString(prompt)
  88. Wait()
  89. # Split the captured config into a list, containing the interface string, old ip helper string, and
  90. # remaining config. Will capture each interface on the device that currently has an ip helper command
  91. # Note: this only captures the first ip helper command on each interface
  92. configlist = re.split(r'(interface [^n]+?)n[^!]*?(ip helper-address .+?)n', config, flags=re.DOTALL)
  93. if len(configlist) > 1:
  94. configlist.pop(0)
  95. return configlist
  96. else:
  97. # add message stating no interfaces matched
  98. return False
  99. def Update_config(configlist):
  100. # For each interface with an ip helper command, remove old command and add new helper command
  101. while len(configlist) > 2:
  102. int_id = configlist.pop(0)
  103. old_helper_cmd = configlist.pop(0)
  104. if re.search(r'interface .+', int_id) and re.search(r'ip helper-address .+', old_helper_cmd):
  105. Enter_config_mode()
  106. # Here is where you actually update the config
  107. # If you don't want to remove old helper command, remove "no {1}n" from the following string
  108. crt.Screen.Send('{0}n no {1}n {2}n'.format(int_id, old_helper_cmd, NEW_HELPER_CMD))
  109. Wait()
  110. return True
  111. elif not re.search(r'interface .+', int_id):
  112. # add error message stating invalid interface id
  113. return False
  114. else:
  115. # add error message stating invalid ip helper command
  116. return False
  117. def Main():
  118. hostfile = Get_list_from_file(HOSTSFLIE)
  119. for host in hostfile:
  120. ok = Log_in(host)
  121. if not ok:
  122. # add error message stating login failed
  123. continue
  124. ok = Exec_mode()
  125. if not ok:
  126. # add error message here
  127. continue
  128. configlist = Pull_and_parse_config()
  129. if not configlist:
  130. # add error message stating config capture failed
  131. ok = Update_config(configlist)
  132. if not ok:
  133. # add error message stating config update failed
  134. else:
  135. # add success message stating config on host successfully updated
  136.  
  137. Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement