Skip to content

Instantly share code, notes, and snippets.

@Xpktro
Created November 23, 2012 06:02
Show Gist options
  • Save Xpktro/4134199 to your computer and use it in GitHub Desktop.
Save Xpktro/4134199 to your computer and use it in GitHub Desktop.
Simple decimal subnet mask calculator.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Simple decimal subnet mask calculator.
#
# How to use:
# python prefmask.py <number>
# i.e.: python prefmask.py 17
#
#
# (c) 2012 Moisés Cachay Tello
# This software can be distributed under the terms of the BSD License
#
import sys
def prefix_mask(number):
mask_bin = ('1' * number) + ('0' * (32 - number))
segments = [mask_bin[i:i+8] for i in range(0, 32, 8)]
segments_dec = map(lambda n: str(int(n, 2)), segments)
return '.'.join(segments_dec)
def main():
if len(sys.argv) != 2 or \
not (sys.argv[1].isdigit() and 0 < int(sys.argv[1]) < 33):
print 'Usage: prefmask <number in range 1-32>'
return
prefix = int(sys.argv[1])
print 'The decimal subnet mask for prefix /%s is %s' % \
(prefix, prefix_mask(prefix))
if __name__ == '__main__':
main()
# -*- coding: utf-8 -*-
from distutils.core import setup
import py2exe
setup(
name='Simple decimal subnet mask calculator.',
version='0.1b',
description='A simple console-driven decimal subnet mask calculator.',
author=u'Moisés Cachay',
author_email='mcachayt@gmail.com',
license='BSD',
scripts=['prefmask.py'],
console=['prefmask.py'],
options={'py2exe': {'bundle_files': 1}},
zipfile=None
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment