#!/bin/env python
#################################################################
# Who: James Conner
# When: Feb 24, 2009
# What: pysub
# Version: 0.0.1
# Why: Test subnetting for Cisco exams
#################################################################
# Updates:
# 0.0.1:James Conner:Feb 24, 2009
#     Initial creation
#################################################################
# Notes:
# Need error and exception controls
#################################################################

#################################################################
# Modules
#################################################################
import optparse
import xml.sax
import paramiko
import shutil
import sys, os
import datetime
import string
import random


#################################################################
# Global Variables
#################################################################
stamp = datetime.datetime.now()


#################################################################
# Classes
#################################################################


#################################################################
# Functions
#################################################################
def int2bin(n, count=8):
    return "".join([str((n >> y) & 1) for y in range(count-1, -1, -1)])

def gen_IP():
    oct1=random.randrange(0,223)
    oct2=random.randrange(0,255)
    oct3=random.randrange(0,255)
    oct4=random.randrange(0,255)
    while oct1 == 127:
        oct1=random.randrange(0,223)

    ipaddr=str(oct1)+"."+str(oct2)+"."+str(oct3)+"."+str(oct4)
    ipaddr_list=[oct1,oct2,oct3,oct4]
    ipaddr_list_binary=[int2bin(ipaddr_list[0]),int2bin(ipaddr_list[1]),int2bin(ipaddr_list[2]),int2bin(ipaddr_list[3])]
    ipaddr_binary=ipaddr_list_binary[0]+"."+ipaddr_list_binary[1]+"."+ipaddr_list_binary[2]+"."+ipaddr_list_binary[3]

    if oct1 <= 126:
        netclass="A"
    elif oct1 <= 191:
        netclass="B"
    elif oct1 <= 223:
        netclass="C"
    else:
        print("oh, damn")
    
    randmask=str(256-(2**random.randrange(0,8)))
    
    if netclass == "C":
        while randmask=="255":
            randmask=str(256-(2**random.randrange(0,8)))
        netmask_temp=[255,255,255,randmask]
    elif netclass == "B":
        a=random.randrange(0,1)
        if a == 0:
            while randmask=="255":
                randmask=str(256-(2**random.randrange(0,8)))
            netmask_temp=[255,255,255,randmask]
        elif a == 1:
            netmask_temp=[255,255,randmask,0]
        else:
            print("Need a strongbow over here, STAT!")
    elif netclass == "A":
        a=random.randrange(0,2)
        if a == 0:
            while randmask=="255":
                randmask=str(256-(2**random.randrange(0,8)))
            netmask_temp=[255,255,255,randmask]
        elif a == 1:
            netmask_temp=[255,255,randmask,0]
        elif a == 2:
            netmask_temp=[255,randmask,0,0]
        else:
            print("No cookie!")
    else:
        print("Shazbot?")

    netmask_list=[netmask_temp[0],netmask_temp[1],netmask_temp[2],netmask_temp[3]]
    netmask=str(netmask_list[0])+"."+str(netmask_list[1])+"."+str(netmask_list[2])+"."+str(netmask_list[3])
    netmask_list_binary=[int2bin(int(netmask_list[0])),int2bin(int(netmask_list[1])),int2bin(int(netmask_list[2])),int2bin(int(netmask_list[3]))]
    netmask_binary=str(netmask_list_binary[0])+"."+str(netmask_list_binary[1])+"."+str(netmask_list_binary[2])+"."+str(netmask_list_binary[3])
    increment=256-int(randmask)
    
    netip_list=[int(ipaddr_list[0])&int(netmask_list[0]),int(ipaddr_list[1])&int(netmask_list[1]),int(ipaddr_list[2])&int(netmask_list[2]),int(ipaddr_list[3])&int(netmask_list[3])]
    broadcastip_mask_list=[((int(ipaddr_list[0])|int(netmask_list[0]))-255)*-1,((int(ipaddr_list[1])|int(netmask_list[1]))-255)*-1,((int(ipaddr_list[2])|int(netmask_list[2]))-255)*-1,((int(ipaddr_list[3])|int(netmask_list[3]))-255)*-1]
    broadcastip_list=[int(ipaddr_list[0])+int(broadcastip_mask_list[0]),int(ipaddr_list[1])+int(broadcastip_mask_list[1]),int(ipaddr_list[2])+int(broadcastip_mask_list[2]),int(ipaddr_list[3])+int(broadcastip_mask_list[3])]
    
    
    broadcastip=str(broadcastip_list[0])+"."+str(broadcastip_list[1])+"."+str(broadcastip_list[2])+"."+str(broadcastip_list[3])
    netip=str(netip_list[0])+"."+str(netip_list[1])+"."+str(netip_list[2])+"."+str(netip_list[3])
    
    
    if netclass == "A":
        subnets=(2**(str(netmask_binary).count("1")-8))
    elif netclass == "B":
        subnets=(2**(str(netmask_binary).count("1")-16))
    elif netclass == "C":
        subnets=(2**(str(netmask_binary).count("1")-24))
    else:
        print("Yaba daba doo!")
    
    hosts=(2**(str(netmask_binary).count("0"))-2)
    
    print("IP: "+str(ipaddr)+"/"+str(str(netmask_binary).count("1"))+"    "+str(ipaddr_binary))
    print("Mask: "+str(netmask)+"    "+str(netmask_binary))
    print("Network IP: "+netip+"    Broadcast IP: "+broadcastip)
    print("Class: "+str(netclass)+"    Increment: "+str(increment)+"    Subnets:"+str(subnets)+"    Hosts: "+str(hosts))
    


#################################################################
# Option Parser
#################################################################
parser = optparse.OptionParser(version = "0.0.1")
parser.set_defaults(mode="advanced")
(opts, arg) = parser.parse_args()


#################################################################
# Program Execution
#################################################################
if __name__ == '__main__':
        gen_IP()
James Conner