Skip to main content
Rollback to Revision 4
Source Link
voices
  • 767
  • 1
  • 6
  • 10

#randip.py

I know there are probably better, easier ways to do this. It
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:
Either a 4 (IPv4) or a 6 (IPv6).

 

Usage###Usage:

./randip.py 4./randip.py 4
61.104.170.242
  
./randip.py 6./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()

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

#!/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()

#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()
deleted 74 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

#randip.py

I know there are probably better, easier ways to do this.
It 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, either a 4 (IPv4) or a 6 (IPv6).


 

###UsageUsage:

./randip.py 4./randip.py 4
61.104.170.242
  
./randip.py 6./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()

#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()

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

#!/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()
several minor edits for clarity
Source Link
voices
  • 767
  • 1
  • 6
  • 10

#randip.py

I know there'sthere are probably better, easier ways to do this. It
It was just a bit of a learning exercise for the sake of familiarising myself with Python.

It takes onea single argument (positional parameter):
Either a 4 (IPv4), or a 6 (IPv6).

 

The respective outputs will look something like the following###Usage:
61.104.170.242
b4fc:391d:3ec8:68ef:0ec8:529b:166d:ece2

./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:
        hextetsoctets = []
        for x in range(8):
            hextetoctet = []
            for x in range(4):
                hextetoctet.append(str(choice(hexdigits.lower())))
            hextetsoctets.append(''.join(hextetoctet))
        return ':'.join(hextetsoctets)

    else:
       return 

def main(): 
    print(random_ip(int(argv[1])))

if __name__ == '__main__':
    main()

I know there's 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 one argument (positional parameter):
Either a 4 (IPv4), or a 6 (IPv6).

The respective outputs will look something like the following:
61.104.170.242
b4fc:391d:3ec8:68ef:0ec8:529b:166d:ece2


#!/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:
        hextets = []
        for x in range(8):
            hextet = []
            for x in range(4):
                hextet.append(str(choice(hexdigits.lower())))
            hextets.append(''.join(hextet))
        return ':'.join(hextets)

    else:
       return 

def main(): 
    print(random_ip(int(argv[1])))

if __name__ == '__main__':
    main()

#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()
Tag.
Source Link
Daniel
  • 4.6k
  • 2
  • 18
  • 40
Loading
Tweeted twitter.com/StackCodeReview/status/1022497085428510721
deleted 24 characters in body
Source Link
voices
  • 767
  • 1
  • 6
  • 10
Loading
Source Link
voices
  • 767
  • 1
  • 6
  • 10
Loading