From owner-freebsd-questions Thu Jun 6 22:10:29 2002 Delivered-To: freebsd-questions@freebsd.org Received: from ares.blahz.ab.ca (h24-64-95-168.cg.shawcable.net [24.64.95.168]) by hub.freebsd.org (Postfix) with SMTP id EEF6F37B411 for ; Thu, 6 Jun 2002 22:10:17 -0700 (PDT) Received: (qmail 14376 invoked by uid 508); 7 Jun 2002 04:50:25 -0000 Received: from bsd-lists@blahz.ab.ca by ares.blahz.ab.ca by uid 505 with qmail-scanner-1.12 (sweep: 2.10/3.58. . Clear:. Processed in 1.486473 secs); 07 Jun 2002 04:50:25 -0000 Received: from unknown (HELO zeus) (24.64.93.70) by h24-64-95-168.cg.shawcable.net with SMTP; 7 Jun 2002 04:50:23 -0000 From: "Mike Roest" To: "'BSD Freak'" , "'FreeBSD Questions'" Subject: RE: Converting decimal dotted subnet mask to CIDR in a script Date: Thu, 6 Jun 2002 22:49:49 -0600 Message-ID: <000101c20dde$c4914230$465d4018@zeus> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.2627 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 In-Reply-To: <000001c20ddc$4916c370$465d4018@zeus> Importance: Normal Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG 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