How to get the last IP address on the Subnet using Ruby

on Tuesday, June 9, 2015
 >> 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"  

0 comments:

Post a Comment