- In a previous video in this course,
which I've linked here and below,
I showed you how to use Python,
to interact with the REST API of an iOS XE router.
I showed you how to connect to that device
and retrieve a list of interfaces.
I showed you the interaction when we use Postman and Python.
So I used Postman to create an interface
and then I used Python
to retrieve the list of interfaces on the iOS XE device.
In this video, we're going to take it a step further,
we're going to use Python
to create a loopback interface on a router.
And then we're gonna use another Python script
to delete the interface.
We are iterating our scripts
and hopefully you're learning something
through each of these videos.
If you are please consider liking this video,
consider subscribing to my YouTube channel,
and consider clicking on the bell
to get notifications when I post a new video.
I'm really hoping that this free CCNP course
is helping many of you.
So please leave comments below this video
and tell me what you'd like me to cover,
tell me how the videos are helping you.
Okay, without further ado, let's get started,
and I'll show you how to create a more complex Python code.
(upbeat music)
Now originally, I was only gonna show you how to do a GET
using a Python script,
but I figured a lot of you would wanna see more than that.
So what I've done is create a whole bunch of scripts
that you can use.
The first one is, once again, the GET interfaces.
I've updated this so that we can see a status code
as well as the response text
rather than just the response text.
I've also removed import sys
because that's not actually required.
All we need is import requests.
So let me run that now,
because I'm in a different location,
wanna make sure that the script still works okay.
So, let's use a better naming convention.
So let's say getinterfaces.py.
And I'll paste that script in.
So all I've done is paste the script in that I've given you.
I'll save that.
And let's run python3 getinterfaces.py.
That should give us the interfaces on the XE router
hosted by DevNet.
As you can see at the moment, we only have gigabit one,
gigabit two and gigabit three interfaces on the router.
Okay, so now let's create an interface.
Very similar script, import requests,
we're going to disable warnings
about self signed certificates,
our username is developer,
our password is the Cisco12345,
we are connecting to this URL.
That's exactly what I showed you in Postman previously.
So if I wanted to create an interface using Postman
I'd use this URL.
Authorization is developer,
Cisco12345,
as you can see over here.
Headers, we're going to use
application yang data JSON.
So scrolling down here,
notice the headers are exactly the same,
except content type.
Exactly the same once again as Postman.
And this is what's nice about Postman,
you can build this stuff and test it
and then create Python code.
Postman actually allows you to look at the code,
so if I click on Code on the right hand side,
I can see the Python requests code.
So you could build your own script based on this output.
So going to Body as an example,
this is the command that we wanna send
to create the loopback interface.
So,
under Code you'll notice it uses this backslash,
backslash means continuation of line in Python.
So that's not very easy to read.
So going back to my script,
notice my payload is exactly this payload in Postman,
try and get those side by side.
So this
is actually this,
just with a backslash.
All I've done is added backslash
to tell Python that it's a single line.
This is one continuation,
rather than Python seeing it as separate lines.
Postman once again does something very similar here,
it's just not very easy to read.
They've also added some additional headers,
which I didn't add,
because in my actual request to the router,
I'm sending my authentication information,
which is the username and password,
Postman added to that to the headers.
So I've built my own version of this
rather than following Postman exactly.
But Postman can help you if trying to build Python scripts.
So what we're doing here is we're using requests,
the requests library,
we're sending a request, it's a POST.
So rather than sending a GET it's a POST,
the URL that we're using is this URL over here.
Authentication information is our username and password,
our headers are these headers here.
The data is the payload,
which is this JSON information,
the JSON payload.
And we're not gonna verify the certificate.
So verification of certificate is set to false.
And then we're gonna print the status code
and print the response.
So I'll copy this.
And in Ubuntu, I'll create a new script.
So nano
create1interface.py
and I'll paste that in.
Control + X, save the script.
Before I create the interface,
let's see the interfaces on the router once again.
So python3 getinterfaces.
At the moment,
you can see there are no loopback interfaces on the router.
Status code is 200,
because I changed my script
to print the status code,
and then we get the actual response.
So here's the response text,
showing us the interfaces on the router.
No loopback once again.
So python3
create1interface.py.
This will connect to the router and create an interface.
Status code is 201, response text is nothing.
That's exactly what we saw when we used Postman.
So no response is given back,
but if I run python3 getinterfaces
I should see that that interface is created on the router.
And there you go, interface has been created.
We can see that once again in Postman.
If I get interfaces on the router,
so click Send.
Notice that loopback exists.
Now if I try and create that loopback again,
so I'll run the script again, create one interface,
we should get back an error,
because the interface already exists.
So notice the status code here is 409, it's an error.
Error message object already exists.
So we can't create that interface because it already exists.
So I've created a Python script,
which once again, you can get
from the PowerPoint presentation that deletes an interface.
The difference here
is we are sending DELETE rather than POST
and under the URL, if I scroll to the right hand side,
I'm adding the interface name.
So we're importing requests as before,
we are disabling warnings about self signed certificates,
username and password are set here.
We are going to delete Loopback1234,
that I've put into a variable
that's then used as part of the URL.
Payload is blank,
headers is set to accept application yang data JSON.
Response, this is a variable.
We're using the requests library.
We're using DELETE, URL is this,
authentication, username and password, headers per this,
payload is blank.
We are not going to verify the certificate.
We're going to print out the status code
and the response text.
So I'll copy that code
in Ubuntu nano
let's create a
script called delete1interface.py.
I'll paste that code in.
Save it.
So,
before I run that, python3 getinterfaces.py.
This will once again tell us the interfaces on the router,
we can see that loopback interface.
Python3,
that's because I'm using Python three in this example,
delete1interface.py.
Let's delete that single interface.
Status code is 204.
Response text is nothing.
Get interfaces shows us that that interface is gone.
Now Postman can help if you're not sure about to the codes
or you can just look them up.
So let's create that interface again.
And then what I'll do in Postman is delete it.
So in Postman,
get interfaces, notice it's a GET,
I'll send that again,
we've got that interface on the router.
If I delete the interface, notice we're using DELETE here.
The interface that I'm gonna delete is Loopback1234.
Click Send.
Status code is 204.
Server has successfully processed the request
but is not returning any content.
That's exactly what we saw when we ran the script.
Using Python, if I try and delete it again,
this should return an error
because that interface doesn't exist.
Notice status code 404,
the famous can't find this resource error code.
If I do that in Postman once again,
requested resource could not be found.
So now I've shown you through Python code,
how to get to the interfaces on the router,
how to create a loopback on the router
and how to delete a loopback on the router.
But that's only a single loopback.
Now you may say once again
that it's easier to use the CLI to do this.
And that may be true
if you've got one router and one interface.
But let's create a whole bunch of loopbacks.
So in the next script, I'm gonna show you
how to create a whole bunch of loop backs.
And then I'll show you how to delete
a whole bunch of loop backs.
Now this video was getting too long,
so I've left the looping for the next video.
So in this video, we created a single interface,
we deleted a single interface,
don't lose sight of where we're going
with these Python scripts.
In the next video,
you'll see a little bit more about automation,
where I can create a whole bunch of loopback interfaces
and delete a whole bunch of loopback interfaces
on a single device.
Don't get hung up about the fact
that it's a loopback interface.
I'm starting simply,
I'm trying to show you through simple code,
how you can do things using Python.
Just imagine where this can go.
You can eventually create a script
that creates a whole bunch of VLANs on many, many devices,
or configures the interfaces of switches
to a standard that you've got.
So you could connect to 100 switches,
configure 12 or 24 ports in every one of those switches
to a standard.
That's the power of automation,
or one of the options in automation,
there are many others as well.
But one of the options
is rather than you manually doing things,
you get a computer or a script to do it for you.
Spend your time on the intelligence stuff
like which VLANs do we actually need?
Or do we actually need to enable this port?
Shouldn't it be shut down?
You need to make the intelligent decisions
and then delegate the basic repetitive tasks to code.
Okay, see you in the next video.
(upbeat music)