cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Joe Clarke
Hall of Fame Cisco Employee Hall of Fame Cisco Employee
Hall of Fame Cisco Employee

It is very easy to send email messages using EEM.  Often times, it is sufficient to simply include CLI output or other messages in the body of the email.  However, some output may be quite large, or you may have a need to send binary attachments (e.g. Embedded Packet Capture files).  In those cases, it would be better to use MIME encoding, and add the data as file attachments.  Here is a short proof-of-concept EEM script that demonstrates how to do this.

::cisco::eem::register_event_none

namespace import ::cisco::eem::*

namespace import ::cisco::lib::*

# First, create the headers and body of your message

set email_body_pre "Mailservername: $_email_server
From: $_email_from
To: $_email_to
Cc:
Subject: Email from Router $_router_name
MIME-Version: 1.0
Content-type: multipart/mixed; boundary=\"EEM_email_boundary\"
\n--EEM_email_boundary\n
\n--EEM_email_boundary
Content-Type: application/octet-stream
Content-Transfer-Encoding: Base64
Content-Disposition: attachment; filename=\"test.log\"\n\n"

# Then, read in the data you wish to attach.

if [catch {open "flash:test.log"} result] {
    error $result $errorInfo
}
set fd $result

# Encode that data using the built-in base64 library.
set email_b64 [::base64::encode [read $fd]]
close $fd

# Construct the final message with the headers, body, and MIME parts.

set email_body_mime "\n--EEM_email_boundary--"

set email_body [format "%s%s%s" $email_body_pre $email_b64 $email_body_mime]

# Send the email.

if [catch {smtp_send_email $email_body} result] {
    error $result $errorInfo
}

Comments
Joe Clarke
Hall of Fame Cisco Employee Hall of Fame Cisco Employee
Hall of Fame Cisco Employee

No, because for starters you don't use email_pre2 anywhere.  What you need to do is prepend the header to each MIME attachment.  So, you can do:

 

set mimeh "\n--EEM_email_boundary

Content-Type: application/octet-stream

Content-Transfer-Encoding: Base64"

 

Then append the final line for the file name each time you need it:

 

set mime1 $mimeh

append mime1 "Content-Disposition: attachment; filename=\"test.log\"\n\n"

set mime2 $mimeh

append mime2 "Content-Disposition: attachment; filename=\"test2.log\"\n\n"

 

Then construct your email using:

 

email_pre

mime1

email_b4

mime2

email_b64_2

email_body_mime

ideocisco
Beginner
Beginner

Hi,

I finally understand.


It is working.

This is what I did :

 

Thanks a lot for your help.

set email_body [format "%s%s%s%s%s%s%s" $email_pre $email_body $email_mime_1 $email_b64 $email_mime_2 $email_b64_2 $email_body_mime]

cpartsenidis
Beginner
Beginner

Joe,

Thanks for providing the script.

I've been trying to use it without any success on a 2951 router.

The problem I'm having is that I am unable to execute or register the script.  I've saved the script as email.tcl and uploaded it to the router's flash.

When trying to register it using "event manager policy email.tcl type user"  I receive the following error:

" EEM Register event failed: Error empty reg spec, policy does not start with EEM registration commands.

EEM Configuration: failed to retrieve intermediate registration result for policy email.tcl"

My question is how can I use the script correctly ?

When I replaced the first line of the script with the following"

"::cisco::eem::event_register_syslog pattern $_syslog_pattern",  the script would register, but I couldn't trigger it even despite configuring the event manager environment _syslog_pattern

What I'm trying to do is use this script with an applet that is triggered when CPU usage is high - it creates a TXT file on the router's flash and I want it to be email to me.

Any help would be much appreciated!

Thank you.

cpartsenidis
Beginner
Beginner

Just as a reference for other users, I had to change the following in the first line of the script:

::cisco::eem:event_register_none

Apparently the events have changed in the newer IOS version.

Also be very careful with the .TCL files - I had two identical .tcl files one would come up with the error : EEM Register event failed: Error empty reg spec, policy does not start with EEM registration commands:  while the other didn't.

The problem seems to be with my editor (Notepad ++ and Windows notepad), but I'm not sure why this is happening.

The file seems to need to be clear txt without any UTF or other encoding.

Many thanks,

Chris.

Firewall.cx

Eric Roach
Beginner
Beginner

Joe,

I have your script using file attachments working on ASR1002-X and an ISR4451.

I am not able to use the script with Catalyst 6880.

Version 15.2(1)SY1

The debugs did not seem to provide much additional informational, and I have used a very stripped down mime script with no attachment with same results. I did test the stripped down on ASR router and it worked. I am using the same SMTP server on the ISR4451 which works just fine.

event manager run basicsndmail.tcl
error connecting to mail server:
cannot connect to all the candidate mail servers
    while executing
"smtp_send_email $mail_msg"
    invoked from within
"$slave eval $Contents"
    (procedure "eval_script" line 7)
    invoked from within
"eval_script slave $scriptname"
    invoked from within
"if {$security_level == 1} {       #untrusted script
     interp create -safe slave
     interp share {} stdin slave
     interp share {} stdout slave
..."
    (file "tmpsys:/lib/tcl/base.tcl" line 50)
Tcl policy execute failed: error connecting to mail server:
cannot connect to all the candidate mail servers

Joe Clarke
Hall of Fame Cisco Employee Hall of Fame Cisco Employee
Hall of Fame Cisco Employee

Can the 6800 ping the SMTP server?  If you telnet from this switch to tcp/25 on the SMTP server and type "HELO Cat6800" where "Cat6800" is the hostname of the switch, what do you see?

Eric Roach
Beginner
Beginner

Ping is good and I see "Unrecognized command" see below for results.


NET_MHO_LAN_6880#telnet 10.40.11.30 25
Trying 10.40.11.30, 25 ... Open
220 MCEXCHHUB02.mclane.mclaneco.com Microsoft ESMTP MAIL Service ready at Fri, 15 Jul 2016 09:56:51 -0500


Helo NET_MHO_LAN_6880
250 MCEXCHHUB02.mclane.mclaneco.com Hello [10.40.11.7]

Edited this post b/c I mistyped Helo command.

Joe Clarke
Hall of Fame Cisco Employee Hall of Fame Cisco Employee
Hall of Fame Cisco Employee

The command is HELO (with one 'L').

Eric Roach
Beginner
Beginner

Thanks for correction.

NET_MHO_LAN_6880#telnet 10.40.11.30 25
Trying 10.40.11.30, 25 ... Open
220 MCEXCHHUB02.mclane.mclaneco.com Microsoft ESMTP MAIL Service ready at Fri, 15 Jul 2016 09:56:51 -0500


Helo NET_MHO_LAN_6880
250 MCEXCHHUB02.mclane.mclaneco.com Hello [10.40.11.7]

Joe Clarke
Hall of Fame Cisco Employee Hall of Fame Cisco Employee
Hall of Fame Cisco Employee

This could be a socket bug.  From enable mode, run tclsh, then try:

set fd [socket 10.40.11.30 25]

info exists fd

close $fd

What do you get?

Eric Roach
Beginner
Beginner

LAN_6880#tclsh


LAN_6880(tcl)#set fd [socket 10.40.11.30 25]
sock0
LAN_6880(tcl)#info exists fd
1
LAN_6880(tcl)#close $fd


LAN_6880(tcl)#tclquit

Joe Clarke
Hall of Fame Cisco Employee Hall of Fame Cisco Employee
Hall of Fame Cisco Employee

Odd.  Okay, next steps would be to enable "debug event manager tcl mail" as well as get a sniffer trace of the tcp/25 traffic between the switch and the SMTP server when trying to execute your policy.

As you've been making changes to your Tcl policy, did you execute "no event manager policy ..." then "event manager policy ..." to re-register it?

Eric Roach
Beginner
Beginner

I do re-register each change. I have been burnt enough in the past that you have ingrained that in my head from your other post.

I will get the capture and debug events tomorrow.

Thanks.

robert
Enthusiast
Enthusiast

Hi, I'm trying to pass an argument (file.txt) when running the event policy and it is throwing an error:
"_none_arg1": no such variable".

What am I am doing wrong?

This is what I run: event manager run basicsndmail.tcl file.txt

My TCL code includes this line:

set file $_none_arg1

Note: Every time I make a change to my TCL code and save, I also re-register the policy on the IOS device.
event manager directory user policy "flash:/tclscripts/"
no event manager policy basicsndmail.tcl type user
event manager policy basicsndmail.tcl type user

I appreciate if anyone can assist on how to fix this while passing args into the TCL script (which runs as an event policy).

 

Thanks

 

--Robert

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:

Quick Links