This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

How to get a value from an interface and use it to configure the device in a script

I'm looking to script the config of some Cisco devices. I am happy to select the nodes and interfaces and configure them all with the same config.

However I have two lines of config per interface which are dependent on the switch access vlan number.

So does anyone know how I get this as a variable and config the interfaces based on the vlan? Thanks

Config snippet:

interface GigabitEthernet1/0/2

switchport access vlan 10

authentication event server dead action authorize vlan 10

  • Have you looked at the 'Change VLAN Membership on Cisco IOS Ports' config change template? There is also detailed documentation in the admin guide on how to arrive at this template

    You modified script would look something like

    /*

    .CHANGE_TEMPLATE_DESCRIPTION

           Insert your description here

    .CHANGE_TEMPLATE_TAGS

            Cisco, IOS, VLAN Membership

    .PLATFORM_DESCRIPTION

            Cisco IOS

    .PARAMETER_LABEL @ContextNode

            NCM Node

    .PARAMETER_DESCRIPTION @ContextNode

            The node the template will operate on.  All templates require this by default. The target node is selected during the first part of the wizard so it will not be available for selection when defining values of variables.

    .PARAMETER_LABEL @TargetPorts

            Select Port(s)

    .PARAMETER_DESCRIPTION @TargetPorts

            Select the port(s) for which you would like to change VLAN membership.

    .PARAMETER_LABEL @VlanToAssign

            VLAN to assign

    .PARAMETER_DESCRIPTION @VlanToAssign

            Select the VLAN you would like to assign.

    */

    script BLAHBLAHBLAH(  

                                              NCM.Nodes @ContextNode,

                                              NCM.Interfaces[] @TargetPorts,

                                              NCM.VLANs @VlanToAssign          )

    {

      // Enter configuration mode

      CLI

      {

      configure terminal

      }

      // Loop through selected ports

      foreach (@portItem in @TargetPorts)

      {

        CLI

        {

          interface @portItem.InterfaceDescription

        }

        CLI

        {

          switchport access vlan @VlanToAssign.VLANID

          authentication event server dead action authorize vlan @VlanToAssign.VLANID

        }

        CLI

        {

          exit

        }

      }

      // Exit configuration mode

      CLI

      {

        exit

      }

    }

  • Thanks for the quick reply, I need to explain some more, the vlan config is already on the interface so need to take that vlan and use it for the next command.

    So pre change the config is

    interface GigabitEthernet1/0/2

    switchport access vlan 10

    and post change it should be

    interface GigabitEthernet1/0/2

    switchport access vlan 10

    authentication event server dead action authorize vlan 10

    Cheers

  • ‌if you look at the config change templates section of the admin guide, you will find a list of entities and properties you can reference in the script. vlanid is of the properties for an interface object. In the script body above, you can reference this as


    @portItem.InterfaceDescription

    your script body will also expect only 2 parameters - NCM.Nodes and NCM.Interfaces

  • ‌also meant to add, if you go to the ncm tab and click on the inventory view, you can run the interfaces report and make sure it does contain vlanid ad expected.

  • I'm happy with selecting the node and interfaces and I can configure these with the script. The vlan.id through I'm not sure what you mean, it's not an svi but physical and we have a voice vlan too. 

    Thanks.

  • A solution that works, but can take a long time to configure is to create a rule for every VLAN. This is the only solution that I have come up with so far. It was still better than trying to login to a 1000 devices or however many you manage and manually update each one.