cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2170
Views
5
Helpful
2
Replies

Using EEM (w/Python, Tcl, etc.) to Call an External API (REST)

rcarrara
Level 1
Level 1

Hello,

I'm not sure if this is possible but wanted to ask the question before asking specifics. Here's what I'm trying to do:

Have EEM w/IP SLA monitor an existing network connection. In the event the remote side becomes unreachable I need to run a script (or something) that will make external REST API calls to an external device. The REST API calls will tell the external device to dynamically create a second circuit (it cannot be up and running concurrently with the first). Then I want to move traffic over to the new connection. The secondary interface on the router/switch will be preconfigured on the Cisco router/switch. The actual layer 2 circuit providing transport (on an external device) is what needs to be created dynamically.

Is this possible? I know the EEM portion will work, what I don't know is if it's possible to make external calls via REST to an external device. (Via Python, EEM Applet, EEM Script, Tcl or something else.) Note, the external device is a provider switch and not Cisco but supports REST for circuit creation.

Any information is greatly appreciated.

Regards,

2 Replies 2

Carlos T
Level 1
Level 1

Hi,

 

Have you found the solution? I need to run something similar on IOS and IOS XE nodes to make rest api calls to other vendor devices, triggered by EEM events.

 

Hi Carlos, yes you should be able to do this as part of your PnP Python script using the urllib module and at the end of your script once a routable IP-address and other identification has been provisioned which initiates a post-operation toward your automation engine depending upon which object your tool is listening to………………..for regular IOS devices we don’t have a GuestShell to be able to run the Python scripting (which is part of the reason that the IOS-XE devices are more expensive, as they have additional CPU/Memory to be able to operate Linux GuestShell, etc).

 

[guestshell@guestshell /]$ ls -l /usr/lib64/python3.6/urllib

total 154

-rw-r--r--. 1 root root      0 Nov 21  2019 __init__.py

drwxr-xr-x. 2 root root   1024 Sep 23 12:31 __pycache__

-rw-r--r--. 1 root root   2641 Dec 23  2018 error.py

-rw-r--r--. 1 root root  38327 Nov 21  2019 parse.py

-rw-r--r--. 1 root root 100028 Nov 21  2019 request.py

-rw-r--r--. 1 root root   2299 Dec 23  2018 response.py

-rw-r--r--. 1 root root   8832 Dec 23  2018 robotparser.py

 

ISR4431#guestshell run python3

Python 3.6.8 (default, Nov 21 2019, 19:31:34)

[GCC 8.3.1 20190507 (Red Hat 8.3.1-4)] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import cli

>>> import urllib                                            ß import all the requests module

>>> from urllib import request                   ß or import just the request module

>>> 

>>> help(request)

Help on module urllib.request in urllib:

 

NAME

    urllib.request - An extensible library for opening URLs using a variety of protocols

 

DESCRIPTION

    The simplest way to use this module is to call the urlopen function,  which accepts a string containing a URL or a Request object (described below).  It opens the URL and returns the results as file-like object; the returned object has some extra methods described below.

 

    The OpenerDirector manages a collection of Handler objects that do all the actual work.  Each Handler implements a particular protocol or option.  The OpenerDirector is a composite object that invokes the Handlers needed to open the requested URL.  For example, the HTTPHandler performs HTTP GET and POST requests and deals with  non-error returns.  The HTTPRedirectHandler automatically deals with HTTP 301, 302, 303 and 307 redirect errors, and the HTTPDigestAuthHandler deals with digest authentication.

 

    urlopen(url, data=None) -- Basic usage is the same as original urllib.  pass the url and optionally data to post to an HTTP URL, and get a file-like object back.  One difference is that you can also pass a Request instance instead of URL.  Raises a URLError (subclass of OSError); for HTTP errors, raises an HTTPError, which can also be treated as a valid response.

snip.............

Rgds, Jim.W.

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: