From ce68f7be1f1b22d3d2eb88affc5ee72d9e36b630 Mon Sep 17 00:00:00 2001 From: Jack-Benny Persson Date: Fri, 6 Nov 2015 19:30:58 +0100 Subject: [PATCH] Initial commit --- LICENSE | 16 +++++++++++++++ README.md | 13 +++++++++++++ subcalc.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100755 subcalc.py diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5d92852 --- /dev/null +++ b/LICENSE @@ -0,0 +1,16 @@ +Copyright (C) 2015 Jack-Benny Persson + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + diff --git a/README.md b/README.md new file mode 100644 index 0000000..21b4dba --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# Subcalc # +Subcalc is a small Python program I wrote as a free assigment during a Python +class at EC Utbildning. The program takes a netmask in slash notation as +argument and gives back the netmask in dotted decimal notation. + +## Usage ## +``` +Usage: subcalc.py +Example: subcalc.py 24 +``` + +## License ## +Subcalc is made by Jack-Benny Persson and is released under GNU GPL version 2. diff --git a/subcalc.py b/subcalc.py new file mode 100755 index 0000000..3fa3074 --- /dev/null +++ b/subcalc.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python +# -*- coding:utf-8 -*- +import sys + +def subcalc(slash): + """ Subcalc is a simple function to convert a slash-notation netmask + into a dotted decimal notation netmask. + + Usage: subcalc(netmask) + Example: subcalc (24) + will return 255.255.255.0 + + Note: As input to subcalc, enter only the number, not the leading slash. + If an invalid slash-notation netmask is sent to subcalc. + it will return -1 + """ + # Check if the value is an int, if it's not then quit and return -1 + if isinstance(slash, int) == False: + return -1 + sys.exit() + # Check the size of the netmask, if bogus, then quit end return -1 + if slash < 0 or slash > 32: + return -1 + sys.exit() + + zeroes = "0" * (32-slash) # Fill up "the rest" with zeroes + binNum = "1" * slash # Fill up the binary string with ones + binNum = binNum + zeroes # Put together the ones and the zeroes (tot = 32) + sub = "" # Initalize sub-variable + + # Divide the binary string into four groups + group = [] + group.append(binNum[0:8]) + group.append(binNum[8:16]) + group.append(binNum[16:24]) + group.append(binNum[24:]) + + # For each group, convert to decimal type and end with a dot and append + # the result to the sub-variable + for i in group: + sub += (str(int(i, 2))+".") + return sub.rstrip(".") # Strip of the last dot + +def main(): + # A simple implementation of the subcalc-function above + # Takes one command-line argument (the netmask) + if len(sys.argv) < 2: + print "Usage: subcalc.py " + print "Example: subcalc.py 24" + sys.exit() + arg = int(sys.argv[1]) # Have to typecast here since arguments are strings + print subcalc(arg) + + #help(subcalc) + +if __name__=='__main__': + main()