1 00:00:00,960 --> 00:00:01,230 Okay. 2 00:00:01,230 --> 00:00:05,100 So let's edit our Python three three script. 3 00:00:08,140 --> 00:00:13,330 What we want to do here is replace all our VLAN code with a loop. 4 00:00:14,020 --> 00:00:16,360 So I'm going to paste a loop in. 5 00:00:18,610 --> 00:00:23,860 This is very similar to what we saw previously for MN in range 2 to 11. 6 00:00:24,250 --> 00:00:28,210 Rather than simply printing the information to the screen. 7 00:00:29,110 --> 00:00:34,690 We're going to write it to the switch in a similar way to what we did previously. 8 00:00:36,410 --> 00:00:40,580 So we're going to write a VLAN followed by a string. 9 00:00:41,280 --> 00:00:46,530 We have to convert and which is an integer to a string here. 10 00:00:47,350 --> 00:00:52,690 And then encoded as ASCII text when sending it to the switch. 11 00:00:53,140 --> 00:00:55,660 The reason for doing that is the following. 12 00:00:59,160 --> 00:01:01,470 So let's go back to our range script. 13 00:01:03,190 --> 00:01:05,620 If I edited our script and said Print 14 00:01:07,750 --> 00:01:17,000 VLAN plus PN, we'd get an error, so let's try that. 15 00:01:17,020 --> 00:01:19,090 So Python three. 16 00:01:21,310 --> 00:01:23,230 Python ranged at p y. 17 00:01:23,950 --> 00:01:25,210 Notice the error. 18 00:01:25,630 --> 00:01:32,260 Python doesn't like the statement because this is an integer and this is a string. 19 00:01:33,060 --> 00:01:35,280 So let's talk a little bit about that. 20 00:01:35,310 --> 00:01:37,380 If I say X equals one. 21 00:01:39,270 --> 00:01:42,260 And then use type to check what X is. 22 00:01:42,270 --> 00:01:48,210 We told that x is an integer or int integers or whole numbers. 23 00:01:48,840 --> 00:01:56,250 If I say y equals to type the type of y, the type of y is integer. 24 00:01:56,730 --> 00:01:58,230 Those are both integers. 25 00:01:58,410 --> 00:02:00,960 So X plus y is accepted. 26 00:02:01,500 --> 00:02:08,550 I could say that Z or z equals x plus y and z or z equals three. 27 00:02:09,030 --> 00:02:18,180 But if I said A equals hello and print a day that works. 28 00:02:18,180 --> 00:02:20,940 But it doesn't work like this. 29 00:02:21,180 --> 00:02:24,270 We can't add strings and integers. 30 00:02:24,960 --> 00:02:29,610 If you want to do that, we have to convert the integer to a string. 31 00:02:29,790 --> 00:02:30,600 And notice it. 32 00:02:30,600 --> 00:02:32,010 Concatenate them. 33 00:02:32,010 --> 00:02:34,080 It doesn't add them together. 34 00:02:34,500 --> 00:02:36,000 You can't add strings. 35 00:02:36,000 --> 00:02:38,580 Like you can add mathematical values. 36 00:02:38,850 --> 00:02:41,160 You concatenate them together. 37 00:02:41,340 --> 00:02:49,860 So if I said a plus string Y plus string x, that's what we're going to see play 21. 38 00:02:50,220 --> 00:02:52,920 That's different to X plus Y. 39 00:02:53,400 --> 00:03:02,550 So again, I could concatenate one x like that, which is different to x plus y or why plus x? 40 00:03:03,360 --> 00:03:04,920 This is concatenation. 41 00:03:05,160 --> 00:03:06,480 This is addition. 42 00:03:07,050 --> 00:03:08,940 So when we run our script. 43 00:03:12,670 --> 00:03:20,380 We need to tell Python that we're going to convert this integer value to three, four, five, six, 44 00:03:20,380 --> 00:03:23,500 seven, eight, nine and ten to a string. 45 00:03:23,950 --> 00:03:27,670 And then we're going to concatenate it with the word VLAN. 46 00:03:27,970 --> 00:03:34,450 We're going to encode this as an ASCII character and then press enter. 47 00:03:35,960 --> 00:03:44,960 So essentially what that's doing is sending that to the switch or that or this in the range 2 to 10. 48 00:03:48,070 --> 00:03:51,400 Then it's going to set the name of the VLAN. 49 00:03:51,580 --> 00:03:58,810 So name python underscore, vlan underscore and are the number in the range. 50 00:03:59,470 --> 00:04:06,640 Again, these are a string of values encoded as ASCII and sent to the switch. 51 00:04:07,900 --> 00:04:12,640 So on the switch at the moment we have a vlans 1 to 8. 52 00:04:13,090 --> 00:04:15,700 I'm going to remove those vlans. 53 00:04:17,920 --> 00:04:24,070 So at the moment we have VLAN one only as our ethernet vlan. 54 00:04:24,520 --> 00:04:28,270 But let's save our script and see what happens. 55 00:04:29,250 --> 00:04:29,550 So. 56 00:04:29,550 --> 00:04:30,780 Python three. 57 00:04:31,350 --> 00:04:32,700 Python three, three. 58 00:04:32,980 --> 00:04:33,800 P y. 59 00:04:34,170 --> 00:04:35,790 We asked for our username. 60 00:04:35,790 --> 00:04:37,230 Ask for our password. 61 00:04:37,410 --> 00:04:40,500 And as you can see it's toll leading to the switch. 62 00:04:41,730 --> 00:04:43,410 Show VLAN brief. 63 00:04:44,860 --> 00:04:49,390 You can see that VLANs 2 to 10 were created on the switch. 64 00:04:51,220 --> 00:04:53,260 So in other words, we've replaced. 65 00:04:55,910 --> 00:04:57,290 These lines of code. 66 00:05:01,580 --> 00:05:04,250 What's these three lines of code? 67 00:05:05,930 --> 00:05:12,850 And if we wanted to create more VLANs, we could simply change this one entry. 68 00:05:12,860 --> 00:05:14,870 And this is the power of automation. 69 00:05:15,080 --> 00:05:17,540 I'm simply changing that one entry. 70 00:05:18,530 --> 00:05:27,950 And if I run the script again, what we should see is that 20 VLANs are created on the switch show line 71 00:05:27,950 --> 00:05:31,040 shows us that the script is telnet to the switch. 72 00:05:31,370 --> 00:05:33,410 It's now configured the switch. 73 00:05:34,190 --> 00:05:36,170 So show vlan brief. 74 00:05:37,280 --> 00:05:42,050 Notice we have 20 ethernet vlans configured on the switch. 75 00:05:42,410 --> 00:05:46,490 And again I could simply edit. 76 00:05:47,240 --> 00:05:54,470 But that one entry make it 101 and run the script again. 77 00:05:57,110 --> 00:06:01,190 And what we should see is that 100 VLANs are created. 78 00:06:01,340 --> 00:06:05,870 Show line shows us that the script is telnet to the switch once again. 79 00:06:06,290 --> 00:06:10,640 Show VLAN shows us that VLANs are being created. 80 00:06:11,180 --> 00:06:13,160 At this point it's up to. 81 00:06:14,930 --> 00:06:16,460 52 villains. 82 00:06:18,660 --> 00:06:20,550 And let's use brief here. 83 00:06:22,360 --> 00:06:24,220 Now it's got up to. 84 00:06:27,520 --> 00:06:32,920 81 VLANs and as you can see, it's now completed running the script. 85 00:06:33,310 --> 00:06:35,730 So show VLAN brief again. 86 00:06:35,740 --> 00:06:41,410 We should see that 100 vlans are configured on the switch. 87 00:06:44,640 --> 00:06:55,230 So once again, by only making a small change in our script, we were able to configure many VLANs on 88 00:06:55,230 --> 00:06:56,190 the switch. 89 00:06:56,760 --> 00:07:03,060 Now you may be thinking, well, I could simply paste the VLAN information from an Excel spreadsheet 90 00:07:03,060 --> 00:07:05,700 or from notepad onto the switch. 91 00:07:06,000 --> 00:07:11,150 But again, don't lose sight of the fact that this is the beginning. 92 00:07:11,160 --> 00:07:16,170 We are going to iterate and improve our scripts as we go along. 93 00:07:16,530 --> 00:07:19,470 We're going to run loops within loops. 94 00:07:19,680 --> 00:07:26,280 We want to be able to configure many switches at the same time using one script. 95 00:07:26,490 --> 00:07:32,750 And later on, you could even make your scripts more intelligent where you use if statements. 96 00:07:32,760 --> 00:07:35,490 So if this is true, then do that. 97 00:07:35,610 --> 00:07:38,130 But if it's not true, then do something else. 98 00:07:38,370 --> 00:07:45,480 This is just the beginning, but hopefully it shows you that it's possible to automate Cisco Networks. 99 00:07:45,660 --> 00:07:50,190 And the same applies to other network devices using Python. 100 00:07:51,730 --> 00:07:54,100 We could, as an example, do another step here. 101 00:07:54,840 --> 00:07:59,730 Where we say Save the configuration once you've run the script. 102 00:08:03,100 --> 00:08:04,630 I'll run the script again. 103 00:08:07,250 --> 00:08:10,760 You can see it's telnet into the switch show line. 104 00:08:12,710 --> 00:08:15,650 This is the v t y line used by the script. 105 00:08:16,550 --> 00:08:23,630 Script is completed and as you can see here, the configuration of the switch was saved. 106 00:08:24,340 --> 00:08:32,409 So just think about how you can scale this to do configurations on many network devices rather than 107 00:08:32,409 --> 00:08:34,270 just a single switch. 108 00:08:34,870 --> 00:08:39,789 But hopefully already you can see the potential of using a network automation.