1 00:00:00,490 --> 00:00:00,820 Now. 2 00:00:00,820 --> 00:00:02,830 Previously we created a script. 3 00:00:02,860 --> 00:00:05,230 Python 32. p y. 4 00:00:06,490 --> 00:00:15,250 If I look at the contents of that file, we can see that the network automation container telnet to 5 00:00:15,250 --> 00:00:19,270 a switch that's hardcoded in the script. 6 00:00:20,000 --> 00:00:24,380 And then creates some VLANs on that switch. 7 00:00:26,110 --> 00:00:35,050 So using this as our basis, let's now extend the script to use a file of IP addresses. 8 00:00:35,290 --> 00:00:45,550 So rather than hard coding an IP address in the script, let's open a file called My Switches. 9 00:00:46,340 --> 00:00:51,560 What you'll notice is the script is very similar to what we created previously. 10 00:00:53,310 --> 00:00:55,260 This code is essentially the same. 11 00:00:55,470 --> 00:01:03,000 Except here we've got a hardcoded IP address here have simply left host equal to local host and notice 12 00:01:03,000 --> 00:01:11,040 at a line eight we change things because we're going to open up a file called my switches and only later 13 00:01:11,040 --> 00:01:14,160 are we going to telnet to the device. 14 00:01:15,440 --> 00:01:18,770 Unlike what we did in our original script. 15 00:01:19,580 --> 00:01:27,440 The reason for doing this is we want to open up a file called My Switches, and then for each line or 16 00:01:27,440 --> 00:01:36,110 entry in that file we going to telnet to the switch and create the relevant VLANs. 17 00:01:36,770 --> 00:01:44,570 So rather than only telnet into an individual switch and making changes on that individual switch, 18 00:01:44,570 --> 00:01:48,980 we're going to telnet to multiple switches and make changes on them. 19 00:01:50,800 --> 00:01:55,210 So the first thing we need to do is create a file called My Switches. 20 00:01:56,330 --> 00:02:02,000 You could call this anything, but that's the name of the file in my script. 21 00:02:03,690 --> 00:02:07,590 What this is going to contain is a list of IP addresses. 22 00:02:08,009 --> 00:02:10,410 So I'll copy and paste that a few times. 23 00:02:12,350 --> 00:02:22,790 This is simply the IP addresses of devices that we want to make changes to or changes on, make sure 24 00:02:22,790 --> 00:02:24,410 that you don't have any spaces. 25 00:02:25,440 --> 00:02:30,030 Later on, I'll show you how to strip lines in spaces. 26 00:02:30,090 --> 00:02:37,170 But for now, try and remove all of the additional spaces and carriage returns in your file. 27 00:02:38,880 --> 00:02:40,210 I'll save that file. 28 00:02:40,230 --> 00:02:41,760 So, Kat, my switches. 29 00:02:41,910 --> 00:02:43,950 That's what my switches looks like. 30 00:02:45,300 --> 00:02:48,480 Let's create another test. 31 00:02:48,480 --> 00:02:50,190 Python script. 32 00:02:50,580 --> 00:02:53,550 So Python file p y. 33 00:02:54,500 --> 00:02:56,240 Let's copy this entry. 34 00:02:56,270 --> 00:02:57,950 This opens a file. 35 00:02:58,220 --> 00:03:03,140 So we're going to open up my switches, and then we're going to do something. 36 00:03:03,960 --> 00:03:09,750 I'm going to say four IP address in F so for each a line in the script. 37 00:03:12,030 --> 00:03:13,140 Four spaces. 38 00:03:14,130 --> 00:03:17,700 Print and lets print the IP address. 39 00:03:18,000 --> 00:03:19,770 So it's a fairly simple script. 40 00:03:20,490 --> 00:03:30,930 All it's going to do is open up a file and print the lines in the file and there you go. 41 00:03:31,440 --> 00:03:36,510 Notice it's printed the entries in the my switches file. 42 00:03:38,790 --> 00:03:42,840 So again, the python filed on p script. 43 00:03:43,940 --> 00:03:47,750 Simply opens a file that you specify. 44 00:03:47,900 --> 00:03:49,940 In this case, it's my switches. 45 00:03:49,940 --> 00:03:54,770 And then for each align in the file, it prints the line. 46 00:03:55,130 --> 00:03:56,690 Now you could call this anything. 47 00:03:56,690 --> 00:03:59,170 You could call this a line as an example. 48 00:03:59,180 --> 00:04:05,000 So for each line in the file f print the line. 49 00:04:07,180 --> 00:04:08,230 And there you go. 50 00:04:08,260 --> 00:04:15,640 The result is the same, but because we have IP addresses in our file. 51 00:04:17,700 --> 00:04:19,860 I'm going to use IP. 52 00:04:23,520 --> 00:04:26,520 And again, notice the result is the same. 53 00:04:27,240 --> 00:04:31,050 So what we've got here is a script that opens a file. 54 00:04:31,290 --> 00:04:36,060 It strips any trailing spaces, so that removes an error. 55 00:04:36,060 --> 00:04:45,420 If you have an IP address of 121681 2282 space that removes or strips any spaces after the IP address. 56 00:04:45,450 --> 00:04:49,950 To ensure that we only have the IP address and not IP address space. 57 00:04:50,370 --> 00:04:56,820 The script then prints configuring and the IP address from the file. 58 00:04:57,330 --> 00:05:03,300 Now this is a little bit redundant, but I'm going to keep it in the script to keep it consistent with 59 00:05:03,300 --> 00:05:04,560 our previous scripts. 60 00:05:08,140 --> 00:05:09,700 In our previous scripts. 61 00:05:13,160 --> 00:05:16,610 We had host set to a hardcoded IP address. 62 00:05:17,280 --> 00:05:20,150 Here I have left it to the default of a local host. 63 00:05:20,750 --> 00:05:28,220 However, after this point, the host of variable is going to be replaced with the IP address of the 64 00:05:28,220 --> 00:05:29,450 switch in the file. 65 00:05:30,110 --> 00:05:34,370 This is now consistent to what we had previously in our script. 66 00:05:35,130 --> 00:05:41,550 But notice the host verbal is set to the IP address of the switch in the file. 67 00:05:42,330 --> 00:05:49,410 So what you'll notice is this is a very similar to what we have in our original script. 68 00:05:52,260 --> 00:05:58,320 And then here we are going to global configuration mode and creating our VLANs. 69 00:05:58,440 --> 00:06:06,630 We don't have to go to enable mode because when we telnet to the switches, we've configured them. 70 00:06:09,670 --> 00:06:13,150 To take us immediately to privilege mode. 71 00:06:13,180 --> 00:06:15,250 Notice I'm in privilege mode already. 72 00:06:15,820 --> 00:06:20,290 That's because the username David has. 73 00:06:21,930 --> 00:06:25,560 Level 15 privileges, as you can see there. 74 00:06:29,430 --> 00:06:32,640 So all we going to do is create some VLANs. 75 00:06:34,800 --> 00:06:37,080 And then we're going to print the results. 76 00:06:37,710 --> 00:06:39,960 Now, we'll optimize this in a moment. 77 00:06:41,150 --> 00:06:49,130 We want to improve the script by running loops within loops, but initially will start with a single 78 00:06:49,130 --> 00:06:49,670 loop. 79 00:06:50,840 --> 00:06:53,270 Configuring Individual VLANs. 80 00:06:54,250 --> 00:06:58,330 So let's create a script called Python 35. p y. 81 00:06:59,170 --> 00:07:04,960 I'll paste that script into the network automation container and save the script. 82 00:07:05,350 --> 00:07:09,610 So again, more Python 35. p y. 83 00:07:10,450 --> 00:07:13,270 That's the script that we've just copied. 84 00:07:14,170 --> 00:07:16,150 Before I run the script. 85 00:07:17,080 --> 00:07:24,130 Notice that switch to only has the default VLANs. 86 00:07:25,740 --> 00:07:27,960 I'll do a debug telnet on the switch. 87 00:07:29,750 --> 00:07:37,850 So we can see connections to the switch on switch for show vlan brief. 88 00:07:38,300 --> 00:07:41,210 We only have the default vlans. 89 00:07:41,810 --> 00:07:47,180 Now you could do something similar on all the other switches, but I'm not going to do that. 90 00:07:47,210 --> 00:07:48,800 They all are the same. 91 00:07:49,310 --> 00:07:51,830 Show vlan brief on switch six. 92 00:07:51,950 --> 00:07:55,580 Notice only has the default vlans. 93 00:07:56,030 --> 00:07:58,160 Switch one has more vlans. 94 00:07:58,160 --> 00:08:00,980 Because we configured those vlans previously. 95 00:08:01,190 --> 00:08:05,720 So show vlan brief shows us that we have a lot of vlans on the switch. 96 00:08:06,500 --> 00:08:09,800 So I'm going to say no vlan one two, 100. 97 00:08:12,200 --> 00:08:15,530 And I should say too because vlan one cannot be deleted. 98 00:08:15,650 --> 00:08:17,480 So show vlan brief. 99 00:08:18,270 --> 00:08:21,600 We now only have the default VLANs.