Network Change & Configuration Management

Reply
Accepted Solution

Iterate through CSV list in CSS

benzaam
Techie
Posts: 4
2399     0

Hi Everyone !

 

I'm looking for a way to add a list of VLANS to switches with a CSS script, using a list (csv) that contains the VLAN ID + VLAN NAME.

 

The CSV looks like this: 

vlan_id,vlan_name
1,vlan_1
2,vlan_2
3,vlan_3
4,vlan_4
5,vlan_5
6,vlan_6
7,vlan_7
...

 

I don't want any user input during the script execution, so basically, it should just add all the VLANS listed in the CSV file.

 

However, I can't seem to find a way to iterate through all the rows in the CSV file using a counter...

 

The commands to execute are pretty simple though:

 

SET: $vlan_name= getListValue(vlans,vlan_id,$row,vlan_name,NULL)

### where $row should be incremented at each run until the end of the list is reached

Action:
Add VLANs from CSV

 

Action-Commands: { $enable_changes eq "on" }
configure terminal
vlan $vlan_ID
name $vlan_name
exit

 

Any idea?

 

Thanks,

Benjamin

Re: Iterate through CSV list in CSS

[ Edited ]
Authority
Posts: 32
2399     0

 

There is not a simple way to do that in CSS.  The only iterative control structure in CCS is to loop over each line of the device's output in response to an action -- there is no way to loop through a List and take actions for each row.

 

If the number of VLANs is static (and reasonably small), you could simply brute-force it, by having your ACTION: section just include commands for each VLAN -- but if you've gone through the trouble of putting them into a .CSV, I suspect there are probably too many for that to be workable.

 

What you're trying to do can certainly be accomplished using a Perl script instead of CSS.

 

Another option might be to make these changess using a Template instead of a script.  You could setup a template called something like "Rename all VLANs", and have it just contain the relevant commands, like:

 

vlan 1
 name "vlan_1"
vlan 2
 name "vlan_2"
[etc]

 

...and then apply that Template to devices or device groups as needed.

 

 -Marc-

 

Re: Iterate through CSV list in CSS

Adviser
Posts: 390
2399     0

We can do it PERL if that will work for you?

 

Sif

Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh

Re: Iterate through CSV list in CSS

Authority
Posts: 32
2399     0

 

This may or may not help in your situation -- but there is something you can do in CSS, if your goal is to rename existing vlans, rather than creating new ones.  It really depends on whether you need your devices to have the correct VLAN name configured for VLANs that device is not participating in.

 

You could run a command like "show vlan brief", and then iterate through the resulting list of VLANS, comparing the configured name to the NetMRI List for each one and changing it if needed.

 

Something like this:

 

Action:
     Get List of VLANs

Action-Commands:
     show vlan brief

Output-Triggers:
     Process VLAN

###############

Trigger:
     Process VLAN

Trigger-Description:
     For each active VLAN, compare the configured name to the one in NetMRI's List, 
     and change it if needed.

Trigger-Variables: 
     $DeviceVlanID          "/\d{1,3}/" 
     $DeviceVlanName    word 
     $DeviceVlanStatus   word 
	 
Trigger-Template: 
     ^[[$DeviceVlanID]]\s+[[$DeviceVlanName]]\s+[[$DeviceVlanStatus]].*

Trigger-Filter: 
     $DeviceVlanStatus eq "active"

Trigger-Commands:
     SET: $vlan_name= getListValue(vlans,vlan_id,$DeviceVlanID,vlan_name,NULL) 
	 
Trigger-Commands { $vlan_name ne $DeviceVlanName } 
     configure terminal 
     vlan $DeviceVlanID 
     name $vlan_name
     exit

 

Please note that I didn't actually try running that; it was just typed into the editor.  The syntax will almost certainly need some "adjustment" before it'll work...

 

Anyway, I hope it helps.  :-)

 

-Marc-

 

Highlighted

Re: Iterate through CSV list in CSS

benzaam
Techie
Posts: 4
2399     0

Thank you all!

 

But I think after all if use a config template and run it as a job, I should be just fine... 

It's just a matter of editing/maintaining the vlan ID and names in the script, rather than in a list.

 

 

 

Showing results for 
Search instead for 
Do you mean 

Recommended for You