1 00:00:14,360 --> 00:00:18,350 So I copied this script from the Python website. 2 00:00:18,920 --> 00:00:25,880 Now for this initial script, I'm going to do the editing directly in the network automation container. 3 00:00:26,510 --> 00:00:30,980 Ellis shows us that we have no files on the network automation container. 4 00:00:31,700 --> 00:00:41,090 I'll create a file called Python 31py and I'll paste the script from the Python website into the network 5 00:00:41,090 --> 00:00:42,320 automation container. 6 00:00:42,770 --> 00:00:50,990 One of the advantages of using Python over other programming languages is all of the modules and additional 7 00:00:50,990 --> 00:00:53,420 code that you can leverage. 8 00:00:53,600 --> 00:01:01,340 So rather than writing everything yourself, you can simply import a libraries such as the Telnet library 9 00:01:01,340 --> 00:01:02,990 into your Python script. 10 00:01:03,350 --> 00:01:06,170 So in this example, we're going to import get pass. 11 00:01:06,200 --> 00:01:14,090 We're going to leverage that here by asking the user to enter their password rather than storing the 12 00:01:14,090 --> 00:01:15,410 password in the script. 13 00:01:15,500 --> 00:01:22,010 We're going to ask the user to enter their password and then save that in what's called a verbal. 14 00:01:22,400 --> 00:01:26,690 A variable is a placeholder that can change in value. 15 00:01:26,900 --> 00:01:30,500 So as an example, I could enter a password of Cisco. 16 00:01:30,530 --> 00:01:38,180 You might enter a different password in your topology that's going to be stored in this verbal that's 17 00:01:38,180 --> 00:01:40,880 then going to be sent to the router. 18 00:01:40,910 --> 00:01:48,350 At this point, now, the first thing the script needs to know is what a device are we going to telnet 19 00:01:48,350 --> 00:01:49,160 to? 20 00:01:49,430 --> 00:01:56,870 In this example, it's configured to Telnet to itself, and that's not what we want to do in this network. 21 00:01:57,290 --> 00:02:04,610 So one of the first things we need to do is configure the router with an IP address and a username and 22 00:02:04,610 --> 00:02:11,900 password so that the network automation container can telnet to the router and configure it. 23 00:02:12,170 --> 00:02:19,250 So I'll give the router a name of router one and then on gigabit zero zero I'll configure an IP address 24 00:02:19,250 --> 00:02:22,760 of 1921681 2271. 25 00:02:23,330 --> 00:02:30,170 Now the reason for doing that is the network automation container has been allocated to this IP address 26 00:02:30,380 --> 00:02:34,070 in this subnet by the net cloud. 27 00:02:34,100 --> 00:02:36,140 This is a slash 24 subnet. 28 00:02:37,100 --> 00:02:40,370 Network automation containers using this IP address once again. 29 00:02:40,700 --> 00:02:47,450 So I'll configure the router with an IP address in the same subnet and I'll shut the interface. 30 00:02:48,020 --> 00:02:56,540 So hopefully at this point the network automation container should be able to ping this IP address of 31 00:02:56,660 --> 00:02:59,990 the broader because the interface is now come up. 32 00:03:01,240 --> 00:03:02,590 And there you go. 33 00:03:02,680 --> 00:03:09,520 The network automation container can ping the router, but at this point won't be able to telnet to 34 00:03:09,520 --> 00:03:10,240 the router. 35 00:03:10,510 --> 00:03:12,850 Notice the connection is refused. 36 00:03:13,480 --> 00:03:18,760 Before you automate, make sure that things work manually. 37 00:03:19,270 --> 00:03:24,490 So in other words, you should first test that things work and then you can automate. 38 00:03:24,760 --> 00:03:31,990 So as an example, one of the things we want to configure here is a username and a password of, let's 39 00:03:31,990 --> 00:03:33,370 say, Cisco. 40 00:03:33,820 --> 00:03:37,840 In these initial examples, I'm not trying to do everything securely. 41 00:03:38,050 --> 00:03:40,300 I simply want to get you started. 42 00:03:40,690 --> 00:03:44,230 In programming, we have this concept of iteration. 43 00:03:44,440 --> 00:03:50,080 Start something, then iterate or improve your scripts as you go along. 44 00:03:50,230 --> 00:03:57,370 And I think that's a very important skill just to get started and then improve things as you go along. 45 00:03:57,790 --> 00:04:04,840 So in this example, I want to simply configure things so that you can see an example of network automation. 46 00:04:06,590 --> 00:04:15,500 In this example on the V lines, I'm going to use the command login local so that the script gets prompted 47 00:04:15,500 --> 00:04:17,899 for a username and password. 48 00:04:19,060 --> 00:04:22,450 And I'm going to say transport input all. 49 00:04:22,720 --> 00:04:27,730 Now, you could tie that down to S-H and Telnet. 50 00:04:28,030 --> 00:04:31,720 But because this is a lab, I'm not too worried about that. 51 00:04:32,420 --> 00:04:34,580 So let's try and telnet again. 52 00:04:34,880 --> 00:04:39,350 We can see that we prompted for our username and then our password. 53 00:04:40,230 --> 00:04:45,300 So exit out of the rudder on back on the network automation container. 54 00:04:46,060 --> 00:04:48,220 I'll save the router's configuration. 55 00:04:50,090 --> 00:04:53,480 So let's ed the script by using Nano. 56 00:04:54,310 --> 00:05:00,550 The IP address that we're going to connect to is 1921681 2271. 57 00:05:01,330 --> 00:05:05,260 In other words, it's the IP address of the router. 58 00:05:06,980 --> 00:05:12,050 We then going to prompt the user to enter their username? 59 00:05:12,590 --> 00:05:19,820 So rather than prompting them for this, let's ask them to enter their telnet username. 60 00:05:21,000 --> 00:05:28,770 So what should happen is the script should prompt the user for their telnet username and their password 61 00:05:28,950 --> 00:05:34,380 and then it will telnet to the router based on this available. 62 00:05:34,740 --> 00:05:41,130 So we configuring a variable here with the IP address of the router. 63 00:05:41,130 --> 00:05:44,220 We are hard coding values in the script. 64 00:05:44,610 --> 00:05:48,210 Now programmers with a lot of experience will tell you that you shouldn't. 65 00:05:48,210 --> 00:05:49,920 Hard code values. 66 00:05:50,370 --> 00:05:51,450 That's true. 67 00:05:51,450 --> 00:05:54,750 But again, we'll get to that later. 68 00:05:55,050 --> 00:06:02,910 For now, we simply want to get started automating the network so we're going to hard code or that value 69 00:06:03,510 --> 00:06:07,470 and then later on we'll improve the scripts. 70 00:06:07,980 --> 00:06:12,270 So the host of variable is configured with this value. 71 00:06:12,360 --> 00:06:22,740 After this point in the script and then at this point the Telnet library is being used to Telnet to 72 00:06:23,130 --> 00:06:25,770 the host that we specified over here. 73 00:06:26,190 --> 00:06:32,370 We are leveraging the Telnet library that someone else wrote in our script. 74 00:06:32,640 --> 00:06:41,220 So rather than us developing a low level Telnet program, we're going to simply leverage or reuse the 75 00:06:41,220 --> 00:06:45,180 code that's then going to be stored in this variable. 76 00:06:45,420 --> 00:06:48,540 And then we're doing something at this point. 77 00:06:49,270 --> 00:06:52,660 The script's going to wait until it sees a log in. 78 00:06:53,450 --> 00:07:01,580 And that's actually incorrect because when we telnet to the router and I'll just telnet locally, what 79 00:07:01,580 --> 00:07:06,380 it should look for is the username, so I'll copy that. 80 00:07:08,060 --> 00:07:15,710 And I'll edit this line because what we're looking for is username and not login. 81 00:07:16,130 --> 00:07:20,810 When the script sees a username, it's going to write. 82 00:07:20,810 --> 00:07:22,550 In other words, it's going to send. 83 00:07:23,540 --> 00:07:29,180 The username that the user entered at this point to the router. 84 00:07:29,630 --> 00:07:35,240 It's using ASCII encoding here and then it's going to follow that with a carriage return. 85 00:07:35,990 --> 00:07:38,720 So in other words, it's going to type your username. 86 00:07:38,720 --> 00:07:40,310 David Press Enter. 87 00:07:40,670 --> 00:07:43,400 That's logically what's happening in the background. 88 00:07:43,820 --> 00:07:50,090 Then the script says if a password was configured, in other words, we entered a password at this point. 89 00:07:50,420 --> 00:07:53,420 It's going to wait until it sees password. 90 00:07:54,980 --> 00:07:58,550 That's correct when telnet to a Cisco router. 91 00:07:59,060 --> 00:08:04,130 So when it sees that it's going to write the password that was entered here. 92 00:08:04,730 --> 00:08:12,020 So in other words, it's going to send the password to the router in ASCII format and press enter again, 93 00:08:12,020 --> 00:08:15,500 enter the username and then the password. 94 00:08:15,500 --> 00:08:19,370 And hopefully at that point we'll be logged into the router. 95 00:08:19,790 --> 00:08:24,260 Now here it's trying to write a Linux command to the router. 96 00:08:24,470 --> 00:08:27,980 That command is not supported on a Cisco router. 97 00:08:28,190 --> 00:08:32,809 What we want to do is type enable so that we can go to enable mode. 98 00:08:33,350 --> 00:08:35,780 At this point, no password has been set. 99 00:08:36,260 --> 00:08:42,830 So going back to the console of the router, we need to set or create an enable password. 100 00:08:42,980 --> 00:08:45,860 And again, I'll simply set it to Cisco. 101 00:08:46,630 --> 00:08:53,680 This is why it's important to test things, make sure that they work manually before you automate. 102 00:08:53,950 --> 00:08:54,880 So again. 103 00:08:55,740 --> 00:09:03,540 And to use a name and to password at this point then type enable. 104 00:09:04,290 --> 00:09:06,510 So enable press enter. 105 00:09:06,990 --> 00:09:09,570 Now we need to enter the enable password. 106 00:09:09,810 --> 00:09:13,950 So what I'm going to do is copy that line and paste it here. 107 00:09:14,980 --> 00:09:17,770 And I'll send a password of Cisco to the broader. 108 00:09:19,910 --> 00:09:24,740 Now again, you don't want to embed passwords in your scripts like this. 109 00:09:25,190 --> 00:09:27,740 Later on, we'll improve the script. 110 00:09:27,770 --> 00:09:31,400 But to get started will embed the password in the script. 111 00:09:32,230 --> 00:09:39,040 So we're going to enter a password of Cisco and that will take us to privilege mode or enable mode on 112 00:09:39,040 --> 00:09:39,820 the broader. 113 00:09:40,580 --> 00:09:44,100 Now, once we there, we want to do something. 114 00:09:44,120 --> 00:09:52,010 So as an example, we want to go to global configuration mode and then we want to create a loopback 115 00:09:52,010 --> 00:09:52,850 interface. 116 00:09:53,270 --> 00:09:56,120 So let's say loopback interface zero. 117 00:09:57,650 --> 00:10:06,200 And then we want to configure an IP address on the loopback interface, something like that. 118 00:10:06,920 --> 00:10:08,840 And then we want to exit. 119 00:10:08,960 --> 00:10:16,370 Or in our example, before we exit the Telnet session, we want to type end. 120 00:10:17,540 --> 00:10:21,560 So that we go back to privilege mode. 121 00:10:22,540 --> 00:10:25,360 And then we exit out of the Telnet session. 122 00:10:26,600 --> 00:10:36,410 So again when we telnet to the router username password enable password quantity interface loopback 123 00:10:36,410 --> 00:10:40,940 zero IP address creator the IP address. 124 00:10:42,550 --> 00:10:47,470 On the rudder type end and exit out of the rotor. 125 00:10:47,530 --> 00:10:50,650 That's the logic of what our script does. 126 00:10:50,920 --> 00:10:53,230 So I'm back on the console of the rudder. 127 00:10:53,620 --> 00:10:56,920 What I'll do is I'll remove the loopback interface. 128 00:10:57,850 --> 00:11:00,310 So show IP interface brief. 129 00:11:01,270 --> 00:11:02,320 Do that again. 130 00:11:02,350 --> 00:11:05,860 No loopback is currently configured on the router.