Printable Version of Topic

Click here to view this topic in its original format

Dream.In.Code _ Ruby _ calculating subnet masks and IP ranges

Posted by: rogersj7 30 Nov, 2008 - 04:59 PM

Hi all,

I've read the forums and have posted before but now I'm stuck on something. I know what subnet mask and binary masks are and can calculate them (123.456.7.0 = 2 to the power of 7 and down the line) but translating it to ruby....ugh.

Here is what I have thus far:

CODE


print "Enter the TCPIP Network Address (e.g. 10.0.0.0): "
address=gets.chomp
address1, address2, address3, address4=address.split(".")

print "Enter the required number of hosts per subnet: "
hosts=gets.chomp
puts
puts "-" * 40
print "Address Information"
puts
puts "-" * 40
puts

print "Decimal Octets: " + address.to_s
puts

#converting from decimal to binary
b1="%b" % address1
b2="%b" % address2
b3="%b" % address3
b4="%b" % address4
print "Binary Octets: " + b1.rjust(8, "0") + "." + b2.rjust(8, "0") + "." + b3.rjust(8, "0") + "." + b4.rjust(8, "0")
puts

#converting from decimal to hex
h1="%x" % address1
h2="%x" % address2
h3="%x" % address3
h4="%x" % address4
print "Hex Octets: " + h1.upcase.rjust(2, "0") + "." + h2.upcase.rjust(2, "0") + "." + h3.upcase.rjust(2, "0") + "." + h4.upcase.rjust(2, "0")
puts

#converting from binary to decimal address
daddress=[b1.rjust(8, "0"), b2.rjust(8, "0"), b3.rjust(8, "0"), b4.rjust(8, "0")].join
print "Decimal Address: " + Integer("0b"+daddress).to_s
puts
puts

puts "-" * 40
print "Netmask Information"
puts
puts "-" * 40
puts

#defining class types
if address1.to_i<=127
    print "This is a Class A network. (16,777,214 addresses)"
else
if address1.to_i<=191
    print "This is a Class B network. (65,534 addresses)"
else
if address1.to_i<=233
    print "This is a Class C network. (256 addresses)"
        end
    end
end

puts
print "Subnet Mask: "
puts


print "Binary Mask: "
puts
print "Number of sub-networks: "
puts
puts
#end

puts "-" * 40
print "List of IP Ranges"
puts
puts "-" * 40
puts
puts



So the conversion to decimal to binary and hex went smooth but I hit a brickwall when it comes to calculating subnet masks, binary masks, and the IP ranges.

I'm getting the hang of Ruby but once in awhile I face a problem and need a little wisdom. Any suggestions and appreciated.

J.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)