randip.py
I know there are probably better, easier ways to do this.
It was just a bit of a learning exercise for the sake of familiarising myself with Python.
It takes a single argument (positional parameter):
Either a 4
(IPv4) or a 6
(IPv6).
Usage:
./randip.py 4
61.104.170.242
./randip.py 6
4bfc:391d:3ec8:68ef:0ec8:529b:166d:2ece
Code:
#!/usr/bin/env python3
from sys import argv
from random import randint, choice
from string import hexdigits
def random_ip(v):
if v == 4:
octets = []
for x in range(4):
octets.append(str(randint(0,255)))
return '.'.join(octets)
elif v == 6:
octets = []
for x in range(8):
octet = []
for x in range(4):
octet.append(str(choice(hexdigits.lower())))
octets.append(''.join(octet))
return ':'.join(octets)
else:
return
def main():
print(random_ip(int(argv[1])))
if __name__ == '__main__':
main()