cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2643
Views
10
Helpful
4
Replies

EEM/TCL script to run IOS-XE package clean command...

derek-shnosh
Level 1
Level 1

Platform: 3650/3850 running IOS-XE 16.3.6

 

Simply put, I need a script to run the command request platform software package clean switch all at next reboot to scan the root of flash: for unused .bin and .pkg files;

  • If there are packages to clean if the switch prompts Do you want to proceed? [y/n] and awaits user input.

  • If there are no packages to clean the switch presents a message SUCCESS: No extra package or provisioning files found on media. Nothing to clean. and then returns to the exec prompt hostname#.

I tried an EEM applet to that watches the switch log for %SYS-5-RESTART*1, then;

  1. Runs the pkg_clean.tcl script.

    1. Reconfigures int gi1/0/48; removes description.*2

    2. Performs the "software package clean" command, with typeahead "y".

  2. Deletes SVI for vlan 2.

  3. Deletes itself (the applet).

  4. Writes the config.

*1 I found event timer cron cron-entry "@reboot" as a trigger to be unreliable.

*2 I only put the Gi1/0/48 command in the tcl script so I could see the %SYS-5-CONFIG_I syslog message in debug since TCL does not print to syslog. This tells me that at least the ios_config line from the tcl script successfully ran.

EEM Applet

event manager applet pkg_clean
 event syslog occurs 1 pattern "%SYS-5-RESTART: System restarted" maxrun 60
 action 001 cli command "enable"
 action 002 cli command "tclsh flash:pkg_clean.tcl"
 action 005 cli command "conf t"
 action 006 cli command "no int vlan 2"
 action 007 cli command "no event man app pkg_clean"
 action 008 cli command "end"
 action 009 cli command "write mem"
 action 010 cli command ""
 action 011 syslog msg "\n     ##Old .bin and .pkg files cleaned from flash:, temporary interface vlan 2 deleted, wrote startup-config."

 

pkg_clean.tcl

ios_config "int gi1/0/48" "no desc"
typeahead "y"
exec "req plat soft pack clean sw all"

Result... The EEM applet hits the 60 second maxrun timer and then dies.

switch#debug event man act cli
switch#event man run pkg_clean
*Jul 31 2018 14:13:38.181 PDT: %HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : CTL : cli_open called.
*Jul 31 2018 14:13:38.183 PDT: %HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : switch>
*Jul 31 2018 14:13:38.183 PDT: %HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : switch>enable
*Jul 31 2018 14:13:38.295 PDT: %HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : switch#
*Jul 31 2018 14:13:38.508 PDT: %HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : switch#tclsh flash:pkg_clean.tcl
*Jul 31 2018 14:13:38.551 PDT: %SYS-5-CONFIG_I: Configured from console by  on vty0 (EEM:pkg_clean)
switch#
*Jul 31 2018 14:14:38.223 PDT: %HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : CTL : cli_close called.
*Jul 31 2018 14:14:38.228 PDT: 
*Jul 31 2018 14:14:38.228 PDT: tty is now going through its death sequence

If I run it manually, it only takes about 15 seconds to prompt with [y/n].

switch#show clock
*14:41:47.006 PDT Tue Jul 31 2018
switch#req plat soft pack clean sw all
Running command on switch 1
Cleaning up unnecessary package files
No path specified, will use booted path flash:packages.conf
Cleaning flash:
  Scanning boot directory for packages ... done.
  Preparing packages list to delete ... 
    cat3k_caa-guestshell.16.03.06.SPA.pkg
      File is in use, will not delete.
    cat3k_caa-rpbase.16.03.06.SPA.pkg
      File is in use, will not delete.
    cat3k_caa-rpcore.16.03.06.SPA.pkg
      File is in use, will not delete.
    cat3k_caa-srdriver.16.03.06.SPA.pkg
      File is in use, will not delete.
    cat3k_caa-wcm.16.03.06.SPA.pkg
      File is in use, will not delete.
    cat3k_caa-webui.16.03.06.SPA.pkg
      File is in use, will not delete.
    packages.conf
      File is in use, will not delete.
  done.
  
The following files will be deleted:
[1]:
/flash/cat3k_caa-guestshell.16.03.03.SPA.pkg

Do you want to proceed? [y/n]n
switch#
switch#show clock
*14:42:01.748 PDT Tue Jul 31 2018
2 Accepted Solutions

Accepted Solutions

Seems like it might be a race condition where the platform cannot perform the clean operation that early in the boot process.  You should look for another syslog message that appears later on during boot that can trigger the applet.

View solution in original post

That thought had crossed my mind.

 

I just added a 30s wait and it worked as expected, thanks!

 

 action 050 wait 30
 action 055 cli command "req plat soft pack clean sw all" pattern "proceed|#"
 action 060 cli command "y"

View solution in original post

4 Replies 4

Joe Clarke
Cisco Employee
Cisco Employee

You don't need Tcl at all for this.  It will work much better (well, it will work) without it.

event manager applet pkg_clean
 event syslog occurs 1 pattern "%SYS-5-RESTART: System restarted" maxrun 60
 action 001 cli command "enable"
 action 002 cli command "req plat soft pack clean sw all" pattern "proceed|#"
action 003 cli command "y" action 005 cli command "conf t" action 006 cli command "no int vlan 2" action 007 cli command "no event man app pkg_clean" action 008 cli command "end" action 009 cli command "write mem" action 011 syslog msg "\n ##Old .bin and .pkg files cleaned from flash:, temporary interface vlan 2 deleted, wrote startup-config."

That's great to hear, because I admittedly threw in the towel with TCL and ended up with this script that still didn't work;

 

 action 045 cli command "req plat soft pack clean sw all" pattern "Do you want to proceed"
 action 050 cli command "y"

 

The applet hits the 60s maxrun and gives up, but if I type the req plat soft pack clean sw all manually, I'm given the Proceed? prompt within approximately 15 seconds.

Does the pattern pipe operate the same as a show command would, "or"?

 

Edit: Here is the script after changing the pattern to "proceed|#", it for some reason complaiend about the command.

 

Script;

event manager applet pkg_clean
 event syslog occurs 1 pattern "%SYS-5-RESTART: System restarted" maxrun 300
 action 000 syslog msg "\n     ##Reboot detected, run 'pkg_clean' EEM applet."
 action 001 cli command "enable"
 !## Remove debug when done testing.
 action 002 cli command "debug event man act cli"
 action 005 cli command "conf t"
 action 010 cli command "no int vl 2"
 action 025 cli command "no event man app pkg_clean"
 action 030 cli command "end"
 action 035 cli command "write mem"
 action 040 cli command "req plat soft pack clean sw all" pattern "proceed|#"
 action 045 cli command "y"
 action 050 syslog msg "\n     ##Config cleanup completed."
 action 055 cli command "undebug all"

 

Result;

%HA_EM-6-LOG: pkg_clean: 
     ##Reboot detected, run 'pkg_clean' EEM applet.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : Debug EEM action cli debugging is on
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : ACCESS-SW#
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : ACCESS-SW#conf t
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : Enter configuration commands, one per line.  End with CNTL/Z.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : ACCESS-SW(config)#
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : ACCESS-SW(config)#no int vl 2
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : ACCESS-SW(config)#
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : ACCESS-SW(config)#no event man app pkg_clean
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : ACCESS-SW(config)#
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : ACCESS-SW(config)#end
%SYS-5-CONFIG_I: Configured from console by  on vty0 (EEM:pkg_clean)
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : ACCESS-SW#
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : ACCESS-SW#write mem
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : Building configuration...
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : [OK]
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : ACCESS-SW#
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : ACCESS-SW#req plat soft pack clean sw all
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : req plat soft pack clean sw all
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT :   ^
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : % Invalid input detected at '^' marker.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT :
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : ACCESS-SW#
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : y
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : % Bad IP address or host name% Unknown command or computer name, or unable to find computer address
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : ACCESS-SW#
%HA_EM-6-LOG: pkg_clean:
     ##Config cleanup completed.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : ACCESS-SW#undebug all

 

I moved the request to first action after `enable`, but got the same result;

%HA_EM-6-LOG: pkg_clean: 
     ##Reboot detected, run 'pkg_clean' EEM applet.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : Debug EEM action cli debugging is on
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : ACCESS-SW#
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : ACCESS-SW#req plat soft pack clean sw all
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : req plat soft pack clean sw all
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT :   ^
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : % Invalid input detected at '^' marker.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT :
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : ACCESS-SW#
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : y
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : % Bad IP address or host name% Unknown command or computer name, or unable to find computer address
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : ACCESS-SW#
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : ACCESS-SW#conf t
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : Enter configuration commands, one per line.  End with CNTL/Z.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : ACCESS-SW(config)#
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : ACCESS-SW(config)#no int vl 2
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : ACCESS-SW(config)#no event man app pkg_clean
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : ACCESS-SW(config)#
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : ACCESS-SW(config)#end
%SYS-5-CONFIG_I: Configured from console by  on vty0 (EEM:pkg_clean)
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : ACCESS-SW#
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : ACCESS-SW#write mem
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : Building configuration...
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : [OK]
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : ACCESS-SW#
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : ACCESS-SW#
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : ACCESS-SW#
%HA_EM-6-LOG: pkg_clean:
     ##Config cleanup completed.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : ACCESS-SW#undebug all

 

But if I change the event trigger to none, it worked fine;

ACCESS-SW#conf t
ACCESS-SW(config)#event man app pkg_clean
ACCESS-SW(config-applet)#event none maxrun 60
ACCESS-SW(config-applet)#end
*Aug  5 2018 21:49:50.701 PDT: %SYS-5-CONFIG_I: Configured from console by admin on console
ACCESS-SW#event man run pkg_clean

%HA_EM-6-LOG: pkg_clean:
     ##Reboot detected, run 'pkg_clean' EEM applet.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : Debug EEM action cli debugging is on
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : ACCESS-SW#
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : ACCESS-SW#req plat soft pack clean sw all
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : Running command on switch 1
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : Cleaning up unnecessary package files
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : No path specified, will use booted path flash:packages.conf
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : Cleaning flash:
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT :   Scanning boot directory for packages ... done.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT :   Preparing packages list to delete ...
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT :     cat3k_caa-guestshell.16.03.06.SPA.pkg
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT :       File is in use, will not delete.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT :     cat3k_caa-rpbase.16.03.06.SPA.pkg
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT :       File is in use, will not delete.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT :     cat3k_caa-rpcore.16.03.06.SPA.pkg
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT :       File is in use, will not delete.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT :     cat3k_caa-srdriver.16.03.06.SPA.pkg
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT :       File is in use, will not delete.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT :     cat3k_caa-wcm.16.03.06.SPA.pkg
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT :       File is in use, will not delete.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT :     cat3k_caa-webui.16.03.06.SPA.pkg
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT :       File is in use, will not delete.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT :     packages.conf
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT :       File is in use, will not delete.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : CTL : 20+ lines read from cli, debug output truncated
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : y
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : y^J
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : [1]:
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : Deleting file flash:cat3k_caa-guestshell.16.03.03.SPA.pkg ... done.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : Deleting file flash:cat3k_caa-rpbase.16.03.03.SPA.pkg ... done.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : Deleting file flash:cat3k_caa-rpcore.16.03.03.SPA.pkg ... done.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : Deleting file flash:cat3k_caa-srdriver.16.03.03.SPA.pkg ... done.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : Deleting file flash:cat3k_caa-universalk9.16.03.06.SPA.conf ... done.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : Deleting file flash:cat3k_caa-wcm.16.03.03.SPA.pkg ... done.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : Deleting file flash:cat3k_caa-webui.16.03.03.SPA.pkg ... done.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : Deleting file flash:packages.conf.00- ... done.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : SUCCESS: Files deleted.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT :
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : ACCESS-SW#
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : ACCESS-SW#conf t
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : Enter configuration commands, one per line.  End with CNTL/Z.
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : ACCESS-SW(config)#
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : ACCESS-SW(config)#no int vl 2
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : ACCESS-SW(config)#no event man app pkg_clean
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : ACCESS-SW(config)#
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : ACCESS-SW(config)#end
%SYS-5-CONFIG_I: Configured from console by  on vty0 (EEM:pkg_clean)
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : OUT : ACCESS-SW#
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : IN  : ACCESS-SW#write mem
%HA_EM-6-LOG: pkg_clean : DEBUG(cli_lib) : : CTL : cli_close called.
tty is now going through its death sequence

 

Seems like it might be a race condition where the platform cannot perform the clean operation that early in the boot process.  You should look for another syslog message that appears later on during boot that can trigger the applet.

That thought had crossed my mind.

 

I just added a 30s wait and it worked as expected, thanks!

 

 action 050 wait 30
 action 055 cli command "req plat soft pack clean sw all" pattern "proceed|#"
 action 060 cli command "y"
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: