CCIE or Null!

My journey to CCIE!

Working with Cisco Macros.

with 4 comments

As I was going some switch configuration guides I stumbled upon something that caught my attention macros. These macros allow you to create a set of commands that are issued to the device anytime you apply the macro.

Now these macros are very open ended, and have a host of potential uses. For example if you needed to change the management address of switch in a remote location simply create a macro that will do it for you. This way even though you will lose connectivity to the switch the macro still runs because the commands are being issued by the switch not by you from your computer, thus the new IP address is applied allowing you to connect back in using the new IP address. You can also use these to configure ports. Simply create a macro that will enable various features like portfast, port-security, rootguard, storm control,  etc then head into interface config or interface range config mode and apply the macro, its an even quicker way to apply the same configuration to multiple ports this can also assist you with enforcing a uniform configuration to these ports leaving out human error (in case you configure ports manually one by one or continually configure small groups of ports using the interface range command).

You can also set variables in your macros, variables are assigned using the $ followed by the variable name. So let’s just say you created a macro that will reset a port that has been disable due to a port security violation you can set the interface as variable within the macro, now when you apply the macro simply specify the value of the variable.

See the below macro:

macro name psecrst
do clear port-security stick int $int
int $int
shut
no shut
end
sh int | include err-dis
 @

Note: Macros are created from configuration mode.

The key points with defining the above macro are the following, the name of the macro in this case the name is psecrst (short for port security reset, but you can name macro’s whatever you want), then you input the commands in the format and order needed to accomplish what you want successfully. Something to keep in mind is the commands will be issued from configuration mode (config) so if some commands needs to be issued in different modes you will need to account for that. You can also see I specified the variable at $int so I am able to specify whatever interface I need to in order for this macro to work correctly. When you are finished putting the commands in your macro you finish the macro by inputting the @ symbol.

To apply a macro we can issue the following command:

macro global apply psecrst $int fa0/2

As you can see after I specify the macro name I then start calling the variables (and you can have more then one variable in a macro) followed by the desired value of the variable. So in this case I specified fa0/2 as the variable so when the macro runs it resets port fa0/2.

Now if you run a macro and you are not getting the results you were expecting you apply the macro using this command

macro global trace psecrst $int fa0/2

Here we specified the keyword trace instead of apply this tells the switch to show you the commands as they are being issued.

I’ve attached the below screen captures:

Creating a macro within the CLI of a catalyst switch

Starting a macro with a variable assigned.

Tracing a macro with a variable assigned.

Written by Stephen J. Occhiogrosso

June 13, 2011 at 8:11 AM

4 Responses

Subscribe to comments with RSS.

  1. very interesting subject , outstanding post.

    Cary Stier

    June 22, 2011 at 7:15 PM

  2. […] you cannot nest commands to issue commands in global config mode. (You will want to look for a macro for something like […]

  3. How would I go about configuration of an auto macro for shut / no shut on a port that gets flagged for a security violation and sends snmp event to log.

    Bear

    October 31, 2022 at 8:12 AM


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: