1 00:00:00,440 --> 00:00:05,600 So let's iterate our script and get it to do something else. 2 00:00:05,990 --> 00:00:10,220 Here's a new version of the script and I've made some changes. 3 00:00:10,460 --> 00:00:18,020 We don't need this sort of removed that from the script that says Enter your remote account. 4 00:00:18,530 --> 00:00:24,950 I've changed that back to enter your telnet username because that makes more sense. 5 00:00:25,580 --> 00:00:27,950 This is the same as before. 6 00:00:28,860 --> 00:00:37,410 We asking the user for a password and we opening up a file called my switches for each IP address in 7 00:00:37,410 --> 00:00:38,140 the file. 8 00:00:38,160 --> 00:00:45,810 We're going to strip any spaces and then we're going to print a get a running config from switch. 9 00:00:46,110 --> 00:00:48,420 So I've made a slight change here. 10 00:00:48,510 --> 00:00:55,890 Rather than making changes to the VLAN database of the switches. 11 00:00:56,370 --> 00:01:04,769 I'm going to use the command terminal length zero and show run to show the running configuration of 12 00:01:04,769 --> 00:01:05,790 the switches. 13 00:01:06,240 --> 00:01:15,360 So I've added a print statement here so that we know that the script is getting the running configuration 14 00:01:15,540 --> 00:01:16,590 from a switch. 15 00:01:16,650 --> 00:01:18,360 Was this IP address? 16 00:01:19,670 --> 00:01:27,080 So rather than saying configuring switch, it's now going to say getting running config from the switch. 17 00:01:27,850 --> 00:01:29,890 The rest of the code is very similar. 18 00:01:29,890 --> 00:01:32,650 So telnet into the switch. 19 00:01:33,420 --> 00:01:40,170 And a logging in is the same as the previous script, but we're not going to type confetti. 20 00:01:41,150 --> 00:01:46,010 We simply going to type terminal length zero and show run. 21 00:01:47,080 --> 00:01:48,790 The reason for doing that. 22 00:01:50,430 --> 00:01:55,670 Is when you telnet to a switch and you type show run by default. 23 00:01:55,680 --> 00:02:01,530 We only getting 24 lines of output and I have to press spacebar. 24 00:02:02,360 --> 00:02:04,820 To show each page of output. 25 00:02:05,000 --> 00:02:05,630 So show. 26 00:02:05,630 --> 00:02:06,290 Run. 27 00:02:09,060 --> 00:02:10,560 Press spacebar. 28 00:02:10,590 --> 00:02:16,410 Press spacebar again and so forth to see the rest of the configuration. 29 00:02:16,860 --> 00:02:21,990 And I need to do that multiple times to view the entire running configuration. 30 00:02:22,560 --> 00:02:30,480 So rather than doing that, I'm going to type terminal length, zero show run and that will show us 31 00:02:30,480 --> 00:02:33,960 the entire running configuration in one go. 32 00:02:36,960 --> 00:02:40,590 It's all set to the length back to 24 of the default. 33 00:02:41,630 --> 00:02:49,040 But when the script telnet to the switch, it's going to type terminal length, a zero show run, get 34 00:02:49,130 --> 00:02:54,410 the entire running configuration and then exit out of the Telnet session. 35 00:02:54,710 --> 00:03:02,720 But it's not only going to do that, we're going to take the output and save that to a file. 36 00:03:03,350 --> 00:03:06,710 So I've got this variable called save output. 37 00:03:07,160 --> 00:03:15,200 We're going to open up a file called Switch, followed by the hostname, which is essentially the IP 38 00:03:15,200 --> 00:03:16,670 address of the switch. 39 00:03:17,060 --> 00:03:20,030 And we're going to open the file as a reader. 40 00:03:20,030 --> 00:03:20,780 Right. 41 00:03:21,230 --> 00:03:22,370 So save output. 42 00:03:22,370 --> 00:03:23,180 Right. 43 00:03:23,990 --> 00:03:25,370 What are we going to write? 44 00:03:25,400 --> 00:03:31,790 We're going to write the output of the Telnet session in ASCII format. 45 00:03:32,120 --> 00:03:35,840 We're going to press carriage return and close the file. 46 00:03:36,140 --> 00:03:41,060 Now we can print the output to the screen, but we don't need to do that. 47 00:03:41,480 --> 00:03:45,230 We'll simply save the configuration to a file. 48 00:03:45,800 --> 00:03:50,450 So again, we are iterating our scripts and improving them. 49 00:03:50,720 --> 00:03:53,150 So let's copy that script. 50 00:03:55,550 --> 00:03:57,710 On the network automation container. 51 00:03:58,130 --> 00:04:02,570 Let's create a file called Back Up Configs dot py. 52 00:04:04,910 --> 00:04:08,390 And I'll save that script to the file. 53 00:04:09,650 --> 00:04:12,320 So again, cat back up configs. 54 00:04:12,620 --> 00:04:15,320 P There's our script. 55 00:04:16,550 --> 00:04:23,870 Now, before I run the script and notice that there are no files on the network automation container. 56 00:04:24,630 --> 00:04:31,800 That have the name switch followed by IP address. 57 00:04:32,980 --> 00:04:35,980 So after the moment, we only have our Python scripts. 58 00:04:36,280 --> 00:04:42,160 But if I type Python three back up configs, specify my username and password. 59 00:04:43,210 --> 00:04:51,850 The script is now telling, heading to the individual switches and is grabbing the running configuration. 60 00:04:52,580 --> 00:04:57,860 And is saving that as files under the network automation container. 61 00:04:58,340 --> 00:04:59,570 It's now completed. 62 00:04:59,570 --> 00:05:03,110 So Liz shows us these files. 63 00:05:03,650 --> 00:05:07,280 So more switch and let's look at 83. 64 00:05:07,980 --> 00:05:12,960 You can see that this is the running configuration of the switch. 65 00:05:14,310 --> 00:05:19,170 Space Bar shows us the configuration of the switch. 66 00:05:21,070 --> 00:05:24,850 Again, we could look at another switch, let's say switch six. 67 00:05:25,240 --> 00:05:28,870 There's the running configuration of the switch. 68 00:05:30,010 --> 00:05:35,020 Now you could edit the output and get rid of this kind of output. 69 00:05:35,230 --> 00:05:37,690 I'm not doing that at the moment. 70 00:05:38,110 --> 00:05:45,040 I simply want to back up the running configuration of the switches to the network automation container. 71 00:05:45,550 --> 00:05:48,580 Notice the power of network automation. 72 00:05:48,730 --> 00:05:56,710 I'll remove all files that were backed up and then I'll simply run the script again. 73 00:06:02,110 --> 00:06:08,980 So I'm running the script and getting it to telnet to the switches and getting it to save. 74 00:06:09,830 --> 00:06:13,670 The running configurations in this directory. 75 00:06:14,540 --> 00:06:17,750 Liz shows us the running configurations. 76 00:06:18,290 --> 00:06:20,150 So again, more switch. 77 00:06:20,570 --> 00:06:22,010 Let's look at 72. 78 00:06:23,030 --> 00:06:32,270 There's the configuration of switch one with IP address 1921681 2272. 79 00:06:35,960 --> 00:06:37,670 We could look at A2. 80 00:06:38,510 --> 00:06:41,180 There's the configuration of that switch. 81 00:06:42,310 --> 00:06:49,900 So it's not very difficult to use Python to back up the configurations of your network devices. 82 00:06:50,470 --> 00:06:54,790 The script is only 27 lines in length. 83 00:06:55,810 --> 00:07:00,010 It's a very short script but does something very useful. 84 00:07:00,220 --> 00:07:08,860 It telnet to network devices and retrieves the running configuration of those network devices.