How to get the last IP address on the Subnet using Ruby
>> require 'ipaddr'
>> ip=IPAddr.new("192.168.1.0/24")
=> #<IPAddr: IPv4:192.168.1.0/255.255.255.0>
>> gateway=IPAddr.new(ip.to_range.last.to_i-1,ip.family).to_s
=> "192.168.1.254"
>> ip=IPAddr.new("192.168.1.0/20")
=> #<IPAddr: IPv4:192.168.0.0/255.255.240.0>
>> gateway=IPAddr.new(ip.to_range.last.to_i-1,ip.family).to_s
=> "192.168.15.254"
to get the first ip of the subnet
>> gateway=IPAddr.new(ip.to_range.first.to_i+1,ip.family).to_s
Post a Comment