Forum Discussion

JQUINONES82NB's avatar
JQUINONES82NB
Icon for Nimbostratus rankNimbostratus
Jan 11, 2019

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 }

 

8 Replies

  • To do a simple replacement in sed you use sed

    's///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.

  • 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.

     

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

      sed 's/mystuff/' < test.txt
    
  • 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
    
  • File with IP mappings

     

    oldipnewip

     

    1.1.1.12.2.2.1

     

    1.1.1.22.2.2.2

     

    1.1.1.32.2.2.3

     

    1.1.1.42.2.2.4

     

    1.1.1.52.2.2.5

     

    1.1.1.62.2.2.6

     

    1.1.1.72.2.2.7

     

    1.1.1.82.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

     

  • 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.

     

  • 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?

     

  • I was able to modify your script and load a set of ips It was able to generate the commands i wanted but I will be testing writing back directly to the api after reading your link for the plugin. Appreciate the support!!!! You rock!

     

    python pool_member_generator.py tmsh modify ltm pool jqtest3 { member add 10.10.10.10:10000 }

     

    tmsh modify ltm pool pool1 { member add 10.10.10.10:80 }

     

    tmsh modify ltm pool pool1 { member add 10.10.1.2:80 }

     

    tmsh modify ltm pool jqtest3 { member add 10.10.10.10:10000 }

     

    tmsh modify ltm pool pool1 { member add 10.10.10.10:80 }

     

    tmsh modify ltm pool pool1 { member add 10.11.1.2:80 }

     

    tmsh modify ltm pool pool1 { member add 11.11.11.11:80 }

     

    tmsh modify ltm pool pool2 { member add 10.10.1.1:80 }

     

    tmsh modify ltm pool pool2 { member add 10.11.1.1:80 }

     

    tmsh modify ltm pool pool_test { member add 172.16.0.1:80 }

     

    tmsh modify ltm pool pool_test { member add 172.17.1.1:80 }