Normally, when you're dealing with IP addresses where you'd also want to validate them, it is best to use the IPy module (more details in the answers to this question)
However, in your case, you're trying to read from a configuration file and create the script. In this case, the addresses are already validated. So using a regex
to get the text matches from the config and writing them in the scripted format should do the trick.
Code:
import re # Import re module
# Define the pattern you'd like to match. You can use pat1 which does a more stringent check on the IP digits and decimals
pat1 = re.compile('^ip\s+route\s+(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\/\d{1,2})\s+(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})\s+description\s+(.*?)$')
However, since no validation is needed here, you could use the below pattern as well which is smaller and would give the same match (you can see how it works on regex101.com)
pat2 = re.compile('^ip\s+route\s+([0-9|.]{7,15}\/\d{1,2})\s+([0-9|.]{7,15})\s+description\s+(.*?)$')
# Once the pattern is set, all you need to do is search for text and get the match
# Since you'd want to do this recursively over multiple lines of the input file and write to the output file, you need to open both files and iterate through the lines
with open (<outputfile>, 'w') as outfile: # open the <path of outputfile+filename> as outfile
with open (<inputfile>, 'r') as infile: # open the <path of inputfile+filename> as outfile
for line in infile: # read each line of the inputfile
match = re.search(pat2,line) # match pattern with the line
# assign the results of matching groups to variables
ip, dr, desc = match.group(1), match.group(2), match.group(3)
# Write to the output file in the required format
# You'll see the use of '\n' for next line and '\t' for tab
outfile.write("static-route-entry {}\n\tnext-hop {}\n\tdescription {}\n\tno shutdown\n\texit\nexit".format(ip, dr, desc))
Output Sample:
I guess you'd need to specify the next-hop, description and the no shutdown in the same indent as the first exit. You can use '\t' to add the tabs if you need, but if it's a script, it won't matter.
[Out]:
static-route-entry 10.8.125.144/28
next-hop 10.0.59.5
description Sunny_House_HLR1_SIG
no shutdown
exit
exit