Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 6 Jun 2002 22:49:49 -0600
From:      "Mike Roest" <bsd-lists@blahz.ab.ca>
To:        "'BSD Freak'" <bsd-freak@mbox.com.au>, "'FreeBSD Questions'" <freebsd-questions@FreeBSD.ORG>
Subject:   RE: Converting decimal dotted subnet mask to CIDR in a script
Message-ID:  <000101c20dde$c4914230$465d4018@zeus>
In-Reply-To: <000001c20ddc$4916c370$465d4018@zeus>

next in thread | previous in thread | raw e-mail | index | archive | help
Here's a quick python script that does it.  You should be able to port
this to any language fairly easily

--code--
#!/usr/local/bin/python

from string import *
subnet = "255.255.255.0"

(octet1,octet2,octet3,octet4) = subnet.split(".")

octet1 = int(octet1)
octet2 = int(octet2)
octet3 = int(octet3)
octet4 = int(octet4)

worker = octet1
count = 0
while worker != 0:
    if worker % 2 == 1:
        count = count + 1
    worker = worker/2

worker = octet2
while worker != 0:
    if worker % 2 == 1:
        count = count +1
    worker = worker/2

worker = octet3
while worker != 0:
    if worker % 2 == 1:
        count = count +1
    worker = worker/2

worker = octet4
while worker != 0:
    if worker % 2 == 1:
        count= count+1
    worker = worker/2

print count

--end code--

--Mike
-----Original Message-----
From: owner-freebsd-questions@FreeBSD.ORG
[mailto:owner-freebsd-questions@FreeBSD.ORG] On Behalf Of Mike Roest
Sent: Thursday, June 06, 2002 10:32 PM
To: 'BSD Freak'; 'FreeBSD Questions'
Subject: RE: Converting decimal dotted subnet mask to CIDR in a script


You can 
1)split each of the octects at the decimals.
2)convert each resulting numbers to binary
3)count the number of ones in the 4 binary numbers.
4)bang you have the result.

This procedure "should" work for all subnet masks
-Mike

-----Original Message-----
From: owner-freebsd-questions@FreeBSD.ORG
[mailto:owner-freebsd-questions@FreeBSD.ORG] On Behalf Of BSD Freak
Sent: Thursday, June 06, 2002 10:05 PM
To: FreeBSD Questions
Subject: Converting decimal dotted subnet mask to CIDR in a script


Hi all,

I have a variable in a script we use

$MASK=255.255.255.0

Is there a utility or an easy way to process $MASK and get 

$MASKCIDR=24

the CIDR value of 255.255.255.0


Many thanks...

---------------------------------------------------------------------
Faxes delivered directly to any email address, new to mBox!
Find out more http://www.mbox.com.au/fax

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?000101c20dde$c4914230$465d4018>