-1

input from file.txt

ip route 10.8.125.144/28 10.0.59.5 description Sunny_House_HLR1_SIG

output needed in file2.txt

static-route-entry 10.8.125.144/28 next-hop 10.0.59.5 description "Sunny_House_HLR1_SIG" no shutdown exit exit

can anyone tell me how to do it?

2 Answers2

0

File Input/Output

You can read in from a file using the following statement:

in_file = open('file.txt', 'r')
data = in_file.read()
in_file.close()

And write:

out_file = open('file2.txt', 'w')
out_file.write(data)
out_file.close()

I'd recommend checking out the official python documentation section on reading/writing with files.

Data Manipulation

As for parsing, understanding, and formatting the data you receive, that's a bit more complicated. It depends on what data you expect to be getting in, how exactly you want to manipulate it, etc.

For the example you gave specifically (and only that example), here's a very straight-forward parse of the data:

# Read data from file.txt

# Split the string based on spaces,
# discarding the entries you don't use 
# by labeling them as underscores
_, _, sre, nh, _, desc = data.split(' ') 

# Insert the extracted values into a
# multi-line formatted string
final_output = f"""\ 
static-route-entry {sre}
    next-hop {nh}
        description {desc}
    exit
exit
"""

# Write final_output to file2.txt

If the data you're expecting in file.txt varies in any way, you'll have to write a more sophisticated algorithm for parsing, but that should give you a start.

Max Goad
  • 1
  • 1
0

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
ParvBanks
  • 1,316
  • 1
  • 9
  • 15