School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,406 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,598 people online right now. Registration is fast and FREE... Join Now!




calculating subnet masks and IP ranges

 

calculating subnet masks and IP ranges

rogersj7

30 Nov, 2008 - 04:59 PM
Post #1

New D.I.C Head
*

Joined: 24 Oct, 2008
Posts: 3

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.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 10:43PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month