So now we have conditional logic under our belt, let's now look at yet another
massive component
with Python and this is the idea of creating loops. So even if you have no
familiarity with Python,
you can probably infer that having the ability to continually repeat a similar
process over and
over and over again in your looping manner is probably going to be pretty
valuable at some point.
And honestly, this is an understatement. Lips are super super awesome. In
Python, we use them all
the time as is the case with network automation. So let's go and demystify what WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the time as is the case with network automation. So let's go and demystify what
a loop is and begin
working with them. So check this out. First thing to state here is that with
Python, we have something
known as a for loop. This is the most common implementation of a loop and
essentially it allows you
to do something a predetermined number of times. Say for example, you want to,
oh, I don't know,
let's say count to the number 10 and print out each number as you do so. Well,
in this case,
here we have a predetermined limit that is we're going to start at the number
zero and at the number 10
we're going to terminate and after that we go no further because we have this WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
we're going to terminate and after that we go no further because we have this
predetermined range
here. This would be a case for a for loop. So let's say I wanted to count to
the number 10. Okay,
so as opposed to having to say print one and then say print two and then just
continually keep
doing this over and over again. Obviously, if I happen to run this, this would
actually work in
theory, but this is not an efficient way. This is certainly not the program
matic solution. So let's
see how we can actually do this and what I can do is invoke this for loop. So
the syntax is as follows, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the syntax is as follows,
we say for and then we specify a variable value. Now you can call this anything
you wish, but again,
a good convention of variables is to make them meaningful. So what I will do is
I will call the variable
number. Okay. And then I can say in the range and then within my parentheses, I
can specify a
starting number and then a number. So I can say start at zero and finish at 11.
Then I have a
column and then if I hit return indented within is where the actual execution
is going to happen. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
is going to happen.
So what I can do is just say print out the number that's happened to be looping
through.
Now watch what happens here. Okay. If I run the new version of the script and
to enter,
let me just pull it up a little bit. We can see we go from zero all the way
down to number 10.
We loop through all of the numbers within this particular range. In fact, what
I could do is modify
that to begin at one and notice by the way, the last number is not inclusive.
So it's the number
before 11 that we're going to print out and there we go right there. So clearly
we can see here,
we're saving a lot of time and of course, I just get more dramatic. If I WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
we're saving a lot of time and of course, I just get more dramatic. If I
actually just make the
value even greater, let's say we go from one to five, oh one, which is going to
count from one to five
hundred. Let's run the script. Look at us right here. All the numbers printed
any fraction of a
second really, really good. So this obviously is a predetermined range of
numbers as I said. Now,
similarly, if I happen to have an object with multiple values such as a list
object, we can also
loop through those. Let's just have a list of IP addresses. Okay. We'll just
say IP addresses is equal
to and we'll have our square brackets. So the first one can be 10, 001. The WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to and we'll have our square brackets. So the first one can be 10, 001. The
next one can be 172,
16, 1.1. And the next one can be 192, 168. Let's go with 10.1. Okay. So right
now we have
our list object right here. And similar to what we did with our range of
numbers, we can actually
loop through each element within this list object. So I can just simply say
four and now I give my
variable value again, call anything you wish, I just maybe call it IP adder.
Okay. And then I use
the keyboard in and then specify the object I want to loop through which is WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the keyboard in and then specify the object I want to loop through which is
going to be the IP
addresses list. And what I can then do is just print out the IP adders. So
watch this as opposed
to printing out the actual list object will loop through each one and print
each element and
eventually look at that right there. So that this might not seem obviously
useful, but actually it
really is. Say for example, I wanted to type the actual command, I can say IP
address. And then
what I will do is I will use this thing called an F string, which allows me to
pass in the value
of the IP address and looping through and then say for example, I could type in WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
of the IP address and looping through and then say for example, I could type in
the subnet mask.
Okay. So that's what happens now. If I run the script, look at this right here.
Imagine this
with Cisco specific syntax because I can now access each element within the
loop. I can now type in
IP address, then this IP address followed by the subnet mask and then look to
the next one,
IP address, then this one followed by the subnet mask. Now, I don't want to get
into too much of the
weeds about Python logic, but you can also do more cool things such as
utilizing conditional logic
once again, because we now have access to each element within the list. So what
I could do is I WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
I could do is I
could say if the IP address I'm looping through starts with and it starts with
the value 10 dot.
What I want to do is I want to print out the value IP address and then pass in
the IP address.
And this time I'm going to use a slash eight mask because of the class address.
However,
I can add more conditions to say LF if the IP address I'm looping through
starts with and let's
say it starts with 172. What I can do is print I'll say IP address pass in the
IP address I'm looping
through and this time we're going to use a slash 16 mask and then I can add in WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
through and this time we're going to use a slash 16 mask and then I can add in
a condition else,
print out IP address pass in the IP address I'm looping through and use a slash
24 mask like so.
Check this out. If I now rerun the script and to enter see what's happening
because this one here
starts with a 10 we're getting a slash eight mask. This one starts with a 172
we're getting a slash 16
mask. This one here is 192 we're getting a slash 24 mask and if we just
continually add in more
values here such as let's maybe add in another 172 one so let's say 172 16 10 WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
values here such as let's maybe add in another 172 one so let's say 172 16 10
dot one and let's have
a comma here. What's what happens if I run this look at this right here this
one also gets the
slash 16 mask because we have now built in the programmatic logic to handle
what should actually
happen. Again, you can just keep adding more and more values let's add another
one with a 10.
Let's go with 10 10 11 again have a comma save you let's just close green and
arrow up. Look at
this right here this one here with a 10 10 11 once again gets a slash eight WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
this right here this one here with a 10 10 11 once again gets a slash eight
mask because it is a
class a address. Now first thing to state do not stress about trying to know
all the individual
details that I just showed you here things like using the starts with methods.
All I'm just trying
to highlight to you here at a high level is that these particular concepts
really have real world
applicability in the world of network automation. The idea of different data
types such as lists
which hold multiple values and conjunction with concepts such as looping
through those values WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
through those values
and testing each value on a particular condition and then acting on that
condition this is a type of
thing that is used all the time in network automation scripting.