WEBVTT

1
00:00.000 --> 00:03.840
So now we have conditional logic under our belt, let's now look at yet another

2
00:03.840 --> 00:04.640
massive component

3
00:04.640 --> 00:09.340
with Python and this is the idea of creating loops. So even if you have no

4
00:09.340 --> 00:11.200
familiarity with Python,

5
00:11.200 --> 00:15.760
you can probably infer that having the ability to continually repeat a similar

6
00:15.760 --> 00:16.720
process over and

7
00:16.720 --> 00:20.370
over and over again in your looping manner is probably going to be pretty

8
00:20.370 --> 00:21.680
valuable at some point.

9
00:21.680 --> 00:26.410
And honestly, this is an understatement. Lips are super super awesome. In

10
00:26.410 --> 00:27.440
Python, we use them all

11
00:27.440 --> 00:32.400
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

11
00:27.440 --> 00:32.400
the time as is the case with network automation. So let's go and demystify what

12
00:32.400 --> 00:34.000
a loop is and begin

13
00:34.000 --> 00:37.650
working with them. So check this out. First thing to state here is that with

14
00:37.650 --> 00:38.640
Python, we have something

15
00:38.640 --> 00:43.000
known as a for loop. This is the most common implementation of a loop and

16
00:43.000 --> 00:44.320
essentially it allows you

17
00:44.320 --> 00:48.760
to do something a predetermined number of times. Say for example, you want to,

18
00:48.760 --> 00:49.120
oh, I don't know,

19
00:49.120 --> 00:53.200
let's say count to the number 10 and print out each number as you do so. Well,

20
00:53.200 --> 00:53.600
in this case,

21
00:53.600 --> 00:57.650
here we have a predetermined limit that is we're going to start at the number

22
00:57.650 --> 00:59.120
zero and at the number 10

23
00:59.120 --> 01:02.730
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

23
00:59.120 --> 01:02.730
we're going to terminate and after that we go no further because we have this

24
01:02.730 --> 01:03.920
predetermined range

25
01:03.920 --> 01:07.490
here. This would be a case for a for loop. So let's say I wanted to count to

26
01:07.490 --> 01:08.800
the number 10. Okay,

27
01:08.800 --> 01:12.860
so as opposed to having to say print one and then say print two and then just

28
01:12.860 --> 01:13.920
continually keep

29
01:13.920 --> 01:18.740
doing this over and over again. Obviously, if I happen to run this, this would

30
01:18.740 --> 01:19.760
actually work in

31
01:19.760 --> 01:23.030
theory, but this is not an efficient way. This is certainly not the program

32
01:23.030 --> 01:25.040
matic solution. So let's

33
01:25.040 --> 01:28.890
see how we can actually do this and what I can do is invoke this for loop. So

34
01:28.890 --> 01:30.240
the syntax is as follows,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

34
01:28.890 --> 01:30.240
the syntax is as follows,

35
01:30.240 --> 01:34.240
we say for and then we specify a variable value. Now you can call this anything

36
01:34.240 --> 01:35.520
you wish, but again,

37
01:35.520 --> 01:39.130
a good convention of variables is to make them meaningful. So what I will do is

38
01:39.130 --> 01:40.400
I will call the variable

39
01:40.400 --> 01:46.200
number. Okay. And then I can say in the range and then within my parentheses, I

40
01:46.200 --> 01:47.120
can specify a

41
01:47.120 --> 01:54.300
starting number and then a number. So I can say start at zero and finish at 11.

42
01:54.300 --> 01:55.040
Then I have a

43
01:55.040 --> 01:59.090
column and then if I hit return indented within is where the actual execution

44
01:59.090 --> 02:00.160
is going to happen.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

44
01:59.090 --> 02:00.160
is going to happen.

45
02:00.160 --> 02:04.260
So what I can do is just say print out the number that's happened to be looping

46
02:04.260 --> 02:04.800
through.

47
02:04.800 --> 02:08.320
Now watch what happens here. Okay. If I run the new version of the script and

48
02:08.320 --> 02:08.960
to enter,

49
02:08.960 --> 02:12.750
let me just pull it up a little bit. We can see we go from zero all the way

50
02:12.750 --> 02:13.920
down to number 10.

51
02:13.920 --> 02:18.120
We loop through all of the numbers within this particular range. In fact, what

52
02:18.120 --> 02:18.960
I could do is modify

53
02:18.960 --> 02:22.810
that to begin at one and notice by the way, the last number is not inclusive.

54
02:22.810 --> 02:23.600
So it's the number

55
02:23.600 --> 02:26.960
before 11 that we're going to print out and there we go right there. So clearly

56
02:26.960 --> 02:27.680
we can see here,

57
02:27.680 --> 02:30.710
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

57
02:27.680 --> 02:30.710
we're saving a lot of time and of course, I just get more dramatic. If I

58
02:30.710 --> 02:31.680
actually just make the

59
02:31.680 --> 02:34.780
value even greater, let's say we go from one to five, oh one, which is going to

60
02:34.780 --> 02:36.320
count from one to five

61
02:36.320 --> 02:40.720
hundred. Let's run the script. Look at us right here. All the numbers printed

62
02:40.720 --> 02:41.760
any fraction of a

63
02:41.760 --> 02:45.830
second really, really good. So this obviously is a predetermined range of

64
02:45.830 --> 02:47.360
numbers as I said. Now,

65
02:47.360 --> 02:51.880
similarly, if I happen to have an object with multiple values such as a list

66
02:51.880 --> 02:53.200
object, we can also

67
02:53.200 --> 02:56.680
loop through those. Let's just have a list of IP addresses. Okay. We'll just

68
02:56.680 --> 02:57.920
say IP addresses is equal

69
02:57.920 --> 03:02.160
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

69
02:57.920 --> 03:02.160
to and we'll have our square brackets. So the first one can be 10, 001. The

70
03:02.160 --> 03:04.640
next one can be 172,

71
03:04.640 --> 03:10.960
16, 1.1. And the next one can be 192, 168. Let's go with 10.1. Okay. So right

72
03:10.960 --> 03:11.600
now we have

73
03:11.600 --> 03:15.800
our list object right here. And similar to what we did with our range of

74
03:15.800 --> 03:17.120
numbers, we can actually

75
03:17.120 --> 03:21.780
loop through each element within this list object. So I can just simply say

76
03:21.780 --> 03:23.360
four and now I give my

77
03:23.360 --> 03:28.480
variable value again, call anything you wish, I just maybe call it IP adder.

78
03:28.480 --> 03:29.360
Okay. And then I use

79
03:29.360 --> 03:32.440
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

79
03:29.360 --> 03:32.440
the keyboard in and then specify the object I want to loop through which is

80
03:32.440 --> 03:33.440
going to be the IP

81
03:33.440 --> 03:37.780
addresses list. And what I can then do is just print out the IP adders. So

82
03:37.780 --> 03:38.720
watch this as opposed

83
03:38.720 --> 03:42.440
to printing out the actual list object will loop through each one and print

84
03:42.440 --> 03:43.200
each element and

85
03:43.200 --> 03:46.190
eventually look at that right there. So that this might not seem obviously

86
03:46.190 --> 03:47.280
useful, but actually it

87
03:47.280 --> 03:51.430
really is. Say for example, I wanted to type the actual command, I can say IP

88
03:51.430 --> 03:52.240
address. And then

89
03:52.240 --> 03:55.680
what I will do is I will use this thing called an F string, which allows me to

90
03:55.680 --> 03:56.800
pass in the value

91
03:56.800 --> 04:00.080
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

91
03:56.800 --> 04:00.080
of the IP address and looping through and then say for example, I could type in

92
04:00.080 --> 04:01.040
the subnet mask.

93
04:01.040 --> 04:05.720
Okay. So that's what happens now. If I run the script, look at this right here.

94
04:05.720 --> 04:06.160
Imagine this

95
04:06.160 --> 04:09.940
with Cisco specific syntax because I can now access each element within the

96
04:09.940 --> 04:11.280
loop. I can now type in

97
04:11.280 --> 04:14.940
IP address, then this IP address followed by the subnet mask and then look to

98
04:14.940 --> 04:15.600
the next one,

99
04:15.600 --> 04:19.400
IP address, then this one followed by the subnet mask. Now, I don't want to get

100
04:19.400 --> 04:20.160
into too much of the

101
04:20.160 --> 04:24.520
weeds about Python logic, but you can also do more cool things such as

102
04:24.520 --> 04:26.400
utilizing conditional logic

103
04:26.400 --> 04:29.990
once again, because we now have access to each element within the list. So what

104
04:29.990 --> 04:30.720
I could do is I
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

104
04:29.990 --> 04:30.720
I could do is I

105
04:30.720 --> 04:34.990
could say if the IP address I'm looping through starts with and it starts with

106
04:34.990 --> 04:36.400
the value 10 dot.

107
04:36.400 --> 04:41.880
What I want to do is I want to print out the value IP address and then pass in

108
04:41.880 --> 04:43.040
the IP address.

109
04:43.040 --> 04:47.020
And this time I'm going to use a slash eight mask because of the class address.

110
04:47.020 --> 04:47.520
However,

111
04:47.520 --> 04:50.960
I can add more conditions to say LF if the IP address I'm looping through

112
04:50.960 --> 04:52.240
starts with and let's

113
04:52.240 --> 04:57.660
say it starts with 172. What I can do is print I'll say IP address pass in the

114
04:57.660 --> 04:58.880
IP address I'm looping

115
04:58.880 --> 05:02.860
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

115
04:58.880 --> 05:02.860
through and this time we're going to use a slash 16 mask and then I can add in

116
05:02.860 --> 05:04.320
a condition else,

117
05:04.320 --> 05:10.260
print out IP address pass in the IP address I'm looping through and use a slash

118
05:10.260 --> 05:12.000
24 mask like so.

119
05:12.000 --> 05:16.960
Check this out. If I now rerun the script and to enter see what's happening

120
05:16.960 --> 05:17.920
because this one here

121
05:17.920 --> 05:22.160
starts with a 10 we're getting a slash eight mask. This one starts with a 172

122
05:22.160 --> 05:23.440
we're getting a slash 16

123
05:23.440 --> 05:27.820
mask. This one here is 192 we're getting a slash 24 mask and if we just

124
05:27.820 --> 05:29.280
continually add in more

125
05:29.280 --> 05:36.020
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

125
05:29.280 --> 05:36.020
values here such as let's maybe add in another 172 one so let's say 172 16 10

126
05:36.020 --> 05:37.040
dot one and let's have

127
05:37.040 --> 05:41.900
a comma here. What's what happens if I run this look at this right here this

128
05:41.900 --> 05:43.120
one also gets the

129
05:43.120 --> 05:47.570
slash 16 mask because we have now built in the programmatic logic to handle

130
05:47.570 --> 05:48.560
what should actually

131
05:48.560 --> 05:51.480
happen. Again, you can just keep adding more and more values let's add another

132
05:51.480 --> 05:52.320
one with a 10.

133
05:52.320 --> 05:58.080
Let's go with 10 10 11 again have a comma save you let's just close green and

134
05:58.080 --> 05:59.360
arrow up. Look at

135
05:59.360 --> 06:03.450
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

135
05:59.360 --> 06:03.450
this right here this one here with a 10 10 11 once again gets a slash eight

136
06:03.450 --> 06:04.560
mask because it is a

137
06:04.560 --> 06:08.870
class a address. Now first thing to state do not stress about trying to know

138
06:08.870 --> 06:09.920
all the individual

139
06:09.920 --> 06:13.920
details that I just showed you here things like using the starts with methods.

140
06:13.920 --> 06:14.400
All I'm just trying

141
06:14.400 --> 06:18.560
to highlight to you here at a high level is that these particular concepts

142
06:18.560 --> 06:20.320
really have real world

143
06:20.320 --> 06:23.970
applicability in the world of network automation. The idea of different data

144
06:23.970 --> 06:25.680
types such as lists

145
06:25.680 --> 06:29.400
which hold multiple values and conjunction with concepts such as looping

146
06:29.400 --> 06:30.560
through those values
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

146
06:29.400 --> 06:30.560
through those values

147
06:30.560 --> 06:34.710
and testing each value on a particular condition and then acting on that

148
06:34.710 --> 06:36.080
condition this is a type of

149
06:36.080 --> 06:40.000
thing that is used all the time in network automation scripting.
