1 00:00:05,610 --> 00:00:05,850 Okay. 2 00:00:05,850 --> 00:00:09,810 So I'm going to copy this bit of Python code. 3 00:00:12,070 --> 00:00:14,350 Go back to the Nexus box. 4 00:00:14,380 --> 00:00:16,870 At the moment I'm not running Python. 5 00:00:17,470 --> 00:00:21,730 I can run standard Cisco iOS commands such as show IP interface brief. 6 00:00:24,520 --> 00:00:25,750 All show version. 7 00:00:25,750 --> 00:00:32,110 So these are standard commands, show, run, etc. But what I'm going to do now is run Python. 8 00:00:32,350 --> 00:00:34,540 This is on box python. 9 00:00:35,490 --> 00:00:39,720 I'm running Python directly on this Nexus box. 10 00:00:41,600 --> 00:00:43,490 And I'm going to teach you about a python now. 11 00:00:43,910 --> 00:00:47,900 The first statement we're going to use is from Sealy import star. 12 00:00:48,620 --> 00:00:54,380 So from cli import, store or import everything. 13 00:00:54,890 --> 00:01:02,390 To be able to use cli commands from within python running on this nexus box, we need to import cli. 14 00:01:03,190 --> 00:01:07,630 We need to talk to that command to allow us to basically run any CLI command. 15 00:01:08,610 --> 00:01:11,030 This is called a variable CMD one. 16 00:01:11,040 --> 00:01:13,130 You could call this anything you wanted to. 17 00:01:13,440 --> 00:01:18,560 The command that we're going to run is show IP interface, brief JSON pretty. 18 00:01:18,570 --> 00:01:22,530 So we basically using the command that I previously demonstrated. 19 00:01:22,770 --> 00:01:25,770 So I'll quit out of Python just to show you that again. 20 00:01:26,310 --> 00:01:28,230 Show IP interface brief. 21 00:01:30,130 --> 00:01:31,180 Jason. 22 00:01:31,990 --> 00:01:32,800 Pretty. 23 00:01:33,880 --> 00:01:36,340 That's me typing the command manually. 24 00:01:36,340 --> 00:01:41,140 And here's our output once again, json array. 25 00:01:41,530 --> 00:01:44,560 But I'm going to run python on box. 26 00:01:45,330 --> 00:01:46,500 The version of Python. 27 00:01:46,500 --> 00:01:48,870 He has 2.7 3011. 28 00:01:49,910 --> 00:01:50,840 Now for the real world. 29 00:01:50,840 --> 00:01:54,100 You really want to learn Python three if you're new to Python. 30 00:01:54,110 --> 00:02:00,650 But here's a real world example of why sometimes it's well worth knowing about Python 2.7 because you'll 31 00:02:00,650 --> 00:02:01,880 still encounter it. 32 00:02:02,480 --> 00:02:07,280 Devices like this nexus are running Python 2.7, not Python three. 33 00:02:07,550 --> 00:02:12,020 So once again, I need to type from CLI, import everything. 34 00:02:12,700 --> 00:02:20,350 And then I'm going to paste this statement that says this command is going to be stored in this variable. 35 00:02:20,350 --> 00:02:21,970 So if I type CMD one. 36 00:02:22,870 --> 00:02:25,870 That's the command that's stored in this variable. 37 00:02:25,930 --> 00:02:28,690 I explained Python theory a little bit later in the course. 38 00:02:28,690 --> 00:02:34,300 If you want to learn a bit about Python theory, such as the fact that a variable is essentially a placeholder 39 00:02:34,300 --> 00:02:37,480 in memory on the device, then continue watching again. 40 00:02:37,480 --> 00:02:39,610 You don't need to learn Python for the exam. 41 00:02:40,390 --> 00:02:41,380 CCNA exam. 42 00:02:41,380 --> 00:02:43,090 Definite exam you would need to. 43 00:02:43,920 --> 00:02:49,860 So I'm going to store the output of that command in another variable. 44 00:02:50,130 --> 00:02:57,330 Basically, the output variable stores, the output of this command, which I'm running using the CLI 45 00:02:57,360 --> 00:02:58,080 function. 46 00:02:58,470 --> 00:03:02,190 So if I look at output one now, there's the output. 47 00:03:02,670 --> 00:03:06,000 There's the information stored in that variable. 48 00:03:06,000 --> 00:03:10,470 But it's not very easy to read some of the type print output one. 49 00:03:12,280 --> 00:03:13,330 And there you go. 50 00:03:14,710 --> 00:03:22,780 There's the output of that show command in JSON formatting running within Python. 51 00:03:23,020 --> 00:03:27,130 Now if you're new to Python, you might say, but David, I could just type the command manually. 52 00:03:27,160 --> 00:03:28,090 Sure you could. 53 00:03:28,090 --> 00:03:29,950 But I'm showing you this step by step. 54 00:03:29,980 --> 00:03:35,920 In a moment, I'm going to show you how to run a Python script from my local computer that searches 55 00:03:35,920 --> 00:03:42,820 into this device and runs one or two commands and then extracts information from that device and shows 56 00:03:42,820 --> 00:03:44,110 it on my local computer. 57 00:03:44,230 --> 00:03:50,620 So rather than me having to manually SSH log into the device type of command, see the output and then 58 00:03:50,620 --> 00:03:53,860 interpret it, I can get a Python script to automatically do that. 59 00:03:54,040 --> 00:03:59,260 In this example, I'm doing it for one device, but remember, I could do this with 100 devices or 500 60 00:03:59,260 --> 00:03:59,980 devices. 61 00:04:00,010 --> 00:04:04,650 Automation and network programmability make a lot of sense when you have many devices. 62 00:04:04,660 --> 00:04:10,300 Not necessarily always when you only have one device, but you need to work in building blocks. 63 00:04:10,630 --> 00:04:17,709 So basically this little script of four lines and I could have made it short if I wanted to, but I 64 00:04:17,709 --> 00:04:18,850 don't want to make it complex. 65 00:04:18,850 --> 00:04:20,560 I want to make it easy to read. 66 00:04:21,100 --> 00:04:29,770 Short script of four lines has given me the output of show IP interface brief using JSON pretty format. 67 00:04:30,190 --> 00:04:38,590 What we can do now is extract specific information from the table, interface dictionary and the row 68 00:04:38,590 --> 00:04:39,760 interface dictionary. 69 00:04:39,910 --> 00:04:41,920 This is what's called a dictionary. 70 00:04:42,010 --> 00:04:44,530 We can query this information. 71 00:04:45,040 --> 00:04:52,360 So as an example, I could query the IP address of a specific interface if I wanted to. 72 00:04:52,660 --> 00:04:56,650 So let's do that by updating our script. 73 00:04:57,340 --> 00:05:06,340 So this information is the same, but what I'm going to do now is load that information using this command 74 00:05:06,610 --> 00:05:11,410 so that Python knows that this information is in JSON format at the moment. 75 00:05:11,410 --> 00:05:12,700 It's currently a string. 76 00:05:13,800 --> 00:05:22,860 So if I use the type command and look at output one, it's a string and we can't query that data format 77 00:05:22,860 --> 00:05:24,480 as easily as JSON. 78 00:05:24,660 --> 00:05:31,020 So it's going to make much more sense to change this into a dictionary using JSON formatting so that 79 00:05:31,020 --> 00:05:33,460 we can query the information much more easily. 80 00:05:33,480 --> 00:05:35,100 Remember, the capture. 81 00:05:35,250 --> 00:05:37,250 Capture is a way to stop machines. 82 00:05:37,260 --> 00:05:39,680 It's easy for humans, but not easy for machines. 83 00:05:39,690 --> 00:05:42,960 Here we want to put it in a format that machines understand. 84 00:05:43,440 --> 00:05:47,880 And again, if you know Python, you could do this more simply than I am. 85 00:05:49,480 --> 00:05:52,780 It's complaining about my script, and that's because I forgot this command. 86 00:05:53,960 --> 00:05:58,460 Previously I had this as output one and I actually changed the script. 87 00:05:59,610 --> 00:06:00,540 So that's bad. 88 00:06:00,540 --> 00:06:01,830 So let's do that again. 89 00:06:01,950 --> 00:06:06,210 So to make sure that you're not confused, I'll simply start this from the beginning again. 90 00:06:06,210 --> 00:06:09,910 So I'll quit out of Python and let's start again. 91 00:06:09,930 --> 00:06:15,540 So Python 2.7, these commands you've already seen. 92 00:06:15,870 --> 00:06:23,310 So let's copy those commands and I'll paste them into Python, which is running on that Nexus device. 93 00:06:24,390 --> 00:06:32,640 This statement runs this command using the CLI function and stores it in JSON data rather than in output 94 00:06:32,640 --> 00:06:35,190 one, which is the name I used previously. 95 00:06:35,430 --> 00:06:41,670 So that JSON data now contains the information. 96 00:06:41,670 --> 00:06:47,510 But the problem with that is it's a string, so notice type string. 97 00:06:47,520 --> 00:06:54,060 So what I want to do is I want to load that data using JSON loads. 98 00:06:54,060 --> 00:07:04,050 Some are loading this information using this function and I'm storing it in this variable now JSON final. 99 00:07:05,200 --> 00:07:06,430 Has that information. 100 00:07:07,030 --> 00:07:12,310 I actually wanted to use type here so that we can see what type of this is. 101 00:07:12,820 --> 00:07:15,550 What you can see is it's now a dictionary. 102 00:07:16,000 --> 00:07:18,490 Dictionaries in Python use key value pairs. 103 00:07:18,490 --> 00:07:25,840 So I can query very easily within the dictionary for specific piece of information rather than trying 104 00:07:25,840 --> 00:07:31,030 to pass or go through a string and look for specific characters and then find the answer. 105 00:07:31,210 --> 00:07:34,520 That's very difficult to do for a machine. 106 00:07:34,540 --> 00:07:36,340 Very easy for us as humans. 107 00:07:36,460 --> 00:07:39,310 But once again, it's the idea of show version. 108 00:07:39,310 --> 00:07:40,330 It's just text. 109 00:07:40,360 --> 00:07:42,970 Easy for a human to read that, not for a machine. 110 00:07:43,030 --> 00:07:47,110 If I put that into JSON format, it's much easier to find information. 111 00:07:47,800 --> 00:07:58,600 So as an example, what I can do now is print the prefix of the first interface. 112 00:07:59,380 --> 00:08:06,880 So going up through this information, not easy to reach here, but notice the first interface is VLAN 113 00:08:06,880 --> 00:08:07,690 100. 114 00:08:07,960 --> 00:08:10,510 First IP address is this. 115 00:08:11,510 --> 00:08:13,970 So if I paste that command in. 116 00:08:15,130 --> 00:08:22,870 We've just retrieved the IP address or prefix of the first interface. 117 00:08:23,620 --> 00:08:27,310 Again, that's not very easy, but don't lose sight of the fact that I'm going to show you a script 118 00:08:27,310 --> 00:08:29,640 in a moment that automates all of this. 119 00:08:29,650 --> 00:08:31,270 Just trying to show you step by step. 120 00:08:32,500 --> 00:08:37,929 So again, just to make sure that you understand the script, we are importing clay so we can use CLI 121 00:08:37,960 --> 00:08:39,429 commands within Python. 122 00:08:40,000 --> 00:08:48,940 We importing JSON so that we can use this command which allows us to take the string data and basically 123 00:08:48,940 --> 00:08:50,260 convert it to a dictionary. 124 00:08:50,260 --> 00:08:51,940 So we have JSON formatting. 125 00:08:53,550 --> 00:08:56,640 This is the command that we ran on the switch. 126 00:08:56,820 --> 00:08:59,520 Show IP interface, brief JSON pretty. 127 00:08:59,730 --> 00:09:04,920 And then I've now queried that data, which is stored in what's called a dictionary. 128 00:09:05,880 --> 00:09:12,660 A dictionary in Python is an unordered key value pair, so I can simply look for a value in the dictionary 129 00:09:12,660 --> 00:09:15,680 and easily retrieve a piece of information. 130 00:09:15,690 --> 00:09:24,120 So again, I've retrieved the IP address of the first interface starting at zero. 131 00:09:24,150 --> 00:09:29,220 Now if I want the IP address of the second interface, I could change that to one third interface. 132 00:09:33,420 --> 00:09:34,620 Fourth interface. 133 00:09:34,650 --> 00:09:36,030 Now I'm doing that manually. 134 00:09:36,030 --> 00:09:37,950 That's not the idea with automation. 135 00:09:38,340 --> 00:09:40,350 The idea with automation is to automate this. 136 00:09:40,470 --> 00:09:44,780 But I'm building or iterating is the term we use in programming. 137 00:09:44,790 --> 00:09:50,280 I'm iterating the process, improving what I'm doing, and hopefully teaching you along the way. 138 00:09:50,310 --> 00:09:54,930 Again, you don't have to learn Python for the CCNA exam, but you need a node for the dev net exam. 139 00:09:54,930 --> 00:09:57,090 So if you're struggling with this stuff, don't worry too much. 140 00:09:57,300 --> 00:09:58,860 But notice how cool this is. 141 00:09:58,860 --> 00:10:06,480 We are running Python on a Cisco Nexus device, so I simply started Python on the Nexus device. 142 00:10:06,480 --> 00:10:09,630 Once again, I'll exit out of Python. 143 00:10:10,290 --> 00:10:17,730 I simply typed Python to start Python on the Nexus device and now I'm running Python scripts directly 144 00:10:17,730 --> 00:10:19,080 on the Nexus device. 145 00:10:19,260 --> 00:10:24,510 I'm going to show you some other videos in this course showing you how to do sort of a Linux based commands 146 00:10:24,510 --> 00:10:27,000 and Python on XY. 147 00:10:27,030 --> 00:10:28,110 This is Nexus. 148 00:10:28,440 --> 00:10:33,930 iOS X also supports Python, so you can run Python directly on an iOS X device. 149 00:10:34,080 --> 00:10:35,550 Classic or monolithic. 150 00:10:35,550 --> 00:10:40,200 iOS doesn't support Python, but you can still use Linux commands on classic iOS. 151 00:10:40,230 --> 00:10:41,840 Notice where the world is going. 152 00:10:41,850 --> 00:10:46,980 Notice how it's important for you to learn Python Network Automation, Linux. 153 00:10:47,400 --> 00:10:50,940 These kind of new skills is where the industry is moving.