Using EEM & tcl script to talk to a remote http server

Unanswered Question
Aug 3rd, 2009

I would like to use the eem and a tcl script to check for any new files to download from a remote http server. The eem would be used as a scheduler to invoke the tcl script - which would access the http server and download any new files (what will be ip phone config files) to the flash on the router.

Can anyone provide me with an example config for this?



I have this problem too.
0 votes
  • 1
  • 2
  • 3
  • 4
  • 5
Overall Rating: 5 (1 ratings)
Joe Clarke Mon, 08/03/2009 - 11:14

First, you should not be using EEM to execute a tclsh script. Instead, you should do everything in an EEM Tcl policy. Second, on what version of IOS will this policy run? If you already have the tclsh script to do the HTTP operations, please post that.

Joe Clarke Mon, 08/03/2009 - 11:23

There is already HTTP code built-in to EEM. that is why I asked about IOS version and whether or not he had existing code to do his HTTP operations.

yjdabear Mon, 08/03/2009 - 11:33

Could you list the minimum version of EEM/IOS required to get built-in http get support?

thinkingphones Mon, 08/03/2009 - 11:43

I am running IOS version 12.4(15)T9 on an IAD 2431 router. I have not written the http get code yet.



Joe Clarke Mon, 08/03/2009 - 13:28

This should do what you want. It requires three environment variables to be set (examples below):

event manager environment http_check_time 86400

event manager environment http_check_url

event manager environment http_check_fs flash:

The above will run the script every 24 hours, download file.txt from the given host, and save the file to flash:/file.txt.

thinkingphones Tue, 08/04/2009 - 12:04

Thanks. I added the following to my config:

event manager environment http_check_url

event manager environment http_check_fs flash:

event manager environment http_check_time 60

event manager directory user policy "flash:/"

event manager policy tm_http_check.tcl

but saw the following in the debug:

%HA_EM-6-LOG: tm_http_check.tcl: "open flash: /file1.txt w"

%HA_EM-6-LOG: tm_http_check.tcl: Tcl policy execute failed: expected integer but got "w"

Any idea what might be wrong? (I set it to check every 60 secs just for testing)



Joe Clarke Tue, 08/04/2009 - 12:08

Did you modify my script any? It looks like something might have been changed.

thinkingphones Tue, 08/04/2009 - 12:25

I didn't modify it... I see there was a "w" at the end of this line:

set fd [open "${http_check_fs}/${fname}" "w"]

So i removed that... now when i run it i get what looks like a permissions error:

002067: .Aug 4 16:23:47.590: %HA_EM-6-LOG: tm_http_check.tcl: invalid access mode "/file1.txt": must be RDONLY, WRONLY, RDWR, APPEND, CREAT EXCL, NOCTTY, NONBLOCK, or TRUNC

Joe Clarke Tue, 08/04/2009 - 13:06

The line should read:

set fd [open "${http_check_fs}/${fname}" "w"]

It could be that the file name is being found with an embedded space. Can you post an attachment of the device config and output (copied from the terminal), so I can check?

thinkingphones Tue, 08/04/2009 - 18:57

Thanks - the new script seems to be working. What was different? I diff'd the files - but didn't see anything.

Your script requires that you must enter a filename for the http_check_url variable. How much harder would it be to have it download all the contents of a directory? Or just the files that have changed since the last download?

Joe Clarke Tue, 08/04/2009 - 20:28

The new script trims whitespace from the environment variables.

Loading all files in a directory over HTTP is non-trivial since you can't do a directory listing over HTTP. The way indexing works is that the web server generates an HTML page which lists the directory contents. Therefore, the client would need to parse the HTML data returns for the directory, and then make new HTTP requests for each file. This is doable, but as I said, it's not as trivial as doing this with a regular directory.

An alternative would be to tar up all of the files you want to download, then have the client download the tar file. IOS can easily work with tar files. You can then call the IOS archive command to extract the file. The new config files can be copied from a temp location to the final destination.

Clearly the latter method is the easiest.

thinkingphones Wed, 08/05/2009 - 12:05

So could i accomplish what i am looking for just using the EEM? Something like this:

event manager applet LoadConfigFiles

event timer cron name "load_timer" cron-entry "0 23 * * *"

action 1.0 cli command "configure terminal"

action 1.1 cli command "file prompt quiet"

action 1.2 cli command "exit"

action 2.0 cli command "copy flash:files.tar"

action 3.0 cli command "archive tar /xtract flash:files.tar flash:/"

Or is it better to use a policy tcl script?

Joe Clarke Wed, 08/05/2009 - 13:32

Yes, this would work provided you add:

action 0.5 cli command "enable"

But you could further simplify this. Change action 2.0 to:

action 2.0 cli command "archive tar /xtract flash:/"

Then remove action 3.0. The advantage of Tcl is that you could extract the tar file to a temp directory, then scan the files to see if any of them need to be installed. This may not be necessary given your end goal, though.

thinkingphones Wed, 08/05/2009 - 13:38

If i were to use the above command - the applet would be simplified to the one action line, correct? This sounds good to me!

The tar file itself will include only files that have changed or are new in the remote directory - so yeah, having a temp dir on the router should not be necessary.

thanks again,


Joe Clarke Wed, 08/05/2009 - 13:40

At the very least you'd need two actions:

action 1.0 cli command "enable"

action 2.0 cli command "archive tar /xtract ..."

Joe Clarke Mon, 08/03/2009 - 23:18

Any device which supports EEM 2.1 should have the http library. This means IOS 12.3(14)T and higher, 12.2(18)SXF5 and higher, etc.


This Discussion