선 밖에 선 자유인

10 digit decimal <-> ip Address 파이썬 스크립트 본문

IT/Security

10 digit decimal <-> ip Address 파이썬 스크립트

Hotman 2013. 8. 13. 16:07
# 10digit decimal -> ip address
#convert 10digit decimal formated ipaddress to normal ipaddress
#by init6
#blog.init6.me

import sys

def main():
    #read file and convert each line to ip address. Comment out to ask for input.
    with open('d:\ipaddress.txt', 'r') as infile:
        for line in infile:
            print (convert(int(line)))
    infile.close()

    #ask for input (dec format) Uncomment to ask for input.
    #decIn = input("Enter 10 digit decimal formated ipaddress: ")
    
def convert(decIn):

    if is32(decIn) == True:
        #convert dec to hex
        fullHex = hex(decIn).lstrip("0x")

        #Split hex number into four pairs
        hex1 = fullHex[0:2]
        hex2 = fullHex[2:4]
        hex3 = fullHex[4:6]
        hex4 = fullHex[6:8]
        #Convert each hex to decimal then to a string and return ip address.
        ipAddr = ( str(int(hex1,16)) + '.' + str(int(hex2,16)) + '.' + str(int(hex3,16)) + '.' + str(int(hex4,16)) )
        return ipAddr
    
#Checks to see if input is a 32bit int or less to make sure its a vaild ip address.         
def is32(n):
    try:
        bitstring=bin(n)
    except (TypeError, ValuueError):
        return False

    if len(bin(n)[2:]) <= 32:
        return True
    else:
        print ("Not a vaild 32bit 10 digit decimal")
        return False
    
main()
# ip address -> 10 digit decimal
#convert ip address to a 10digit decimal formated ipaddress.
#by init6
#blog.init6.me

import sys

def main():
    ipAddr = raw_input("Type in IP Address to convert to 10digit decimal: ")
    print ( convert(ipAddr) )

def convert(ipAddr):
    out = ipAddr.split('.')
    octets = [int(out[0]), int(out[1]), int(out[2]), int(out[3])]
    hexNum = '{:02X}{:02X}{:02X}{:02X}'.format(*octets)
    return int(hexNum, 16)

main()
Comments