Yes, it is possible to shutdown and enable specific ports based on time. The following example may shed some light on this:
When using EEM, you must create two applet timer policies, one to “shutdown” and the other to “no shutdown”. In the following example, the port will be shutdown every day at midnight, and brought back up every day at 8 am.
Keep in mind that this will work only if you are running IOS 12.2(40)SE or higher. Also it’s a good idea to have NTP configured on the switch when implementing time based scripts.
R1#
*Jul 6 12:57:00.360: %SYS-5-CONFIG_I: Configured from console by on vty0 (EEM:shutdown_port)
*Jul 6 12:57:00.365: %HA_EM-6-LOG: shutdown_port: Interface FastEthernet1/0/1 has been shutdown
This command will display the name and value of the EEM environment variables. You can also take a look at this reference for a list of EEM built in environment variables:
Before writing up an EEM script is always a good idea to go over the manual implementation of what you want to do. In order to copy the running-config to another file name and reload the device, you must do the following:
R2#copy running-config flash0:
Destination filename [running-config]? test.cfg
2949 bytes copied in 2.445 secs (1206 bytes/sec)
R2#reload
Proceed with reload? [confirm]
In my example above, I copied the running configuration to a file called test.cfg. So in this case, you would have to issue the first command, then wait for the proper prompt to appear and then put in the correct command. (There is no confirmation there).
In the case of the reload, you will have to wait for the confirm prompt to appear and then press Enter.
After action 10, it confirms “running-config” appears in the prompt before moving to the next command. Similarly, in action 20, “copied” must appear. In 030 “reload” must appear. Finally, after all patterns are matched, the “” indicates that the Enter key should be applied.
It is the pattern keyword that does the matching. Note that I have not tested the above, and you should confirm that it functions before proceeding.
It is possible to retrieve information such as the hostname of the device. There are many predefined functions that do these types of functions. Specifically, for what you’re looking for you need to use the following command:
action 01 info type routername
This command will store the hostname of the device in a predefined variable called $_info_routername. You can then call this variable in subsequent actions like so:
This command will concatenate the saved hostname with the -base.cfg suffix.
Many more such built-in variables can be found at this Cisco community post:
Unfortunately, I found that Cisco documentation is either inadequate or non-existent (or at least very hard to find!) when it comes to details about EEM features.
Thanks for that Laz, I had a quick glance at that link before but maybe I should have tried the find feature! I will try to post my draft EEM applet once its closer to the task of me semi-automating my lab! Cheers, Rob
Great, thanks for sharing that… It’s posts like this that increase the value and usefulness of the forum, as users share their personal experience on what works best for them. Your contributions are much appreciated!
It looks like the problem comes from the fact that there are spaces in the date portion of the file name. It is strange that the ‘^’ marker is pointing after the first space, but I would have to say that this is most likely the problem. I would suggest taking a look at this Cisco community thread that deals with a very similar configuration issue the EEM:
There you will see various options and configurations that may be helpful. You must however keep in mind that some features, such as those that can manipulate text in order to remove spaces, are supported in EEM 3.0 or higher, so you will have to check your version.
Take a look at the thread and let us know how you get along. If you need more help in the process, you know where to find us!
Got another question about EEM.
I have to add a VLAN in TRUNK of multiple Switches. The only thing that helps me to discover which is the “UPLINK” where i have to put in the new VLAN in the Trunk is the description of those link.
Example:
IF G1/0/1
description DA730 - xxxxxxx
Is there a script that allows me to use the “DA” value in the description as trigger for a switchport trunk allowed vlan add command ?
Hmm, that can become involved. In order to achieve this, you need to determine which ports have the “DA” in their description. There is a command that matches a particular string that can be used.
The above commands will output the results of the show interface command into the $_cli_result variable. Within that output, you will have the description.
The next command searches for a string match that contains the text “description DA” with any other text appended before and after. If it finds it, the variable $_string_result will be set to 1. Otherwise, it will be set to 0. The final command simply prints that value to the CLI. In your case, you can use that result in an if statement, to either apply the config you want to that interface or not to apply it.
More info about the string match command can be found here:
I’m trying to write a EEM applet which will do the following;
1- syslog msg Line protocol on Interface FastEthernet0/1, changed state to down
2- syslog msg Line protocol on Interface FastEthernet0/2, changed state to down
3- reload router
i want my router reload automatic when all lines protocol in router change to down
In order to achieve what you’re looking for you must find a way to determine when all line protocols have changed state to down. In order to do this, you must actively keep track of the states of the line protocols. This is not trivial but can be complex if you want to achieve it with an EEM applet. One approach would be to periodically issue the show interface description command which lists all of the interfaces, their descriptions as well as their Status and Protocol indicators like so:
Then you’ll have to save that output into an archive so it can then be parsed. Saving into an archive can be done like so:
archive
log config
logging enable
Once that’s done, you can then parse that archive and search for the correct number of “down” states. If the number matches the requirement, you can then issue the reload command. Now in order for this to work, you must periodically issue this command, say every several seconds, and when the condition is matched, you can issue the command you want.
Now this is one approach, there may be others that are more suitable, however, before we go into any other options, can you share with us the need for this particular operation? What are you trying to achieve with this? Maybe we can help find an alternative solution to the problem you are facing.