I did some more work on this and got it to accept a dotted-quad formatted
subnet mask, so you don't need to use CIDR format:

    $host = "10.122.3.177"
    $mask = '255.255.255.128'

    ### Convert netmask to CIDR bits
    $mask_cidr_bits = {
      '255' => '8',  '254' => '7',
      '252' => '6',  '248' => '5',
      '240' => '4',  '224' => '3',
      '192' => '2',  '128' => '1',
      '0'   => '0',
    }
    $mask_octets = split( $mask, '\.' )
    $cidr =
      $mask_cidr_bits[$mask_octets[0]] +
      $mask_cidr_bits[$mask_octets[1]] +
      $mask_cidr_bits[$mask_octets[2]] +
      $mask_cidr_bits[$mask_octets[3]]

    # Wildcard is inverse of netmask
    $wild_card = 32 - $cidr


    ### Convert dotted quad IP to decimal format
    $host_octets = split( $host, '\.' )
    $ip_decimal =
      ($host_octets[0] * 16777216) +
      ($host_octets[1] * 65536) +
      ($host_octets[2] * 256) +
      ($host_octets[3])


    ### Remove host bits to get subnet by shifting wildcard bits off the end
    $subnet_decimal = ($ip_decimal >> $wild_card) << $wild_card


    ### Convert IP decimal format to dotted quad
    $quad1   = $subnet_decimal / 16777216
    $remain1 = $subnet_decimal % 16777216
    $quad2   = $remain1 / 65536
    $remain2 = $remain1 % 65536
    $quad3   = $remain2 / 256
    $quad4   = $remain2 % 256

    $subnet = "${quad1}.${quad2}.${quad3}.${quad4}"


❧ Brian Mathis
@orev


On Mon, May 19, 2014 at 6:49 PM, Brian Mathis
<[email protected]>wrote:

> The "and" function is only meant to return true or false.  It does not
> perform binary arithmetic, which is what you're trying to do.
>
> If you can use the number of bits in the netmask (CIDR notation), instead
> of the dotted-quad notation, you can do something like this:
>
>     $host = "10.122.3.177"
>     $mask_bits = 25
>     $wild_card = 32 - $mask_bits
>
>     ### Convert dotted quad to decimal format
>     $octet = split( $host, '\.' )
>     $ip_decimal =
>       ($octet[0] * 16777216) +
>       ($octet[1] * 65536) +
>       ($octet[2] * 256) +
>       ($octet[3])
>
>     ### Remove host bits
>     $subnet_junk    = $ip_decimal  >> $wild_card
>     $subnet_decimal = $subnet_junk << $wild_card
>
>     ### Convert decimal format to dotted quad
>     $quad1   = $subnet_decimal / 16777216
>     $remain1 = $subnet_decimal % 16777216
>     $quad2   = $remain1 / 65536
>     $remain2 = $remain1 % 65536
>     $quad3   = $remain2 / 256
>     $quad4   = $remain2 % 256
>
>     $subnet = "${quad1}.${quad2}.${quad3}.${quad4}"
>
>
> Another possibility would be to calculate using ruby code and the
> inline_template() function.
>
>
> ❧ Brian Mathis
> @orev
>
>
> On Mon, May 19, 2014 at 3:22 AM, Michael Wörz <[email protected]>wrote:
>
>> no, thanks split works now,
>> but the boolean operator
>>
>> $ok1=$iparray[0] and $nmarray[0]
>>
>> returns true inetad of 10
>>
>>
>> $ip="10.122.3.177"
>> $nm="255.255.255.128"
>> $iparray=split($ip, '[.]')
>> $nmarray=split($ip, '[.]')
>>
>> $ok1=$iparray[0] and $nmarray[0]
>> notify {"$ok1":}
>>
>>
>>
>>
>> Am Montag, 19. Mai 2014 09:08:16 UTC+2 schrieb Joaquin Menchaca:
>>
>>> Did you escape the meta character of dot?
>>>
>>> $octets = split($ip, '[.]')
>>>
>>>
>>>
>>> On Monday, May 19, 2014 12:01:48 AM UTC-7, Michael Wörz wrote:
>>>>
>>>> Hello folks
>>>>
>>>> i need a bit of help calculating the network address from ip an netmask
>>>> avoiding the use of stdlib within a puppet maifest.
>>>>
>>>> #given IP address and netmask
>>>> $ip="10.122.3.177"
>>>> $nm="255.255.255.128"
>>>>
>>>> #splitting into octets -  split() didnt work for some reason
>>>> if $ip =~ /([0-9]+)\.[0-9]+\.[0-9]+\.[0-9]+/ {$ip1 = $1} #  same for
>>>> $ip2 = $2  .......
>>>> if $nm =~ /([0-9]+)\.[0-9]+\.[0-9]+\.[0-9]+/ {$nm1 = $1}
>>>>
>>>> # apply boolean AND
>>>> $oc1=$ip1 && $nm1
>>>> ##### Could not parse for environment production: Could not match && at
>>>> .......
>>>>
>>>>
>>>> notify {"$oc1":}
>>>>
>>>> thanks for your help
>>>>
>>>  --
>> You received this message because you are subscribed to the Google Groups
>> "Puppet Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/puppet-users/bca3ec21-0338-4f2f-b423-eb79d2300359%40googlegroups.com<https://groups.google.com/d/msgid/puppet-users/bca3ec21-0338-4f2f-b423-eb79d2300359%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/CALKwpEwv_7YF4n3fqP%3DTWh0EyUArU%3DF8THc07nmezGg21SqJHQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to