cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
3981
Views
26
Helpful
4
Comments
Carles Ferrater
Cisco Employee
Cisco Employee

In this post I would like to explain how to create a small POC together with a Spark bot and the Devnet Sandbox IOS XE on Catalyst 9000.

In this POC you will be able to showcase 2 main concepts:

  1. How Catalyst 9000 Guestshell works.
  2. How to integrate a Catalyst 9000 with Spark and begin a ChatOps model in your orgranization.

1) Reserve a Sandbox.

Go to the following link and reserve a IOS XE on Catalyst 9000 sandbox to work on. It may take around 10 minutes for the entire process to finish.

2) Create a Spark bot and write down bot's access token.

Go to https://developer.ciscospark.com/apps.html and create a new bot. Click in My Apps > Create a Bot and later to the add new application:

picc1.png

picc2.png

Create the new Bot with relevant information and make sure you write down Bot's access token.

picc3.png

3) Prepare  Catalyst 9300 guestshell

  • Now it's time to connect to the Catalyst 9300 in the Devnet sandbox. Details of this step will be sent by email once the lab has been fully provisioned. Summary: Use Cisco AnyConnect to VPN towards the LAB Sandbox. If you need additional information on this step please check the following link


  • Connect to the Catalyst 9300 terminal with the right credentials shared in the Sandbox main page. Cat9300 management IP: 10.10.20.100. Credentials admin/ciscopsdt

mylaptop:~ laptop$ ssh admin@10.10.20.100           

  • Enable Guestshell in Catalyst 9300 with the following commands:

>cat9300-2#guestshell enable


Management Interface will be selected if configured

Please wait for completion

guestshell is in RUNNING state


>cat9300-2#guestshell


[guestshell@guestshell ~]$

  • IF above commands didn't work and guesthsell bash is not presented, you will need to destroy current guestshell and configure a new one. Do the following steps only if previous step didn't work.

>cat9300-2#guestshell destroy

Guestshell destroyed successfully


>cat9300-2#guestshell enable

Management Interface will be selected if configured

Please wait for completion

Guestshell enabled successfully


>cat9300-2#guestshell


[guestshell@guestshell ~]$

  • Now that we have the Python guestshell running without any issue is time to update some repositories, libraries and configurations to be able to install and run Spark API and Spark scripts without any problem.

First we need to update the DNS configuration with a proper DNS server to resolve DNS queries. Let's use OpenDNS::208.67.222.222

[guestshell@guestshell ~]$ sudo echo "nameserver 208.67.222.222" | sudo tee --append /etc/resolv.conf


nameserver 208.67.222.222

Following, we need to update repositories, libraries and needed tools. Make sure you type "yes / y" when asked and allow the installation of needed packages.  (You can ignore some of the errors presented when installing different modules)

[guestshell@guestshell ~]$ sudo yum update


[...]

Complete!


[guestshell@guestshell ~]$ sudo pip install --upgrade pip


[...]

Successfully installed pip-10.0.0


[guestshell@guestshell ~]$ sudo pip install -U setuptools


[...]

Successfully installed setuptools-39.0.1

Now that we have the guestshell ready we can install Cisco Spark API:

[guestshell@guestshell ~]$ sudo -E pip install ciscosparkapi


[...]

Successfully installed certifi-2018.1.18 chardet-3.0.4 ciscosparkapi-0.10 future-0.16.0 idna-2.6 requests-2.18.4 requests-toolbelt-0.8.0 urllib3-1.22

  • After this steps we have the guestshell ready to run python scripts using the CiscoSparkApi.

4) Run first python script and test SparkBot

With the guestshell ready it's time to try a script.  (( Thanks @HaroldPreston for putting this together in your post ))

  1. from ciscosparkapi import CiscoSparkAPI 
  2.  
  3. if __name__=='__main__'
  4.     # Use ArgParse to retrieve command line parameters. 
  5.     from argparse import ArgumentParser 
  6.     parser = ArgumentParser("Spark Check In"
  7.     # Retrieve the Spark Token and Destination Email 
  8.     parser.add_argument( 
  9.         "-t", "--token", help="Spark Authentication Token", required=True 
  10.     ) 
  11.     # Retrieve the Spark Token and Destination Email 
  12.     parser.add_argument( 
  13.         "-e", "--email", help="Email to Send to", required=True 
  14.     ) 
  15.     args = parser.parse_args() 
  16.     token = args.token 
  17.     email = args.email 
  18.     message = "**Alert:** Config Changed" 
  19.     api = CiscoSparkAPI(access_token=token) 
  20.     api.messages.create(toPersonEmail=email, markdown=message) 

We need to create a .py file with the above code in order to be executed by the guestshell and reach to Spark. In this example we will create "myscript.py"

1) COPY the script text above: select the text, right click copy

2) Create new file, for example myscript.py Let's use here for example the vi editor:

[guestshell@guestshell ~]$ sudo vi myscript.py

3) Once you enter vi, press "i" to get into insert mode, right click into terminal, click paste.

4) Press key ESC and type :wq! then press ENTER to save all the document.


~

~

"myscript.py" [New] 20L, 780C written

[guestshell@guestshell ~]$

At this point we should have our "myscript.py" in the Catalyst 9300 guestshell. We can verify it by typing any of the commands below:

[guestshell@guestshell ~]$ ls

myscript.py

[guestshell@guestshell ~]$ cat myscript.py

from ciscosparkapi import CiscoSparkAPI

if __name__=='__main__':

    # Use ArgParse to retrieve command line parameters.

    from argparse import ArgumentParser

    parser = ArgumentParser("Spark Check In")

    # Retrieve the Spark Token and Destination Email

    parser.add_argument(

        "-t", "--token", help="Spark Authentication Token", required=True

    )

    # Retrieve the Spark Token and Destination Email

    parser.add_argument(

        "-e", "--email", help="Email to Send to", required=True

    )

    args = parser.parse_args()

    token = args.token

    email = args.email

    message = "**Alert:** Config Changed"

    api = CiscoSparkAPI(access_token=token)

    api.messages.create(toPersonEmail=email, markdown=message)

Now that the script is in the guestshell is time to run it and verify that is working:

sudo python myscript.py -t "Bot's access token." -e "our email where spark is enabled"


For example:

[guestshell@guestshell ~]$ sudo python myscript.py -t ZjZjMTkxY2ItYTUxNy00MGJjLTg1MDEtNzQ5MzgyOWEyOTcwZjM0NmE2NzItNDI2 -e cferrate@cisco.com

As a result we should see a Spark notification from our Bot:

picc4.png

At this point we have validated that the Catalyst9300 guesthsell is able to send messages to our Spark account using the Bot created previously.  

5) Using the script as an action after Cisco Embedded Event Manager (EEM) event.

With the script working, now we will make the Catalyst 9300 to execute the script and send a message over Spark every time a Syslog Message related to a configuration change is issued. For this, we will use above script as an action to a EEM event.

(( Thanks again @HaroldPreston for putting this together in your post ))


First we need to exit the guestshell


[guestshell@guestshell ~]$ exit

exit

cat9300-2#

Now we configure the EEM:

cat9300-2#config t

cat9300-2(config)#event manager applet testapplet

cat9300-2(config-applet)#event syslog pattern "%SYS-5-CONFIG_I: Configured from"

cat9300-2(config-applet)#action 0.0 cli command "en"

cat9300-2(config-applet)#action 1.0 cli command "guestshell run python myscript.py -t ZjZjMTkxY2ItYTUxNy00MGJjLTg1MDEtNzQ5MzgyOWEyOTcwZjM0NmE2NzItNDI2 -e cferrate@cisco.com"

cat9300-2(config-applet)#exit

cat9300-2(config)#exit

With this every time there is a system configuration change syslog message the EEM will run the script in the guestshell and this notify us over spark of a configuration change.

Be aware that EEM event pattern support regex so you could potentially match any type of variable in the syslog messages using the exact same approach as above.

Comments
thodavie
Cisco Employee
Cisco Employee

Great example of using DevNet Sandbox, Cat 9K and Spark APIs for some ChatOps action! Love it!

crnewman
Level 5
Level 5

Thanks for your contribution, Charles!  We've added your POC/post as a example within the instructions of the IOS XE on Catalyst 9000 lab. 

DAVE BLACKBOURN
Level 1
Level 1

Attempting the transition from learning Labs to sandbox.  Tried other Labs without success.  Everything spelled out in this Lab (and I have Cisco Spark).  Thanks for being so thorough, I can now attempt the other labs.

Carles Ferrater
Cisco Employee
Cisco Employee

Thanks Dave; happy to help

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