11-06-2021 12:12 AM
I was trying a python script to send email on cisco nexus 9000 series switch but it says no route to host. I can ping the host from the switch
Solved! Go to Solution.
11-07-2021 10:26 PM
Dear George,
Thank you very much!
This had solved my problem.
from cisco.vrf import *
set_global_vrf('default')
I was out of office due to the weekend break that's why I didn't responded immediately.
11-06-2021 12:23 AM
Hello,
post the script and the configuration of the Nexus...
11-06-2021 12:32 AM
Dear Georg,
Thank you for the reply,
Here is the sript
==========================
#!/bin/env python
import smtplib
import cisco
from cli import cli
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# set_vrf('default')
# me == my email address
# you == recipient's email address
me = "at@at.com"
you = "at@at.com"
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "DC Interconnect Switching to VPN"
msg['From'] = me
msg['To'] = you
# Create the body of the message (a plain-text and an HTML version).
hostname = cli('show hostname').rstrip()
track = cli('show track brief')
text = hostname + " IP SLA Tracking Object 10 is UP, switching to VPN for DC Interconnectivity.\n\n" + track
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
# Send the message via local SMTP server.
s = smtplib.SMTP('10.1.18.23', 25)
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
s.quit()
==========================
Here is the ping result
ping 10.1.18.23
PING 10.1.18.23 (10.1.18.23): 56 data bytes
64 bytes from 10.1.18.23: icmp_seq=0 ttl=125 time=1.352 ms
64 bytes from 10.1.18.23: icmp_seq=1 ttl=125 time=1.132 ms
64 bytes from 10.1.18.23: icmp_seq=2 ttl=125 time=0.936 ms
64 bytes from 10.1.18.23: icmp_seq=3 ttl=125 time=1.131 ms
64 bytes from 10.1.18.23: icmp_seq=4 ttl=125 time=0.915 ms
11-06-2021 01:17 AM
Hello,
this could be related to VRFs. Try and add the below to your script:
from cisco.vrf import *
VRF.get_vrf_name_by_id(get_global_vrf()) 'default'
11-06-2021 01:24 AM
Here is the error
2# python bootflash:N9K_EEM_TRACK_SLA_10_UP.py
File "/bootflash/N9K_EEM_TRACK_SLA_10_UP.py", line 8
VRF.get_vrf_name_by_id(get_global_vrf()) 'default'
^
SyntaxError: invalid syntax
11-06-2021 01:35 AM
Hello,
you commented out the set_vrf. Try the two scripts below. I am pretty sure it has to do with the vrf settings. Otherwise, post the running config of your Nexus:
from cisco.vrf import *
set_global_vrf('management')
or
from cisco.vrf import *
set_global_vrf('default')
11-07-2021 10:26 PM
Dear George,
Thank you very much!
This had solved my problem.
from cisco.vrf import *
set_global_vrf('default')
I was out of office due to the weekend break that's why I didn't responded immediately.
11-07-2021 11:46 PM
Hello,
good stuff, glad you got the issue resolved !
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide