Cisco Support Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Announcements

Welcome to Cisco Support Community. We would love to have your feedback.

For an introduction to the new site, click here. And see here for current known issues.

TcL Script to shut down port on a time schedule

Is there a way to have a TcL script shut and no shut ports on a schedule? for example; shut down the ports at 7:00am and no shut the port at 3:30pm?

Thanks,

Mike

1 ACCEPTED SOLUTION

Accepted Solutions
Cisco Employee

You'll need this:

You'll need this:

puts [open "flash:shut_ports.tcl" w] {set ports [lindex $argv 0]
ios_config "int $ports" "shut" "end"
}
20 REPLIES

Here is what I was going to

Here is what I was going to try that I found.

event manager applet shutdown-IntGi1-1

 event timer cron cron-entry "0 7 * * *"

 action 1.0 cli command "enable"

 action 2.0 cli command "config t"

 action 3.0 cli command "interface gi1/1"

 action 4.0 cli command "shut"

 action 5.0 cli command "end"

event manager applet no-shut-IntGi1-1

 event timer cron cron-entry "0 1530 * * *"

 action 1.0 cli command "enable"

 action 2.0 cli command "config t"

 action 3.0 cli command "interface gi1/1"

 action 4.0 cli command "no shut"

 action 5.0 cli command "end"

Would this work?

Mike

Cisco Employee

You asked for Tcl, but these

You asked for Tcl, but these are applet policies.  Your second applet will not work, either.  The cron-entry should be:

event timer cron cron-entry "30 15 * * *"

Also, have a look at https://supportforums.cisco.com/discussion/13076291/automated-shutdown which was a recent thread on a similar topic.

Yes, that is where I found

Yes, that is where I found this.

I got mixed up on TcL and EEM, sorry very new to both of these.

I have a 2960-x that I need to shut some ports down between 7:00am and 3:30pm can this be done on this switch with either TcL or EEM?

Mike

Cisco Employee

The 2960-X does not support

The 2960-X does not support full EEM.  Have a look at the "kron" commands.  This will allow you to schedule these same commands without Tcl.

Here is what I am going to

Here is what I am going to test now.

kron policy-list policy_IntShut
cli config t
cli interface Gi1/0/1
cli shut
exit

kron policy-list policy_IntNoShut
cli config t
cli interface Gi1/0/1
cli no shut
exit


kron occurrence policy_IntShut at 16:00 tue recurring
no policy-list policy_IntShut
exit

kron occurrence policy_IntNoShut at 18:30 tue recurring
no policy-list policy_IntNoShut
exit

Sorry I had no in the above

Sorry I had no in the above post. This is what I was entering.

kron policy-list policy_IntShut
cli config t
cli interface Gi1/0/1
cli shut
exit

kron policy-list policy_IntNoShut
cli config t
cli interface Gi1/0/1
cli no shut
exit


kron occurrence policy_IntShut at 16:00 tue recurring
policy-list policy_IntShut
exit

kron occurrence policy_IntNoShut at 18:30 tue recurring
policy-list policy_IntNoShut
exit

Just tested and it does not

Just tested and it does not work, am I missing something?

Cisco Employee

I haven't used kron in a

I haven't used kron in a while, and I got used to the NX-OS scheduler.  Kron doesn't work with config commands.  You'll need to use a kron with a Tcl script.  Your Tcl scripts should have:

ios_config "int gi1/0/1" "shut" "end"

===

ios_config "int gi1/0/1" "no shut" "end"

Then you can schedule the script to run with the "tclsh flash:/path/to/script" CLI command.

Ok I will see if I can figure

Ok I will see if I can figure it out. I have never programmed TcL but I will give it a go.

Mike

Switch#tclshSwitch(tcl)#puts

Switch#tclsh
Switch(tcl)#puts [open "flash:shut_port.tcl" w+] "set ports [lindex argv 0] \n ios_config "int $ports" "shut" "end"
Switch(tcl)#exit
Switch#wr mem

Than to call the script at a certain time configura this.

kron policy-list policy_IntShut
tclsh flash:shut_port.tcl "Gi1/0/1"

Does this look right?

Cisco Employee

You'll need this:

You'll need this:

puts [open "flash:shut_ports.tcl" w] {set ports [lindex $argv 0]
ios_config "int $ports" "shut" "end"
}

Thank you. I will give it a

Thank you. I will give it a go.

Mike

It worked swimmingly, thank

It worked swimmingly, thank you for your help. I do have a some questions....

I understand the puts [open "flash:shut_ports.tcl" w] puts the file to flash but what does the "w" do?

Also what does the line set ports [lindex $argv 0] mean?

lindex means?

$argv 0 means?

the line ios_config "int $ports" "shut" "end" I think I get, this lets it know this is an ios configuration and the $ports is the port that is called out in the line cli tclsh flash:shut_ports.tcl "Gi1/0/1"

Now for the kron occurrence part, is there an easy way to schedule this say Monday through Friday with out creating a separate kron occurrence for each day?

Again thank you for your help.

Here is my end configuration.

!This will create a TcL script and save it to flash
!and it will shut down port Gi1/0/1 at 11:30 every Wednesday
!
tclsh
puts [open "flash:shut_ports.tcl" w] {
set ports [lindex $argv 0]
ios_config "int $ports" "shut" "end"
}
tclquit
config t

kron policy-list policy_IntShut
cli tclsh flash:shut_ports.tcl "Gi1/0/1"

kron occurrence policy_IntShut at 11:30 Wed recurring
policy-list policy_IntShut

exit
exit
wr mem
**************************************************
!This will create a TcL script and save it to flash
!and it will no shut the port Gi1/0/1 at 11:32 every Wednesday
!
tclsh
puts [open "flash:No_shut_ports.tcl" w] {
set ports [lindex $argv 0]
ios_config "int $ports" "no shut" "end"
}
tclquit
config t

kron policy-list policy_IntNoShut
cli tclsh flash:No_shut_ports.tcl "Gi1/0/1"

kron occurrence policy_IntNoShut at 11:32 Wed recurring
policy-list policy_IntNoShut

exit
exit
wr mem

Cisco Employee

The 'w' means open the file

The 'w' means open the file for writing and create a new file if it doesn't exist.

The lindex command pulls out an element from a list (element 0 in this case).

$argv is the argument vector passed to the script at execution time.

To schedule every day at a specific time, use:

occurrence NAME at 11:32 recurring

Just don't specify a day.

Thanks for your help. Mike

Thanks for your help.

Mike

New Member

How do you apply it to

How do you apply it to multiple ports?

Thanks

Cisco Employee

You can create a list of

You can create a list of ports and iterate through it.  For example, using the applet example here, you can do something like:

set ports "Gi0/1 Gi0/2 Gi0/3"

foreach port $ports

 cli command "int $port"

 cli command "shut"

end

New Member

Joe,

Joe,

Is the example that you provided for a switch that has EEM support?

I have 2960-S and 2960-X and it doesn't have EEM support.

I am trying to make the following to work, but is is not working. What need to be changed?

Thanks

tclsh
puts [open "flash:shutdown_ports.tcl" w] {set ports [lindex $argv 0]
ios_config "int range $ports" "shut" "end"
}
tclquit
config t
kron policy-list policy_ShutdownPorts
cli tclsh flash:shutdown_ports.tcl "Gi1/0/1 - 18"
exit
kron occurrence policy_ShutdownPorts at 23:30 recurring
policy-list policy_ShutdownPorts

____________________________________________________________________

tclsh
puts [open "flash:No_shutdown_ports.tcl" w] {set ports [lindex $argv 0]
ios_config "int range $ports" "no shut" "end"
}
no
tclquit
config t
kron policy-list policy_NOShutdownPorts
cli tclsh flash:No_shutdown_ports.tcl "Gi1/0/1 - 18"
exit
kron occurrence policy_NOShutdownPorts at 23:31 recurring
policy-list policy_NOShutdownPorts

Cisco Employee

The code looks fine to me,

The code looks fine to me, but tclsh doesn't work well with AAA.  If you have AAA command authz on this switch, it may not work as the user running the script isn't authorized to run the commands.

That said, since you really didn't provide any details as to what "doesn't work" means, it could be that the switch's clock is not what you think it is, or there is some other issue preventing the ports from being manipulated as a range.

Hall of Fame Super Gold

2960-X and it doesn't have

2960-X and it doesn't have EEM support.

Starting with 15.2(4)E, 2960X/XR will support EEM. 

Read THIS.

612
Views
20
Helpful
20
Replies