Posted on May 14, 2021 4:51 am
 |  Asked by Mantas Alonderis
 |  205 views
0
0
Print Friendly, PDF & Email

Hello,

I’m trying to create multiple VLANs for Arista EOS switches with for loop but not sure how to as (config-vlan-number) changing on every vlan configuration. Any advice? Using pexpect for this:

“`

for n in range(2, 11):
print(“Creating VLAN ” + str(n))
ch.sendline(‘conf t’)
ch.expect(‘\(config\)#’)
config_commands = f’vlan {n}’
config_name = f’name PexpectVLAN{n}’
ch.sendline(config_commands)
ch.expect(‘\(config-vlan-{n}\)#’)    #how to read every vlan in the loop
ch.sendline(config_name)
ch.expect(‘\(config-vlan-{n}\)#’)
ch.sendline(‘end’)

“`

Thank you

 

0
Posted by Keerthi Bharathi
Answered on August 2, 2021 5:51 am

Hello Mantas,

The requirement can be achieved in the following two ways :

for n in range(2, 11):
	print('Creating VLAN ' + str(n))
	child.sendline('conf t')
        child.expect('\(config\)#')
	config_commands = f'vlan {n}'
	config_name = f'name PexpectVLAN{n}'
	prompt = f'\(config-vlan-{n}\)#' << create a variable for Prompt with what would be expected
	child.sendline(config_commands)
	child.expect(prompt)
	child.sendline(config_name)
	child.expect(prompt)
	child.sendline('end')

Or

for n in range(2, 11):
	print('Creating VLAN ' + str(n))
	child.sendline('conf t')
	child.expect('\(config\)#')
	config_commands = f'vlan {n}'
	config_name = f'name PexpectVLAN{n}'
	child.sendline(config_commands)
	child.expect('\(config-vlan-' + str(n) + '\)#') << concatenate the vlan as would be expected in prompt
	child.sendline(config_name)
	child.expect('\(config-vlan-' + str(n) + '\)#')
	child.sendline('end') 

Output from the device:

Switch(config)#sh vlan
VLAN Name Status Ports
----- -------------------------------- --------- -------------------------------
1 default active 2 PexpectVLAN2 active 3 PexpectVLAN3 active 4 PexpectVLAN4 active 5 PexpectVLAN5 active 6 PexpectVLAN6 active 7 PexpectVLAN7 active 8 PexpectVLAN8 active 9 PexpectVLAN9 active 10 PexpectVLAN10 active

Hope this helps.

Post your Answer

You must be logged in to post an answer.