Alright friends, let's talk about more of the actual front stuff then.
So if you happen to recall the earlier skill we talked about Ansible,
in this particular scenario we're doing kind of the same thing just using a
different utility.
What I've got here is an Ubuntu Linux machine and out here are just some basic
Cisco iOS switches.
Now all of this is on my local network of 192.168.1.0/24 and each of these
devices here have their
own management IP address I believe. This one here is .101. This is .102. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
own management IP address I believe. This one here is .101. This is .102.
This one is 103 and this one is 104. Now all of the devices should have a login
user name of
admin as well as a password of admin pass and they're going to be reached over
port 22
utilizing SSH. So these are the main connection details that we have to
understand.
The question now therefore is how do we actually use Python over here to talk
to the devices
over there? Well we're going to be looking at a super super popular and well
known network
automation utility and this is known as NetMico. NetMico essentially allows us WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
automation utility and this is known as NetMico. NetMico essentially allows us
to send CLI commands
to our target devices over an SSH channel. So given the fact that these are old
,
dusty devices that are not capable of modern automation, utilizing SSH with Net
Mico is a very,
very good solution. So let's take that information and begin writing our script
.
Now just in a similar way to what we saw in the previous video, we happened to
install something
called PyYaml and to a virtual environment and this gave us access to that WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
called PyYaml and to a virtual environment and this gave us access to that
particular library.
Now in a similar vein we want to get access to NetMico so all I will say is pip
3 install
NetMico and this will now install NetMico with the virtual environment once
again giving
me access to it with thinmypython scripts. So what I will do is just create a
new script,
let's call this example 4.py and let me show you how we can begin doing this
then.
So let's open this up, we'll just go in here. So at the very beginning what I
will say is
from NetMico, what I will do is import the connect handler factory function. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
from NetMico, what I will do is import the connect handler factory function.
Now in the interest of simplicity all I will do right now is just write the
code to connect to
one of my devices as opposed dealing with all of them. So right here what I
will do is I'll target
this one right here, S1 okay. Now before we begin can you remember when we
talked about
opening files we had this thing called a context handler. Now we happen to use
this to open a
particular file and then automatically close that file once we were done with
it. Now the cool thing
is that this doesn't just work with files we can actually use them in the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
is that this doesn't just work with files we can actually use them in the
context of managing a
connection. So pretty much we can use it to open a connection to a device and
then once we are
finished gracefully close that connection. So first things first what I'm going
to do is to
define some particular details that NetMico needs to know. So what I will do
here is create a little
Python dictionary and the first actual key I'm going to use is going to be the
device type.
The type of device I'm talking to is going to be a Cisco iOS device so I'll
pass in that particular WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
pass in that particular
value. Then have a comma. The next key is going to be the host key that allows
me to specify which
IP address I want to target. So I'll say 1,9,2,168,1.101 which is the IP
address of the first device.
I'll then specify the username key and the username is going to be admin. The
next key is going to be
the password key and that's going to be admin pass. So let's now utilize my
context manager. I will
say with connect handler that I'm importing and I'm going to pass in some
particular values. I'm
going to say the device type is equal to my device object and I'm going to get WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
going to say the device type is equal to my device object and I'm going to get
the device type key.
Then also the host is equal to and I'll take my device dictionary and this time
parse out the
host key. I'll then say the username is equal to the device and take the
username key and then the
password is equal to the device and grab the password key. Now what I want to
do is I want to store all
of this as a simple object and I'll just call this connection okay. So if I hit
return here I'm
indented for spaces you see that they're 1,2,3,4 so that means anything I do WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
indented for spaces you see that they're 1,2,3,4 so that means anything I do
from this point is going
to be with the open connection. So what I will do here is I will take my
connection objects and
I'm going to use a particular method in that meco known as send command see
this one right here.
Pretty much this is the method we use when we want to send a show command to a
device and not
make a configuration change just simply a show command. So what we'll do is
with the parentheses I
will specify the show command that I want to run. So let's maybe go with show
IP interface WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
belief. Now I actually want to save the output of this command so I'll save it
to a variable so
I'll save results equal to this and all I will now do is print out the result.
So let's go and see
how this actually works in. So let's run a script Python 3 example 4.py and
enter. In BAM as we can see
here we're now getting the information back for that particular device we can
actually see the
IP address right there in the show IP interface brief output. Now in the
example right here I happen
to write this out any long hand format so you can actually see all of the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to write this out any long hand format so you can actually see all of the
underlying details
but see all of this stuff right here we can actually just directly unpack the
dictionary just by
using the short hand format watch this if I delete all of this and I just say
star star and then give
the name the dictionary which is device this has the exact same effect of what
I just had before
which is a lot shorter and a lot smarter to look at. If I just save this and I
arrow up and try
again BAM we get the exact same information so now we're able to define our
information directly
within a Python script and we can use the mclu with a context manager to open a WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
within a Python script and we can use the mclu with a context manager to open a
connection send the
command and then gracefully close that connection. What I want to do is a
similar thing but this
thing utilize multiple different devices so straight away let's try to remove
this information
from the script itself and move it into its own YAML file so let's go and do
that then. Okay such
delete you and what I will do is I will create a little file just called
inventory dot YAML looking
so let's open this up here so let's begin defining our inventory so what I will
have here is a
key called device at the very top now in the end of the thing I'm going to put WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
key called device at the very top now in the end of the thing I'm going to put
the same type of
information that we just saw before so I'll say the device type is equal to
Cisco iOS I'll say the
host is equal to 192 168 1.101 I'll have the username as admin and the password
as admin password
okay so now we have this separate file and what I want to do is I want to read
in the content from
that particular file so we know how to do this we can import the YAML directory
or rather the YAML
directory the YAML library and we want to make this information accessible so WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
directory the YAML library and we want to make this information accessible so
once again we're going
to use another context manager so I'll say with open and then I'll specify the
name of the file I want
to open which in my case here is going to be infantory dot YAML and I'll have
my comma we want to open
it in read mode and I'll just say as target file which maybe say device info is
equal to and I'll
take the YAML library use the safe load function and I'll pass in the target
file so now I've opened
up this particular file and save the content as structured data into this WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
up this particular file and save the content as structured data into this
variable right here
device info so right now if I happen to print device info and for the moment
here if I just
comment all this out so this portion does not execute okay so this is not going
to have any effect
anymore and before I do that actually let's make sure I've spelled that
correctly that should be
dot YAML not dot YAML so now let's try to run the script look at this right
here okay see this
we have this dictionary object now if I just grab this device key this is going
to give me access to
all of this inner information okay let's just do this grab this key save you
are a up now we have WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
are a up now we have
access to all the exact same information that we saw before device type the
host username as well
as a password and we want to now pass this information into netmeco so as
opposed to printing this
out what I will do is just get rid of this print statements and I'll just say
device is equal to
whatever it is we're parsing out right there so now if I just grab all this and
I uncomment it so
it's going to execute once again now what's happening is we open the actual YAM
L file we then load it
using YAML save it into this particular variable we parse out the top level key WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
using YAML save it into this particular variable we parse out the top level key
right here device
and if you look at the data here this is the top level key meaning that when we
parse it out we get
access to all of the inner data right here and this is now stored as a variable
known as device
which we then pass in to netmeco right here which ultimately means you pass all
of that data all
of this data right here directly into netmeco and netmeco can now connect to
this particular device
so let's clear the screen arrow up try again and as we can see here we are once WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
so let's clear the screen arrow up try again and as we can see here we are once
again connecting to
our device this time though we're reading it from an inventory YAML file now so
far we have made
two good steps the first thing we did was connect to a device by directly
baking in the connection
details within our Python script and then the second one was to load in the
information from a
separate YAML file now as it transpires right now we are only connecting to a
singular device
and this is quite frankly not so useful when you have many many devices so how
about we modify what
is in that inventory YAML file to include the rest of our devices and then we WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
is in that inventory YAML file to include the rest of our devices and then we
'll talk about how we
can introduce a loop into this script to go through all of those devices sequ
entially and send that
command to all of the devices in the network