import random


def random_ip(pattern=None):
    """
    Takes a pattern as a string in the format of #.#.#.# where a # is an
    integer, and a can be substituded with an * to produce a random octet.
    pattern = 127.0.0.* would return a random string between 127.0.0.1 and
    127.0.0.254
    """
    if pattern is None:
        pattern = '*.*.*.*'
    num_asterisks = 0
    for c in pattern:
        if c == '*':
            num_asterisks += 1
    rand_list = [random.randint(1, 255) for i in range(0, num_asterisks)]
    for item in rand_list:
        pattern = pattern.replace('*', str(item), 1)
    return pattern