Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # $language = "python"
- # $interface = "1.0"
- import sys
- import re
- # Your hosts file should just be a list of IP's, each on its own line
- HOSTSFILE = 'path_toyour_hosts_file.txt'
- USER = 'username'
- PASS = 'password'
- ENABLEP = 'enablepassword'
- NEW_HELPER_CMD = 'ip helper-address x.x.x.x'
- def Wait():
- # Wait for a prompt
- while True:
- if not crt.Screen.WaitForCursor(1):
- break
- def Get_prompt():
- row = crt.Screen.CurrentRow
- prompt = crt.Screen.Get(row, 0, row, crt.Screen.CurrentColumn - 1).strip()
- return prompt
- def Get_list_from_file(file_path):
- f = open(file_path, 'r')
- hostlist = f.readlines()
- f.close()
- return hostlist
- def Log_in(host):
- login_command = '/SSH2 /L {0} /PASSWORD {1} {2}'.format(USER, PASS, host)
- try:
- crt.Session.Connect(login_command)
- crt.Session.Synchronous = True
- Wait()
- return True
- except:
- # More specific except clauses needed
- # For specific error conditions, add corresponding error messages
- return False
- def Exec_mode():
- prompt = Get_prompt()
- tries = 0
- # Enter priveleged exec mode if not already there
- while tries > 4:
- if re.search(r'#$', prompt):
- break
- elif re.search(r'[>]$', prompt):
- crt.Screen.Send('enablen')
- Wait()
- prompt = Get_prompt()
- if re.search(r'Password:', prompt):
- crt.Screen.Send('{0}n'.format(ENABLEP))
- Wait()
- elif re.search(r')$', prompt):
- crt.Screen.Send('endn')
- Wait()
- tries += 1
- if tries >= 4:
- # Add an error code stating that exec_mode failed
- return False
- else:
- return True
- def Config_mode():
- prompt = Get_prompt()
- if re.search(r'#$', prompt):
- crt.Screen.Send('config tn')
- Wait()
- else:
- b = Exec_mode()
- if b:
- crt.Screen.Send('config tn')
- Wait()
- prompt = Get_prompt()
- if re.search(r')$', prompt):
- return True
- else:
- return False
- def Pull_and_parse_config():
- Wait()
- crt.Screen.Send('term len 0n')
- Wait()
- prompt = Get_prompt()
- # Output the runnning config
- crt.Screen.Send(r'sh running-confign')
- crt.Screen.WaitForString(r'n')
- # Capture config output to string var 'config'
- # Note: If there is any duplicate of the 'prompt' string in the config, it will stop the
- # capture prematurely
- config = crt.Screen.ReadString(prompt)
- Wait()
- # Split the captured config into a list, containing the interface string, old ip helper string, and
- # remaining config. Will capture each interface on the device that currently has an ip helper command
- # Note: this only captures the first ip helper command on each interface
- configlist = re.split(r'(interface [^n]+?)n[^!]*?(ip helper-address .+?)n', config, flags=re.DOTALL)
- if len(configlist) > 1:
- configlist.pop(0)
- return configlist
- else:
- # add message stating no interfaces matched
- return False
- def Update_config(configlist):
- # For each interface with an ip helper command, remove old command and add new helper command
- while len(configlist) > 2:
- int_id = configlist.pop(0)
- old_helper_cmd = configlist.pop(0)
- if re.search(r'interface .+', int_id) and re.search(r'ip helper-address .+', old_helper_cmd):
- Enter_config_mode()
- # Here is where you actually update the config
- # If you don't want to remove old helper command, remove "no {1}n" from the following string
- crt.Screen.Send('{0}n no {1}n {2}n'.format(int_id, old_helper_cmd, NEW_HELPER_CMD))
- Wait()
- return True
- elif not re.search(r'interface .+', int_id):
- # add error message stating invalid interface id
- return False
- else:
- # add error message stating invalid ip helper command
- return False
- def Main():
- hostfile = Get_list_from_file(HOSTSFLIE)
- for host in hostfile:
- ok = Log_in(host)
- if not ok:
- # add error message stating login failed
- continue
- ok = Exec_mode()
- if not ok:
- # add error message here
- continue
- configlist = Pull_and_parse_config()
- if not configlist:
- # add error message stating config capture failed
- ok = Update_config(configlist)
- if not ok:
- # add error message stating config update failed
- else:
- # add success message stating config on host successfully updated
- Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement