Created
November 21, 2021 13:57
-
-
Save IBle1ddI/c02b346a518889083755f39c3c37d4a7 to your computer and use it in GitHub Desktop.
Convert IP address to Decimal or hexadecimal format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| You can run this in the following format: | |
| For decimal: python3 ip2dh.py D <Ip-address> | |
| For Hexadecimal: python3 ip2dh.py H <Ip-address> | |
| """ | |
| #!/usr/bin/python3 | |
| import sys | |
| if len(sys.argv) < 3: | |
| print('\nYou must give desired format and IPv4 address as input...') | |
| print('e.g.: D 192.168.10.100') | |
| print('Valid formats D=Decimal H=Hexadecimal\n') | |
| sys.exit(1) | |
| Format = sys.argv[1] | |
| def long(ip): | |
| IP = ip.split('.') | |
| IP = list(map(int, IP)) | |
| LongIP = IP[0]*2**24 + IP[1]*2**16 + IP[2]*2**8 + IP[3] | |
| return LongIP | |
| ip = long(sys.argv[2]) | |
| if Format == 'D': | |
| print('\nIP as Decimal format: %s' % (ip)) | |
| if Format == 'H': | |
| print('\nIP as Hexadecimal format: %s' % (hex(ip))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment