cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1475
Views
2
Helpful
2
Replies

Splitting a string in Velocity

glsparks
Level 1
Level 1

I'm attempting to split a user inputted string that is representing a list of comma separated IP addresses in CIDR notation. In order to determine a wildcard mask later in the script.

eg. 192.168.0.0/24,172.16.0.0/16

#set ($networks = ${networks})
#set ($octets =$networks.split('\,'))
#set ($mask = $octets)

#foreach ($net in $mask)
$net
#end

This outputs:

192.168.0.0/24

172.16.0.0/16

I'm using this currently to create a prefix list which works as expected. However i'd like to also create an ACL from the list which would require determining the Wildcard Mask.

So what I would like to achieve is a way of returning just the list of masks e.g.

24

16

Any tips on how can i achieve this?

 

1 Accepted Solution

Accepted Solutions

Something like?

 

#set ($networks = ${networks})
#set ($octets = $networks.split('\,'))
#set ($masks = [])

#foreach ($net in $octets)
  #set ($mask = $net.split('/')[1])
  #set ($masks.add($mask))
#end

#foreach ($mask in $masks)
  $mask
#end

 

The inputted string is 192.168.0.0/24,172.16.0.0/16, the code will print the following to the console:

 

24
16

 

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

View solution in original post

2 Replies 2

Something like?

 

#set ($networks = ${networks})
#set ($octets = $networks.split('\,'))
#set ($masks = [])

#foreach ($net in $octets)
  #set ($mask = $net.split('/')[1])
  #set ($masks.add($mask))
#end

#foreach ($mask in $masks)
  $mask
#end

 

The inputted string is 192.168.0.0/24,172.16.0.0/16, the code will print the following to the console:

 

24
16

 

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

Perfect well almost perfect. I had to add this to get it to work. Else it just through a syntax error. But many thanks really helped me out.

#set ($networks = ${networks})
#set ($octets = $networks.split('\,'))
#set ($masks = [])

#foreach ($net in $octets)
#set ($mask = $net.split('/')[1])
#set ($foo = $masks.add($mask))
#end
#foreach ($mask in $masks) $mask
#end