Learn F5 Technologies, Get Answers & Share Community Solutions Join DevCentral

Filter by:
  • Solution
  • Technology
Answers

LTM script modification

Was wondering if a SED Guru could help me but looking to transform a certain lines of configs in my f5 configuration using bash. i will have another file with an ip address mapping to do modification of the configuration.

Lets say i have an ip mapping of

1.1.1.1 that will need to change to 4.4.4.4

cat test.txt

ltm pool test_pool { members { 1.1.1.1:8004 { address 1.1.1.1 session user-disabled state user-down } 2.2.2.2:8358 { address 2.2.2.2 session monitor-enabled state up } 3.3.3.3:8218 { address 3.3.3.3 session user-disabled state down } } monitor nb-http-iis_mon }

I would need to transfer the line to something like this

modify ltm pool ptest_pool add { members { 4.4.4.4:8004 { address 4.4.4.4 session user-disabled state user-down } } monitor nb-http-iis_mon }

=================================== cat test.txt | sed 's/ +/ /g;s/ltm pool/\nmodify ltm pool/g; s/pool { members {/pool add { members {/g; s/1.1.1.1/4.4.4.4/g'

result modify ltm pool test_pool add { members { 4.4.4.4:8004 { address 4.4.4.4 session user-disabled state user-down } 2.2.2.2:8358 { address 2.2.2.2 session monitor-enabled state up } 3.3.3.3:8218 { address 3.3.3.3 session user-disabled state down } } monitor nb-http-iis_mon }

Does anyone know the sed syntax to make it stop after first pattern match of state user-down|up }

0
Rate this Question
Comments on this Question
Comment made 5 days ago by Dave McCauley 183

No need to cat the file, you can just use the redirector, i.e.

  sed 's/mystuff/' < test.txt
0

Answers to this Question

placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

To do a simple replacement in sed you use sed 's/<from>/<to>/g'. The /g means replace more than one example.

eg cat test.txt|sed 's/1.1.1.1/4.4.4.4/g'

I doubt that sed is powerful enough to do something as complex as this though - I would probably write it in Python as it's simple to learn and you can use regex etc to do complex matches and check the actual state, then print it out in the format that you want.

0
Comments on this Answer
Comment made 5 days ago by Dave McCauley 183

Guaranteed that sed/awk/unix commands for text processing are powerful enough to do what he needs ;)

Not that python isn't a great tool, but I wouldn't learn it for some simple text processing.

0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

cat test.txt | sed 's/1.1.1.1/4.4.4.4/g' | sed 's/}.*/}}}/' | sed 's/members/members add/g' | sed 's/ltm pool/tmsh modify ltm pool/g'

tmsh modify ltm pool test_pool { members add { 4.4.4.4:8004 { address 4.4.4.4 session user-disabled state user-down }}}

This will add the host into the pool

I will have to check out how I will do it with two ips on the same line now.

0
Comments on this Answer
Comment made 5 days ago by Dave McCauley 183

Instead of stringing commands together you can just put a semicolon, so this could be:

 sed 's/1.1.1.1/4.4.4.4/g;s/}.*/}}}/;s/members/members add/g;....the rest; of them' < text.txt
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

Do you really need sed to build your new command???

# Create the node with a name instead of IP... 
tmsh create ltm node mytest address 4.4.4.4
# search all pools with IP 1.1.1.1 member and create the modify command line 
for OUTPUT in $(tmsh list ltm pool one-line | grep "ltm pool" | grep 1.1.1.1  | awk '{print $3}') 
> do
> echo modify ltm pool $OUTPUT members add { mytest:8004} members delete { 1.1.1.1:8004}
> done
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

File with IP mappings

oldip newip

1.1.1.1 2.2.2.1

1.1.1.2 2.2.2.2

1.1.1.3 2.2.2.3

1.1.1.4 2.2.2.4

1.1.1.5 2.2.2.5

1.1.1.6 2.2.2.6

1.1.1.7 2.2.2.7

1.1.1.8 2.2.2.8

I basically wanted to send the ltm pools a file and then loop through find the ip on the right and then switching it

ltm pool test_pool1 { members { 1.1.1.1:8004 { address 1.1.1.1 session user-disabled state user-down } 1.1.1.2:8004 { address 1.1.1.2 session monitor-enabled state up }

ltm pool test_pool2 { members { 1.1.1.3:8005 { address 1.1.1.3 session user-disabled state user-down } 1.1.1.4:8005 { address 1.1.1.4 session monitor-enabled state up }

ltm pool test_pool3 { members { 1.1.1.5:8006 { address 1.1.1.5 session user-disabled state user-down } 1.1.1.6:8006 { address 1.1.1.4 session monitor-enabled state up }

ltm pool test_pool4 { members { 1.1.1.7:8007 { address 1.1.1.6 session user-disabled state user-down } 1.1.1.8:8007 { address 1.1.1.4 session monitor-enabled state up }

essentially they are doing a datacenter shift of ips on the pool members and I was trying to use sed to construct the command line

I have already been able to sed the virtual ips and snatpools

0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

cat f5_pool_orig.txt | sed 's/ltm/tmsh modify ltm/g;s/{\saddress\s[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}//g;s/session[^}]//g;s/monitor[^}]//g;s/members/members add/g;s/} 1/1/g;s/} } }/} }/g'

This assumes we have the pools already there and are changing adding the ips. Will finish testing it but at first glance looks ok.

This will parse the running config to commands. I can move to a file. Then I can use an external mappings file to do sed on it and I can then execute the commands to add the new pool members.

0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

I can tell you that f this were me i'd be doing it via iControl REST. Easy to do it - get a list of pools and loop through them. Is it that you want to remove the old IP and change it for the new IP?

0
Comments on this Answer
Comment made 58 minutes ago by Pete White

I have written a short script to do this with iControl REST and my Python module - iCR. It loops through the pools and checks the members, if it matches the name then it adds the new member and deletes the old one. Let me know if you want some help.

#!/usr/bin/env python
from iCR import iCR
s = iCR("172.24.9.132","admin","admin")
fromIP = "3.3.3.3:80"
toIP = "4.4.4.4:80"
pools = s.get("ltm/pool?expandSubcollections=true")['items']
for pool in pools:
    for member in pool['membersReference']['items']:
        if member['name'] == fromIP:
            print str(pool['name'])
            newIPdata = { "name": toIP }
            add = s.create("ltm/pool/" + pool['name'] + "/members",newIPdata)
            if add:
                print("Added " + toIP + " to pool " + pool['name'])
            delete = s.delete("ltm/pool/" + pool['name'] + "/members/" + fromIP)
            if delete:
                print("Deleted " + fromIP + " from pool " + pool['name'])
0
Comment made 12 minutes ago by JQUINONES82NB 115

Looks simple. I can make a yaml file with and old ip and new ip and read from there and iterate through.

didn't not this module existed. Will take a look.

Essentially I have search through a table of ips

old_ip new_ip (500 per device) basically add in the new IP to the pool.

Is there anyway to grab ip:port of the member

lets say my dictionary or mapping is

key value 1111:2222

I loop through with the key to find the ip:port grab that and then switch to the new_ip but reference the port that I found originally?

for ex

pool_test1 member: 1111:80

add in member: 2222:80

I don't want to reference the lookup with ip:port

I will work on your script and post how far I get.

Essentially the port should stay the same just the ip needs to change.

0