Showing results for 
Search instead for 
Did you mean: 

EEM applet not able to access USB flash drive



I'm hoping someone can point me in the correct direction.  I am trying use EEM to detect with a USB flash drive is plugged into a router and subsequently write a default config to the device.  When I use the syslog detector I always recieve an error stating the "dir" command is sending invalid input.  When I run the script manually I do not recieve any errors.  Is there some difference between the syslog detector and the none detector?

Thanks in advance.


Nov 30 15:20:51.923 CST_: %USBFLASH-5-CHANGE: usbflash0 has been inserted!

Nov 30 15:20:51 CST_: %HA_EM-6-LOG: USB_Reset: Checking for new config.

Nov 30 15:20:51 CST_: %HA_EM-6-LOG: USB_Reset: USB Drive found usbflash0

Nov 30 15:20:51 CST_: %HA_EM-6-LOG: USB_Reset : DEBUG(cli_lib) : : CTL : cli_open called.

Nov 30 15:20:51 CST_: %HA_EM-6-LOG: USB_Reset : DEBUG(cli_lib) : : OUT : admin-mdgraves>

Nov 30 15:20:51 CST_: %HA_EM-6-LOG: USB_Reset : DEBUG(cli_lib) : : IN  : admin-mdgraves>enable

Nov 30 15:20:52 CST_: %HA_EM-6-LOG: USB_Reset : DEBUG(cli_lib) : : OUT : admin-mdgraves#

Nov 30 15:20:52 CST_: %HA_EM-6-LOG: USB_Reset : DEBUG(cli_lib) : : IN  : admin-mdgraves#dir usbflash0:

Nov 30 15:20:52 CST_: %HA_EM-6-LOG: USB_Reset : DEBUG(cli_lib) : : OUT :                    ^

Nov 30 15:20:52 CST_: %HA_EM-6-LOG: USB_Reset : DEBUG(cli_lib) : : OUT : % Invalid input detected at '^' marker.

Nov 30 15:20:52 CST_: %HA_EM-6-LOG: USB_Reset : DEBUG(cli_lib) : : OUT :

Nov 30 15:20:52 CST_: %HA_EM-6-LOG: USB_Reset : DEBUG(cli_lib) : : OUT : admin-mdgraves#

Nov 30 15:20:52 CST_: %HA_EM-6-LOG: USB_Reset : DEBUG(cli_lib) : : CTL : cli_close called.

EEM Applet

no event manager applet USB_Reset

event manager applet USB_Reset authorization bypass

event syslog pattern ".*USBFLASH-5-CHANGE.*usbflash.*inserted!"

action 001 regexp ".*USBFLASH-5-CHANGE.*(usbflash.).*inserted!" $_syslog_msg _match usb_drive

! event none sync yes

! action 001 set usb_drive $_none_arg1

action 010 syslog msg "Checking for new config."

action 030 string length $usb_drive

action 040 if $_string_result eq 0

action 050  syslog msg "Could not determin usb_drive. aborting"

action 051  set _exit_status 1

action 060  exit

action 100 end

action 109 syslog msg "USB Drive found $usb_drive"

action 110 cli command "enable"

action 120 cli command "dir $usb_drive:"

action 140 foreach _i $_cli_result "\n"

action 150   regexp "\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+(.*\.cfg).*" $_i match config_file

action 160   if $_regexp_result eq 1

action 170     string length $config_file

action 180     if $_string_result eq 0

action 190       syslog msg "Did not find any CFG file.  aborting"

action 191       set _exit_status 1

action 192       exit

action 192     end

action 201     syslog msg "CFG file found $config_file"

action 260     syslog msg "restoring default config and reloading."

action 270     cli command "copy $usb_drive:$config_file startup-config" pattern "startup-config"

action 271     cli command ""

action 280     cli command "reload in 100" pattern "confirm"

action 281     cli command "y"

action 290     puts "reset wireless"

action 900   end

action 910 end

3 Replies 3

Jason Pfeifer
Cisco Employee
Cisco Employee

HI Michael,

When you plug in the device are you able to do  "dir usbflash0:" manually?  There should be no difference in this action between the none and syslog events, as long as usb_drive is set to the same value.  You may want to try printing the $usb_drive value first with other characters around it to ensure nothing is hidden.  Something like --$usb_drive--




Thanks for the response.  I did verify that manually doing a sh usbflash0: works as expected.  I also did a debug to determine exactly where the command was failing and was able to cofirm that the command did not like the usbflash0:.  I believe that I am running into a race condition where the syslog message is being sent but the filesystem on the flash isn't ready and available.  I worked around the problem by doing a sh run cli command in the script.  This delays the script long enough so that the dir command succeeds.


Cisco Employee
Cisco Employee

Hi Michael,

You might try changing the 'show run' to a wait. 

Router(config-applet)#action 1.0 wait ?
  <1-31536000>  Seconds value

That is (hopefully) more deterministic and forces the device to wait a specified amount of time.  Looking at the usb device insertion source code the syslog message for USB device insertion is printed before actually setting up the file system.  So the behavior you see actually makes sense in a way for a lightly loaded device but I don't think it is 100% certain that any of these workarounds will work in every situation.

The proper way for EEM trigger policies on USB device insertion is to have a "USB device insertion and removal event detector" built specifically into IOS that - for insertion - runs *AFTER* the USB device is up and after all of the platform specific USB device items have been taken care of.  I don't know if there are any plans to implement such a thing though.  Until such an event detector exists, this sort of policy might be problematic in certain situations.  It may work in your specific situation but may not work in others or on other platforms.


Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community:

Recognize Your Peers