Initial commit
This commit is contained in:
commit
ce68f7be1f
16
LICENSE
Normal file
16
LICENSE
Normal file
@ -0,0 +1,16 @@
|
||||
Copyright (C) 2015 Jack-Benny Persson <jack-benny@cyberinfo.se>
|
||||
|
||||
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
|
||||
|
13
README.md
Normal file
13
README.md
Normal file
@ -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 <netmask in slash-notation>
|
||||
Example: subcalc.py 24
|
||||
```
|
||||
|
||||
## License ##
|
||||
Subcalc is made by Jack-Benny Persson and is released under GNU GPL version 2.
|
57
subcalc.py
Executable file
57
subcalc.py
Executable file
@ -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 <netmask in slash-notation>"
|
||||
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()
|
Loading…
x
Reference in New Issue
Block a user