1

I am working with router configurations and need to write a Python script to loop through a config file, search a specific string for a specific item, in this case an IP address, store it as a variable, and then use that variable in another string. For example:

interface Vlan88
  ip address 12.37.221.1 255.255.255.0
  !
  !
  standby 1 ip (insert ONLY 12.37.221.1 here)

And do this same thing for any and all other interfaces

I'll be honest....at present tries == 0 because this is beyond my knowledge level. However, I am using regex to replace some helper addresses as well as write the HSRP config portion. But, I am not far enough along to know how to search for something specific and turn it into a variable.

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • You should give exactly what your input and expected output is. – Akaisteph7 Jul 10 '19 at 21:06
  • As mentioned, this is the input: interface Vlan88 ip address 12.37.221.1 255.255.255.0 ! standby 1 ip And the output should be: interface Vlan88 ip address 12.37.221.1 255.255.255.0 ! ! standby 1 ip 12.37.221.1 – jeremy young Jul 10 '19 at 21:16

4 Answers4

0

You should first check if the string exists in your config file, and if it does, concatenate to another string:

if <STRING> in open(<CONFIG FILE>).read():
      ip = <STRING>
     some_other_string += ip
Omri Brandes
  • 111
  • 4
0

welcome to Stack Overflow!

For this problem, you could...

  • Load the config file data into a string variable, perhaps contents
  • Use the .find() string method to search for a specfic keyword, such as ip address
  • Use indexing to grab the next set of data: since .find() returns the index of the target word, you could then go about grabbing the next words unto you hit an End-Of-Line character, likely at least \n

Here is a somewhat related problem that my help if you like This may help as well

0

In general, when matching with re for example using re.search(...) you give a pattern and a target string. In the pattern you specify the structure of string you're looking for, and when using (...) inside your pattern, this creates capture groups that can be later retrieved. For example:

s = "ip address 12.37.221.1 255.255.255.0"
mtch = re.search("ip address (\d+.\d+.\d+.\d+) .*")
print(mtch.group(0))
-> ip address 12.37.221.1 255.255.255.0
print(mtch.group(1))
-> 12.37.221.1

group(0) is always the full match, and then comes all your capturing groups.

For more details and examples, read this section of the cookbook

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
0

If I understand the problem correctly, you want to insert first IP address after "standby 1 ip". You can achieve this with regex capturing groups:

data = '''
interface Vlan88
  ip address 12.37.221.1 255.255.255.0
  !
  !
  standby 1 ip'''

import re

new_data = re.sub(r'(?<=ip address )([\d.]+)(.*)(standby 1 ip)', r'\1\2\3 \1', data, flags=re.DOTALL)

print(new_data)

Prints:

interface Vlan88
  ip address 12.37.221.1 255.255.255.0
  !
  !
  standby 1 ip 12.37.221.1

You can see inner workings of this regular expression on Regex101 (link).

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91