cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1866
Views
0
Helpful
3
Replies

basic scripting help? - python

scott.hammond
Level 1
Level 1

I have an issue with the N5k's where a FEX gets a false temp alarm and we have to physically reseat the fex to prevent it from loosing connecton. (B22)

So I think "easy peasy I will set up EEM to send me an email when the alarm presents itself"

Apparently I was wrong about the easy peasy part- Where sending an email in IOS was a simple task now they have removed that canned functionality in NXOS in favor of calling python scripts. I can see how it gives you infinite flexibility but all I want to do is something that used to be easy and now I cant make it work to save my life. So I have tried 100 different variations of scripts and nothing works. I think I have something very basic wrong, so can someone give me an example of a working script just to see if there is something wrong with python on my 5k's?

I tried simple scripts that are just supposed to "show run" and they dont work either. I believe I am missing something very very basic that the whitepapers doesnt mention assuming that people are smarter than I am. So I am looking for a script that I can manually run to see if python even works. This is what I get when I run pretty much any script

Error seen running the script at the bottom

DAY_CR_CR03# source take4.py
ERROR: ld.so: object '/isan/lib/libsandbox.so' from LD_PRELOAD cannot be preloaded: ignored.
no sandbox
disable sandbox before enabled
no sandbox
Bareword found where operator expected at (eval 1) line 11, near ""10.60.6.125"
        msg"
        (Missing operator before msg?)
Bareword found where operator expected at (eval 1) line 12, near ")
        msg"
        (Missing operator before msg?)
Bareword found where operator expected at (eval 1) line 20, near ")
            msg"
        (Missing operator before msg?)
Bareword found where operator expected at (eval 1) line 21, near ")
            part1"
        (Missing operator before part1?)
Bareword found where operator expected at (eval 1) line 22, near ")
            part1"
        (Missing operator before part1?)
Bareword found where operator expected at (eval 1) line 23, near ")
            msg"
        (Missing operator before msg?)
Bareword found where operator expected at (eval 1) line 25, near ")
        
        part2"
        (Missing operator before part2?)
Bareword found where operator expected at (eval 1) line 26, near ")
        msg"
        (Missing operator before msg?)
Bareword found where operator expected at (eval 1) line 28, near ")
        
        server"
        (Missing operator before server?)
Bareword found where operator expected at (eval 1) line 29, near ")
        try"
        (Missing operator before try?)
Bareword found where operator expected at (eval 1) line 31, near ")
        except"
        (Missing operator before except?)
Bareword found where operator expected at (eval 1) line 38, near ""scott.hammond@rlcarriers.com"
    email_from"
        (Missing operator before email_from?)
Bareword found where operator expected at (eval 1) line 39, near ""DAY_CR_CR03@nexussucks.net"
    email_subj"
        (Missing operator before email_subj?)
Bareword found where operator expected at (eval 1) line 40, near ""DAY FEX error!"
    email_body"
        (Missing operator before email_body?)
Bareword found where operator expected at (eval 1) line 41, near ""Check the logs"
    send_email"
        (Missing operator before send_email?)

take4 script-

# -*- coding: utf-8 -*-
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText


def send_email(email_to, email_from, email_subj, email_body, input_file=None):
    if email_to:
        smtp_server = "10.60.6.125"
        msg = MIMEMultipart('alternative')
        msg['Subject'] = email_subj
        msg['Body'] = email_body
        msg['From'] = email_from
        msg['To'] = email_to
        email_to_list = email_to.split(',')
        if input_file:
            fileMA = MIMEApplication(open(input_file,"r").read());
            fileMA.add_header('Content-Disposition','attachment; filename="%s"' % input_file)
            msg.attach(fileMA)
            part1 = MIMEApplication(open(input_file,"rb").read())
            part1.add_header('Content-Disposition', 'attachment', filename=input_file)
            msg.attach(part1)
        
        part2 = MIMEText(email_body, 'plain')
        msg.attach(part2)
        
        server = smtplib.SMTP(smtp_server, 25)
        try:
            server.sendmail("scott.hammond@domain.com", email_to_list, msg.as_string())
        except Exception as e:
            print("Exception error {}".format(e))
            return False
        return True

if __name__ == "__main__":
    email_to = "scott.hammond@rlcarriers.com"
    email_from = "DAY_CR_CR03@nexussucks.net"
    email_subj = "DAY FEX error!"
    email_body = "Check the logs"
    send_email(email_to, email_from, email_subj, email_body)
    

3 Replies 3

Joe Clarke
Cisco Employee
Cisco Employee

When I read the subject, I was hoping this was an IOS-XE Python question.  It would have been the first.

In your case, you're missing the shebang line at the top.  Try adding the following as the first line and see if that helps:

#!/bin/env python

Thanks Joe!

That cut a great many of the errors down. This is what I have now after adding that.

DAY_CR_CR03# source take4.py
Permission denied. Traceback (most recent call last):
  File "/bootflash/scripts/take4.py", line 42, in <module>
    send_email(email_to, email_from, email_subj, email_body)
  File "/bootflash/scripts/take4.py", line 29, in send_email
    server = smtplib.SMTP(smtp_server, 25)
  File "/isan/python/scripts/smtplib.py", line 242, in __init__
    (code, msg) = self.connect(host, port)
  File "/isan/python/scripts/smtplib.py", line 302, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/isan/python/scripts/smtplib.py", line 277, in _get_socket
    return socket.create_connection((port, host), timeout)
  File "/isan/python/scripts/socket.py", line 583, in create_connection
    raise err
socket.error: [Errno 113] No route to host
DAY_CR_CR03#

Seems to me that your switch cannot connect to the SMTP server because of a routing issue.  This might be a VRF thing.  You might need to use:

set_vrf(VRF_NAME)

Where VRF_NAME is your management VRF that can reach the SMTP server.  Note: I found another thread where you and I discussed email on the N5K at https://supportforums.cisco.com/discussion/13261241/n5k-eem-script-send-email-when-fex-error-logged .  It wasn't clear if you got that working.