0

How can I improve this code:

import random
def rand_mac():
    return "00:00:00:%02x:%02x:%02x" % (
        random.randint(0, 255),
        random.randint(0, 255),
        random.randint(0, 255)
        )
add_acl_mac = rand_mac()
add_acl = 'mac access-list extended name test deny src {}-{} dst any\r\n'.format(add_acl_mac, add_acl_mac)
acl_list = 1
while (acl_list <=1024):
    network.sendline(add_acl)
    acl_list += 1

Current behavior: the loop writes the same MAC 1023 times.

Proposed behavior: the loop will write at each iteration a different MAC, for 1023 times.

I've been reading about generators and yield but I fail to understand how to properly use them in my current situation, if indeed they are to be used.

Thanks!

1
  • 1
    Call your function in the loop Commented Oct 6, 2020 at 13:42

2 Answers 2

1

Move the lines

add_acl_mac = rand_mac()
add_acl = 'mac access-list extended name test deny src {}-{} dst any\r\n'.format(add_acl_mac, add_acl_mac)

inside the for loop.

Currently you are generating add_acl with the output from rand_mac() only once.

Sign up to request clarification or add additional context in comments.

Comments

1
for _ in range(1024):
    add_acl_mac = rand_mac()
    add_acl = 'mac access-list extended name test deny src {}-{} dst any\r\n'.format(add_acl_mac, add_acl_mac)
    network.sendline(add_acl)

put the mac generation code inside loop as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.