Transcript for:
Introduction to Network Automation

foreign you're going to be learning the fundamentals of network automation which is a completely new way of doing our day-to-day jobs as Network Engineers where instead of going from one device to another to another and configuring them individually we can do it through a program like a Python program hi my name is Kevin Wallace and this video is a replay of a live event that we did back on Cyber Monday 2022 where we covered at Network Automation and it's a long one so you might want to get comfortable for this one get ready to take a lot of notes and get ready to do some Hands-On practice with network Automation and we'll Begin by taking a look at a few of the network automation certifications out there and I'll give you my recommendation then we'll give an overview of software defined networking or sdn see what that's all about talk about rest conf and netconf how we can do data modeling for a network device and then we'll do lots of demos we'll get started with a utility called Postman which is going to allow us to send and commands to a network device using HTTP verbs something we're going to refer to as rest cough we'll see how to do that using a Cisco CSR 1000v out at the Cisco devnet sandbox and in the real world after you know what you want to send via rest conf instead of doing it all through Postman you probably want to do it through a program so then we'll get into python you'll learn how to do some basic programming in Python and we will write a program that will go out and make a configuration change on a CSR 1000v that I have running locally and at the end of this video which I think is about an hour and a half long you're going to come away with a great introduction to network Automation and I've got some resources for you to download as well I mentioned that this video is a replay of an event that I did on Cyber Monday 2022 and in that event I let attendees download a zip file and you can do the same thing just go to kwtrain.com CM for Cyber Monday cm22 2 and that zip file is going to contain the Python program that I write as part of the demo and one of the network automation certifications we'll discuss is Cisco's devnet associate well this download also gives you a complete Cisco devnet associate a practice exam it's a PDF that you can print out it's got detailed explanations for the questions and I've got another PDF ebook for you and it's called four ways to get Hands-On practice for your Cisco exam and also visit a few different sites in this video and I'm giving you those links in the resource links section of this download all right I hope you're excited and ready to get started as I introduce you to network automation as you're deciding what your next steps are as you're planning your vision for next year let's think about what certification or certifications you might consider in network Automation and here's the Big Challenge unlike cloud and unlike just basic networking and unlike cyber security there's not really a industry recognized vendor neutral Network automation certification they tend to be very vendor specific so that's the challenge if you happen to run a juniper shop for example they have the devops associate cert and they have the devops specialist search again that's for the Juniper environment the number two networking vendor right now but if you're joining me you're probably you're probably more of a more of a Cisco shop maybe a mixture but I would personally focus on Cisco Network automation certifications and I know on Friday I said well depending on where you are in networking you might want to do this one here's another great certification you could take and for cloud oh yeah you can take this one or you can take this one or maybe take this one first this one's super clear here's my recommendation you ready for this if you don't currently have the Cisco devnet associate I think you should get the Cisco devnet associate sometime next year I think that is a clear clear best next move for you in that one pillar of our I.T career Parthenon if you've already got your devnet associate congratulations you may want to look at devnet professional tremendous demand for that right now and if if development is your life if you've got coding in your blood there's even an expert level you can get your Cisco certified devnet expert certification I'm personally never going to go for that one but more power to you if if that just resonates with you it's like we were saying on Friday if that's for you you're going to know it you're gonna you're gonna it's gonna call to you it's not calling to me I'm going to be happy with the devnet associated and just be conversant in that world so those are some certifications to think about again the takeaway is if you don't have the devnet associate from Cisco I think that's your move for next year I think that's going to be my move for next year as well now let's get into what network automation is all about and I'm going to use a few terms synonymously when I say Network automation or software-defined networking or network programmability I'm probably talking about the same thing kind of goes by some different names but let's talk about sdn software-defined networking and what makes it so different than what we have what we've had in the past traditionally the networks that I grew up with I guess we could say were routers switches maybe firewalls and to configure them I would go in with my console cable or I would telnet or secure shell into those devices and I would one at a time configure those and inside of each of those devices there are three different planes of operation if you want to put this in your notes there is the data plane and the data plane is concerned with getting packets for routers or frames of switches it's concerned it's concerned with getting data in one port and as quickly as possible sending it out the appropriate egress port and up at the control plane that's where we run algorithms if we have a router for example and we're running the uh the ospf routing protocol then we're going to be running the dijkstra algorithm in that control plane if we have a switch we're going to be running spanning tree protocol in that control plane and then finally the management plane that's how you and I as a network administrator's communicate with that device maybe we secure shell in we're connecting via the management plane and this is the way I've done networking for literally decades like three decades and the term given to this is a distributed control plane but notice what I just introduced to the topology I brought in an SD and a software-defined network controller and those control planes that used to reside in the devices they went up and they now live in the uh in the sdn controller so now it can be in charge of running all those algorithms and sending your appropriate configuration instructions out now please hear me on this it's not always that way it just depends on what equipment you're using what your application is sometimes the control plane stays where it was it stays in the router it stays in the switch so please don't chat in oh no Kevin there's a there's an exception yeah there's lots of exceptions but I'm just saying this is an option you might see a situation where the control plane actually is residing in the sdn controller if you do that's called a centralized control plane and Cisco I love the term they use they talk about intent based networking instead of me going to a router and say I want to configure this for quality of service let's create a class map and a policy map and apply that with the service policy command now we just express our intent we say yeah I'd like to prioritize video I'd like to de-prioritize Xbox traffic as an example and that's the kind of thing that we can do because when I set up quality of service on a switch or a router it's different commands totally different command set but I've got the same intent I still want to prioritize the same traffic but it's different commands I don't have to worry about the individual commands I express my intent to the controller and it sends appropriate instructions down to the routers and switches and not just Cisco routers and switches anybody that wants to to uh to conform to this Yang data model that we're going to be talking about later we can configure Juniper and HP and the list just goes on and on whatever vendor supports it we can configure their routers even if we don't even know the syntax used on those routers by expressing our intent now the question is what kind of what kind of commands are we sending from that controller down to those devices that we're configuring and it's uh we oftentimes call those south bound interfaces what they really are are apis you might have heard that term a lot an API that's an application programming interface that's the way that one piece of software talks to another piece of software and when we draw this out we typically draw the devices that we're configuring below the controller in other words south of the controller if you think of a compass so we call those south bound application programming interfaces or southbound apis or for short just sbis for sapon interfaces those those interfaces are ways that we're programmatically sending configuration information down to the device is to be configured some examples could be traditional things I mean we've been using simple Network management protocols as long as I can remember I used it back in the 1990s secure shell heaven forbid telnet let's not use that one but that's that is one we're today probably going to be used something something like netconf or rest conf we're actually going to set up rest comp today you're going to see it in action and it is impressive I cannot wait to show you that but that's the language of love if you will or the languages of love that's going to be spoken between the controller and the devices to be configured but I said we're going to do intent based networking we're going to express our intent what we want to configure we're going to express that intent to a controller how are we going to do that well we're going to do that through an application now please hear me on this that application might be something we write kind of like we're going to do today or it could be built in up didn't mean to hit the back arrow there it could be actually built in to the sdn controller itself you see the control of that Cisco often uses is called Cisco DNA Center and Cisco DNA Center if you go to its web interface it's going to in fact I was trying to remember I think I might have given you a link to that in uh in Friday's in Friday's show notes but um DNA center it's you can go to the web interface and it's got a lot of applications built in right there so this application even though I'm showing it is being separate I'm showing it being above or north of the controller yeah it could be part of the controller but somehow we're going to use some application whether it's on the controller whether it's a Python program that we write we're going to send instructions down to that sdn controller and because the applications are typically thought of as being above or north of the controller those are called nbis or Northbound interfaces now what kind of language are we speaking between the applications and the controller well it can vary there there are options what we're going to focus on one of the most popular options out there is called rest apis if you want to drop this in your notes rest is representational State transfer this is an example of an NBI a Northbound interface think about going to a web server if you go to a web server and you send a request to get content from that web server or maybe you're putting in your credit card information and your your putting information on that web server you're using a series of HTTP verbs to do that that's what rest is doing rest is using verbs that HTTP uses like get and put and post and we'll see lots of others today but we're using HTTP verbs to send our intent down to that controller now I say down to the controller please see around this one as well we don't have to have the controller in the mix I could have it in fact what we're going to do today is we're going to have an application that talks directly to the router the controller is going to make things a bit more scalable for us but yeah I could I could have my application send these rest apis down to a router that that understands those rest apis and when we send our instructions down in the inside of those are using those HTTP verbs the data that we're sending and that data might say this interface is up here's this IP address here's a subnet mask here's its description here we're setting up ospf here's the interface we want to advertise here's the area when we're sending that kind of information down it needs to be formatted in a certain way and with rest apis we're going to be using something called rest comp for rest configuration it can be formatted in one of two different ways Json which stands for JavaScript object notation or XML that stands for extensible markup language we're going to be mainly focused on Json today and when we send that information down to a device whether it's coming from our application whether it's coming from Cisco DNA Centrics coming from the controller but when it goes down to that device the way we can be vendor agnostic in all this is that different vendors have agreed to use the same data model they can represent their configuration Juniper HP Cisco is uh I'm trying to remember now calpena they were bought by Cisco lots of others out there but if they all agree to use the same way of modeling their data I can send my intent to set this interface to this IP address with this subnet mask in this description I can just put it in that model and it will apply to all these different routers with all these different operating systems because they all conform to the same model now what do I mean by a model just to make it really simple imagine the current iPhone 14 Pros that came out not long ago now I'm not upgraded I've still got I've got I've got the 13 Pro and I'm perfectly happy with it I'm probably not going to upgrade but if you want to upgrade to the 14 pro we could create a data model to describe your ideal Apple iPhone 14 pro first of all we have to understand that there are a couple of models you could just get the pro or the pro Max a larger form factor we could say what display size do you want is it 6.1 inches or 6.7 inches you can choose your color this year they're coming in space black silver gold or deep purple which uh it'll I'm a big the Deep Purple looks really really cool by the way I'm a big fan of that and you could choose Your Capacity how much storage internal storage do you want it starts at 128 gig all the way up to a terabyte I guess if you're taking maybe raw footage or something raw video footage maybe you need I don't need a terabyte but uh you get to choose that BC there are all these characteristics that could be used to describe the particular phone model that you want to purchase so we could say we're modeling this phone as a pro Max with a 6.7 inch display the color is space black and the storage is a 512 gigabytes that's a metaphor for how we can model data configuration data for our routers and switches I mentioned that we could have all these different vendors agree to use a common data model are they going to say let's use Cisco's data model they're better than we are no there's an industry standards group called The ietf and the ITF has for example a data model for interfaces that we would find inside of a router and that's what I'm showing you here on screen and this is called a Yang data model A Yang data model we see that the module name if you can read that it says it's ITF interfaces and we've got a section of read write information the things that we can change and we've got another section these sections are called containers we've got a section of read-only information so if I'm trying to retrieve statistics from a router interface are you dropping packets how um or what what's your bandwidth utilization right now how many unicasts versus multicast packets are you sending that's read-only statistical information that I can pull from the router but if I'm doing in configuration I'm going to be in that RW interfaces container and notice under RW for read write interfaces there is sort of an indention and we have a list it's called and that's the configuration for a single interface so maybe I've got a list for interface gig0 one then I can have another list for interface gig02 and zero size three we can have this is very modular we could have repetitive lists with different information in them and when I say RW interfaces star we give the identifier for the interface like gig one as an example and then we have a parameter that we could either read or write and here it says RW description so I could read what the current description is I could write a new description and the kind of data that I'm going to be able to write is in this case is just a descriptive string so that is called a leaf that's one element that we can configure inside of that interface now sometimes the instead of having a type of string data just where I'm typing out a description this connects to the HQ second floor we could have different types of of data for example here for the uh when we're saying when the configuration was last updated we see that the type is actually another Yang data model we're representing date and time using yet another Yang data model now to make this a little bit more real world let's take that same ietf interfaces data model and let's populate it a bit here we see basically the same thing we see that we're using the ITF interfaces data model we see that we have a container for interfaces and within that container we've got different interface nodes we've got one for Gig gigabit Ethernet one under that we've got an ipv4 node that's a that's a chunk uh sort of an indented chunk of configuration and again this is for gigzier ethernet one or zero slash one we can have that repeating for other interfaces so the Yang data model do you get the importance of that um it's going to allow us to be very vendor agnostic as we're coming up with ways of of pushing out our intent it's going to make it so so much more so much easier and one way of using that Yang data model is to use a utility called netconf now this is very popular and just to contrast it with the way we used to do things we used to go in via the CLI and we could do things like telnet or secure shell or even use SNMP to yeah yes SNMP was originally designed to be able to configure our network devices it still can people just never used it for that very much but mainly it was used to just monitor information but now what we can do instead of using those I guess we could call them Legacy approaches instead we're going to use something called netconf network configuration and this is sort of like a client server model where we've got this back and forth communication going between maybe a program running on our machine or a uh an sdn controller like Cisco DNA Center where they're having this back and forth conversation now here's the format if you want to kind of jot this down on a piece of paper the format of this we're going to have that um that data that Yang data model that's going to be saying give me this info give me your interface information or here's how I want to set your interface information that is Yang and it's in that data model now with netconf remember I said we could we had different configure or different formatting options I said with rest conf we could have Json or we could have XML we'd this is one of the reasons that I tend not to use netconf as much because it's not as flexible we must use XML personally I thought I like it's a personal preference I like Json better than XML but you know I can I can hang with XML if I need to but it's going to be formatting this Yang data in XML and then this netconf packet is going to be going back and forth between our manager whether it's our computer running a program or whether it's Cisco DNA center it's going to be going back and forth between the manager and the device being configured and it's going to make updates based on that Yang data model that's one way of doing it personal opinion here my preferred way of doing it is to use rest cough because I'm already so familiar with the way web pages work it's it's really using it literally uses the same protocol that I'm using to go buy a book from Amazon or buy anything really now from Amazon https it's going to be a secure connection using https it's still going to use that Yang data model just like netcoff did and if we want to use XML formatting more power to you you can do that but my personal preference is to use Json formatting I think it's easier to read maybe it's not for everybody I like it the best but we can use Json formatting in kind of a side-by-side comparison here I think this also makes the case that HTTP verbs they just look nicer than than some of the Nick conf XML operations going way back just give you a history lesson this is something by the way you should know for the CCNA exam this is on the blueprint they mentioned an acronym called crud c-r-u-d and this goes back to the database days the database people talked about ways of doing of creating a record of reading a record of updating a record or deleting a record in the first letter spells out not a very nice sounding word it smells like crud and we have ways of using crud in rest conf or netconf now here I said I'll give you some more examples of those HTTP verbs like post and get oh yeah be careful with this one if you're wanting to update something you don't want to post because that's going to create it from scratch it's going to override anything that's there but if you just want to make an update to an interface yeah that's going to be a put or a patch not not a post that's going to be a destructive right when you do a post but those are some of the verbs that we might be using all right let's go ahead and start working with some of this stuff in the real world we're going to work with something called Postman and here's the URL and this is going to be in that document you can download at the end of class but it's just simply postman.com and you know what I think I'm gonna I'm gonna go out live right now so let me take my face off the screen so we'll be able to see this a little bit better but let's go over to the devnet website and and I've got the link for devnet in your in that downloadable document as well but the folks over at Cisco devnet they have given us an amazing collection of gear and I think I mentioned this on Friday but we can access that gear for free uh in different as they call them sandboxes we can reserve time on some of those sandboxes some of those sandboxes are just always on uh and I've already gone ahead and logged in here to save the time of the actually it logged me out it must have timed me out here so let me really quickly just so my login credentials don't show here I'm going to re-log myself in one second while I do that okay I am back in so I'm logged in and I'm going to go to the sandbox catalog I'll make the screen a little bit larger where you can see it and there are plenty of technologies that we can practice here Cloud security Data Center and on and on I'm going to do a search for iOS XE on CSR recommended let's see oh that's not XC that's I said excess that's better uh yeah this is the one I was looking for notice it says it is always on meaning we don't have to do a reservation we don't have to set up a VPN connection to get to it it's just always on now please play nice it is possible to be destructive and knock this out of commission until they've the folks at uh the devnet sandbox fix it but here we're given a publicly available CSR 1000 V router and it says if you want to communicate to it on using net comp here's the port for netconf there's the secure shell Port we're going to be using a rest conf which you see is the https Port of Port 443 we're going to log in using a username of developer we're going to use a password of cisco12345 capital c and an i for the for or a one for the first I but what we're going to do is send a configuration instruction actually for the public one I'm not going to configure anything I just want to retrieve some information so the way we're going to do that is we're going to use a utility I gave you the link for it a moment ago called Postman and it's going to be able to send Using HTTP verbs it's going to be able to send these apis to the router to maybe get some information so let me show you let me show you Postman if you go to if you go to postman.com you'll find yourself at this page and I've already logged in you can create an account for free or you can log in I think using your Google credentials but um you can do everything from the web interface as far as I've noticed if you can see it down here there's an option to download the desktop app let me make it larger we can see that I'm going to be using the desktop app I just that's what I started out using and I'm just a big fan of using the desktop app so let me go over to that app right now okay we're now sitting in this uh we're now sitting in this desktop app and what we want to do is not start from scratch we don't want to reinvent the wheel Cisco devnet gives us a lot of code that we can start with so what I'm going to do where it says search Postman I'm going to search for Cisco devnet and under Cisco devnet the search results there's a team that's called Cisco devnet right there I'm going to click on that and it says the most active workspaces the number one is Cisco's devnet public workspace I'm going to click on that and look at all this stuff over here on the left hand side talking about sd-wan yeah we can start configuring things it's going to give us the basic code to configure sd-wan equipment we're going to be dealing with a CSR 1000v running a Cisco iOS XE so let's see if I can find that yeah let's open up Cisco iOS XE I'm going to be using rest comp as we've been talking about and I'm going to do something not destructive I just want to get some information how about we uh how about we read a list of interfaces on a router so I'm going to click read ietf interfaces list of interfaces see that's that reference again to the ietf data model we want to get a list of interfaces on this router so what we're going to do is we're going to send a get request to that router in the sandbox and it's going to say all right here are the interfaces here's the information about those interfaces now here's the caveat please make a note of this if you're not already familiar with it I'm currently in a workspace called Cisco's devnet public workspace allowed to run stuff from Cisco devnet's workspace I got to use my own workspace so what I'm going to do is I'm going to take this URL notice the get verb here's some of the other verbs we could use I'm going to use the I'm going to copy this URL and I'm going to go back to my own workspace so I'm going to go to workspaces my workspace I'll open up a new tab and I will paste in that link that says give me a list of interfaces and again I'm doing this on a CSR 1000v but this would work on a juniper router that supported the this ITF Yang data model now I have to fill in the host information and the port information you'll notice we saw that on the devnet sandbox websites let me go back to that I'm going to copy paste the fully qualified domain name of that router that they're making available to us and I'm going to replace host with that the port actually I remember what that is that's 443 so we'll say colon 443 uh let's see anything else I need to do here yeah I need to authenticate donut I need to give it a username and password so to do that I'm going to go under authorization and under the type of authorization I'm just going to do the basic off basic authorization and it looks like I've already got it set up the username is developer the password is cisco12345 capital c and a one is the I so this is looking pretty good let's try it I'm going to say send this get request to that router on Port 443 with these credentials and this is a what's called a rest API call that says give me your list of interfaces and it comes back and you can see the results down below the first interface that goes out to the internet is gigabit Ethernet one and and because this is the one that allows us to get into the router they have a very strongly worded description that says management interface don't touch me uh I accidentally touched it once and I knocked it out for a while they had to go reset it so my apologies I think the statute of limitations has run up on that so I could admit that Nails a long time ago but please don't mess with that interface but you can see its IP address it's 10.10.20.48 uh gigabit Ethernet 2. some people have gone in here you can see things that other people have created there's a loopback five interface Lubeck 25 interface yeah we can scroll through here there's a lot of them and we could see all of the interfaces that exist on that router now by the way what we're looking at right now is a Yang data model in this Json format what I could do is say Raw I could copy that and I could put paste that in the body that I wanted to sit now I'm not going to do this I don't want you to do this either but I could say I could go in there and change things I could send just a chunk of this code and instead of saying get I could say I could say that I wanted to post it in other words I wanted to update it now please don't do that on their router just do that maybe on on a local router but that's how I could do it what I want to do instead is I want to use a local router where we can actually make some configuration changes so I'm going to open up another tab here and I'm just going to use let me get the same URL I'll copy and paste that over here and the router that I'm going to be using is a CSR 1000v that I've got running I'm looking at the machine right now it's right across the room it's running in gns3 and I promise to talk about that as another option of getting some hands-on experience so here this Cloud connects this CSR 1000v router out to my local network that I'm on right now and I can say that I want to open the cons actually I think I've already got the console open let me get logged in here the username is Kevin the password is uh is Cisco so there is my HQ CSR 1000v let's see what interfaces it has right now I'm going to say show IP interface brief foreign and right now this is the IP address that it has so I'm going to copy that and going back to postman I'm going to say that that is the IP address that I would like to communicate with now I'm not done just yet because I started this in a new tab I need to say I need to say what kind of data that I'm uh that I'm going to be using actually I'm not going to be sending any before we send anything I I'm not going ahead of myself instead let's do exactly what we did on the devnet sandbox let's just pull some information down and then we'll send some information but let's start to let's start by making sure we can we can do exactly what we did before so I'm going to go under authorization I've got different username password credentials for my local machine I'm going to say I'm using basic authentication but the username is Kevin and the password is Cisco I'm going to send that and it comes back and this is my local router it says here's gigabit Ethernet one I don't have strongly worded descriptions there I've got a gigabit Ethernet two and three and four not a lot of not a lot of interesting configuration going on here but it still pulled it down it pulled it down from my from my local router now now let's actually make a configuration change using Postman and in order to do that I want to get some help from The Cisco devnet workspace so let's go back out to the Cisco devnet workspace under workspaces Cisco devnet workspace let's create let's see what we have here how about this one add a new loopback interface you see that let's do that let's say we want to add a new loopback interface so what I'm going to do is I'm going to make sure that I'm looking at the raw data I'm going to select it I'm going to copy it then I'm going to go back to my local workspace let's create a new tab and for the body I'll say Raw and I'll paste that in there now again I'm doing this on my local router so I'm going to go back over and get this same information it's going to be that same rest call except this time instead of using the get verb I'm going to be using the post verb and let's see let's make sure our authorization is set up basic authorization okay so Kevin and Cisco that's going to be correct something else that I need to be concerned with is I need to say I'm sending this for I'm using this particular format for the information I'm sending so under headers I'm going to go in here under headers and I'm going to say application slash actually I need to that I almost gave the value first that's not what I want I'm going to say my content type is application slash Yang I'm using a Yang data model and it's going to be Yang data so Yang Dash data and I'm using the Json format so I'll say plus Json and now we've updated the uh We've updated the header let's go back and change the body a little bit shall we instead of saying loopback 100 let's say we want to create loopback to make it easy to recognize we'll say lubeck123 and instead of saying configure by rest conf let's say configured by Kevin during Cyber Monday demo and let's give it a different IP address let's say its IP address is 123.123.123.123 I'll leave the subnet mask as it is I think we're good let's send it it comes back and says created that's encouraging let's go over to that router now and you can see in the background it says yeah neck netconf by Kevin so if I say show IP interface brief look at that here is a brand new interface that we created there it is loopback123 with an IP address of 123.123.123.123 how about that folks without even going into the CLI of that router I was able I was able to programmatically make a configuration change using Postman now admittedly what we did here was using utility where I had to post I had to type in or I paste it in the uh that that URL string I had to say what our header type was I had to give authentication credentials we don't want to do that every time we have to program something instead wouldn't it be better if I could actually have an application like write a program maybe a Python program that would do that for me yeah that's what we're going to do here in just a few moments and before I forget I'm going to go ahead and reload this I want us to start fresh with that program here in a few moments but um yeah I think I think that was an overwhelming success I hope you enjoyed that one where we were able to use Postman to send a rest API call using Yang formatted data being sent with uh restful apis which used HTTP verbs to configure a router that is a paradigm shift if I have ever seen one compared to the way I have configured routers for just for just literally decades so I that's pretty exciting stuff so if you were concerned about having to learn all this new stuff hopefully that gives you a little a little piece of mind that it's not it's not that bad it's it's very learnable stuff I said we want to write a Python program but and that's what most people it doesn't have to be python but most people in network automation they're using python uh to to write these applications I was reading a study the other day that showed the most in demand or the best programming language you can learn right now is python so I thought you know what let's do a sort of a basic Python tutorial today if you don't have it installed on your machine already or if you'd like to update the version on your machine you can go to python.org like you see on screen there's a download area where you can download the latest version when I took this screenshot the other day the current version was 3.11 I've got 3.10 installed on my machine I installed a little bit before I took that screenshot the reason I make a point of this is some systems are like some older Mac operating systems some Linux systems they might come with the version of 2.7 installed probably up you can use it but most things are going to be working with some version of Python 3 and that's going to be our Focus now I've got it installed so what I'm going to do is go out to a terminal and if I look at my present working directory I've created a little subdirectory called python that we're going to be playing in today and if I do an LS minus l I can see that yeah I've already got a Python program and that's what I'm sharing with you in that download later today and I write you know I've got a file called inventory.txt if I say cat inventory.txt just to show you what's in it it's an inventory of some different Cisco gear that we're pretending I have in my inventory but I just want to explain what those files are for but once we've got python installed as I do uh oh let me make this full screen so you can see it better you don't need to see my face for this that looks better we got python installed and right now we can go into what is called the interactive interpreter which means that python does not have to be compiled and then run it can interact with this and run real time for example what I can do I could say python now again if I have more than one version of python I can specify that version here I have 3.10 so I can say python 3.10 or since I've just that's the only version of Python 3 I have installed I can just say python3 and you see I'm at a I'm at a different I'm at a different prompt here if I can get my mouse to work right oh you know what my uh my Mac laptop has decided to become another screen on on my laptop so now as I'm moving my mouse around it's actually going on my Mac laptop that's not helpful at all but uh here we go let's see if I can highlight it for you there we go these three greater than signs that's my python prompt and if I want to exit out of python I don't just say uh exit or quit I have to give a function I could say exit open and close parentheses oh the spell exit exit open and close parentheses uh or if I go back in another option is uh I could just do a the Ctrl D to get out of it but let's get back in for a moment and let's give some commands here from the interactive interpreter the first thing that I'd like you to know about python is unlike unlike me it's really good at math and it can do some things like this it's going uh let's let's say five times five where the multiplication sign is the asterisk and it says oh it's 25. white Space by the way does not matter I can say five space times five excuse me enter and just pardon me and you can see that works uh that works just fine and one second here I've got some stuff going on with my presentation console just trying to get that sorted out all right I think I'm good now okay now I'm good what else can we do let's um let's get out of this environment though because even though we are in an interactive interpreter and we can give commands there's a better environment I'm going to say exit open and close parentheses and I'm going to go into what is called the interactive development environment uh known as Idle idle for short I'm going to say i d l e 3 because I'm running python 3.10 and let me bring this up on another screen we get to a blank screen I'll drag it over foreign Idol environment my interactive development environment and from here again we could do some math we could say five times five is 25 and so on but in addition to doing basic math we can assign variables we'll be doing a lot with variables I'll say for example x equals four and then I could say something like x times 3 and it's going to know that x equals 4 so it's going to give me a result of 12. I can say 12 divided by 4. notice that this is a little bit different it gives me a 3.0 that's a different data type we're going to be talking a lot about data types this is a floating Point data type I could say 12 plus 4 no decimal there that's just an integer data type it's a 12 minus four or if I want to raise something to a power I use two of the stars I could say four star star two that's four squared and that's going to be 16. now I've mainly just been working with integers that don't have decimal places and that is one type of of data that python recognizes if I want to see what kind of type of data I'm dealing with I can use the function of type and in parentheses I'm going to say what is the type of of a four well that's an integer what is the type of 4.5 that's a floating Point that's a floating point value oh by the way uh we can have strings like I could say what is the type of in quotes my name Kevin you put strings in quotes by the way it says the class is a string check this out I could actually multiply strings I could say Kevin close print uh close quote times four and it says Kevin Kevin Kevin so that's kind of fun uh let's um let's see how we could print something to the screen I could say print I'll say print open parentheses and I'll say in quotes I'll do my name first of all and I'll show you another way of doing it print Kevin well I'll send it prints that it sort of Echoes that to the screen now let's assign each of my names first last name let's assign a space Also to a variable I could say I've got a variable called first name equals Kevin now notice notice the uh the fact that I used an uppercase n in that variable name where it said first name if you want to write this in your notes that is sometimes called camel case that's where if you're putting two words together in a variable you don't capitalize the first the first letter of the first word but you do capitalize the first letter of subsequent words so here I'm I'm combining the words first and name I don't capitalize the F in first but I do capitalize the n in name you don't have to do it that way it's just a it's just a very common a common way of naming your variables let's create another variable called space and I'm going to say that it equals in quotes just a single space let's create another variable called last name equals in quotes Wallace try that again last name equals in quotes Wallace now let's see if I can group all those together this is called concatenation where I'm adding multiple things together I can say print and then in parentheses print whatever value is stored in the variable of first name Plus in other words add on to that output whatever stored in the value of space which is just a space and add on to that output whatever value is stored in the variable of last name and let's enter that and it prints my name Kevin Wallace excuse me another another data type we have is a Boolean data type is something true or false is it greater than or equal to for example I could say type true the word true itself is a Boolean variable so is false I could say does the first name equal Kevin here's how I would do that I would say first name and I don't just say equal Kevin because that would actually set the variable but if I want to check and say and ask the question does the first name equal Kevin I could say equal equal we have two equal signs and then in quotes say Kevin it says true yes it does what if I said does the first name equals equals Wallace it should not equal Wallace so no that's going to be a false however what if I said first name does it not equal Wallace to say not equal it's an exclamation point or some people call that a bang it's a bang equal sign does that equal Wallace let's see uh oh I misspelled first that's embarrassing let's try that again first name exclamation point equals Wallace it says true because that's a true statement the first name does not equal Wallace because it equals Kevin we can do this with integers I'll say I could say it's 5 less than or equal to four uh no it's not is three less than three no but is three less than or equal to three yes it is so that's just a look at playing with some different data types that we have we've got strings we've got integers we've got floating Point variables let's let's take a look at some more programming stuff here let's look at something called a conditional in other words a conditional says if something is true then do this if it's not true then do something else so I'm going to say if pardon me if 10 is less than 100 and I'm going to give a colon here so let me go down to the next line it's um that's going to let me have a compound instruction I'm going to say if 10 is less than or equal to 100 then I want to print I want to print 10 is less than 100. see I did something wrong there oh yeah I forgot my second I forgot my second quote let me try that again if if 10 is less than 100 print 10 is less than 100 and I'll press enter again and that was a true statement so it says yep 10 is less than 100. now what we've been doing right now I said this is great because unlike some programming languages that I used to work with like C or C plus plus where you had to write a program then you had to you had to compile it and then you could run it here we've just been sitting at the interactive interpreter and as soon as we enter something it doesn't it makes things go a lot quicker but what we can do instead is actually write a program that makes it reusable we don't have to type it in every time so here in the idle environment I'm going to go up to file and I'm going to say new file and I'm going to save it and I'll just save it as Cyber Monday Dot py for python and let's do another example where we're using some of those true false conditions I've said that I do a lot in the in the collaboration space and one thing we talk about in collaboration with iPhones is the concept of a native VLAN and a in a data VLAN uh actually a native Elan and a voice VLAN we often say so I'm going to say my native VLAN equals one that's going to carry my data from an attached PC going into a phone I'm going to say that uh my voice VLAN equals 100 and I'll say if the native VLAN equals equals The Voice VLAN colon then I want to print the native and voice vlans are the same what if they're not the same I can say else so what do I do if that's not the case I'll say instead print the native and voice vlans are different let's save this now and run it no need to compile it I just say save and then under run I say run module and when I do that here on my idle screen it says the native and voice vlans are different well that was kind of cool now let's um let's go back to our file for a second make some changes here let me just delete what we have here let's do some quality of service stuff let's say that the COS the class of service will say equals five a dscp value for a packet equals 46 which is what we would expect with voice packets by the way and I'll say if cos equals equals five and so this is a compound conditional I'm saying both the COS has to equal five and the DSP has to equal 46 if cos equals five and dscp equals equals 46 colon then I want to print this is a voice packet because that is a characteristic of a voice packet if that's not the case I'll say else I want to print this is not a voice packet let's save and run that I'll say save run module and it says this is a voice packet it met those uh it met those anticipated criteria now let's spend some more time on the interactive interpreter and I want to show you a few other things let's go back to our interact well we're actually in the interactive interpreter let's just stay here for a moment let's take a look at another type of variable we could have you might want to make a note of this it's called a list variable or a list data type I guess would be a better way of saying that a list for your notes that is an ordered list of comma separated values enclosed in straight brackets I'll say that again a list is an ordered so they're not just random I might say give me the fifth item in the list because that's significant a list is an ordered list of comma separated values enclosed in straight brackets let me um instead of topping everything out by hand and just consuming class time I'm just going to paste in let's paste in a list but notice the syntax here it says inventory that's that is my list and then in uh in straight brackets I have some things in my inventory a catalyst 3750 notice it's in quotes because it's a string comma another item in closing quotes comma another item in closing quotes comma another item in closing quotes and when I'm done yeah I close out my Straight brackets so I just assigned a list data type and if I want to see what type of data the variable inventory is I could say type inventory and it says oh yeah that is a list we can also say how many items are in that list I could say l-e-n for length what is the length of inventory four oh something else I could I could use length for lots of things I could say what is the length of the string Kevin there are five letters in Kevin so it says the length is five if I want to see all the items in the inventory I could simply say inventory there they are or remember we said that it was an ordered list I said that order was significant here here's how we can go in and cherry pick specific items from that list I could say invent oh and here's a huge point please hear me on this a huge point is the numbering starts at zero please write that one down the numbering starts at zero so I'm going to say inventory and then in Brackets I'll say zero and this is saying show me the first item in the list Catalyst 3750 if I want to see the second item in the list I would say inventory in Brackets one again because the numbering starts at zero if I want to see the very last item in the list I can say inventory and instead of having to count over and see how many are in the list I can just say inventory minus one and that's going to give me the very last item in the list which is a 79.45 Gip phone if I want to see the next to the last item I can say inventory and in Brackets negative two that's a 2911 router so it's an ordered list and I can go in and again cherry pick things from that list and I can say what is the length I think I may have already shown you this but I want to make sure you get it the length of inventory it's four there are four items in the inventory now there is a similar but very different data type for your notes so again a list it's an ordered list separated by commas enclosed in straight brackets there's another data type for your notes called a dictionary a dictionary is an unordered list of name value pairs for example there could be a variable name of last name and the value might be Wallace there might be another name of first name and the value might be Kevin so we've got these pairs of data a name and a value the name and the value are separated with a colon if you want to have another name value pair there's a comma in between those pairs and the big difference is instead of being in straight brackets a dictionary is in curly brackets let me show you again to save topping time I'll just paste it in but there it is I'm saying IP address and here is my first name value pair the name is router a it's a string so it's in quotes and I'm saying colon that separates the name and the value and the value encloses 10.10.10.1 so I've got a name of my device router a and it's got a value which looks to be the IP address of 10.10.10.1 and then I've got a comma and another name value pair router B it's got an IP address of 10.10.20.1 switch a 10.10.30.1 switchb10.10.40.1 notice they're in curly brackets and if I were to and I assigned all that to the variable called IP address and if I say type if I say type didn't mean to use a curly bracket there if I say type it says that is a dictionary if I say I P address and then I name one of the remember we had those name value pairs if I want to see what the value is for a name I can specify the name one of the names in quotes was router a RTR hyphen a I'll put that in Brackets and it says oh that's 10.10.10.1 so that's a way of querying what we have what if I want to add something to the dictionary here's how I do that I would say I P address the dictionary name IP address and then in brackets I would say what my new name is RT r c let's have router C now and I'll say that it equals the value of 10.10.50.1 and if I say IP address now you're going to see that it has a new entry this was not here before we just added a name value pair for router or for router C if I want to see if router C is in my list I could say RTR hyphen C and I could say is that in the variable of IP address true so that's a little Boolean check for us next let's take a look at how we can do looping here's what I mean by that we can repeatedly go through a section of code until a certain condition is no longer met for example I've already got a variable called inventory if I just say inventory that's what's in inventory right now what if I wanted to pull out the word uh what if I wanted to pull out the inventory items that contain the word Catalyst check this out you're going to love this I want to say four item so this is a four while loop it's called so four item in inventory colon if the word catalyst is in item and I'm going to explain all these as soon as I get it topped in then I want to print whatever the item is and notice it came back and it printed those two items that had Catalyst in the name now let's break this down I'm saying four item in inventory what that means is I'm going to start off with the very first item in this inventory which is a catalyst 3750 and I'm going to assign that to the variable called item this is my first time through this Loop the next line says if the word catalyst is in that item then print it well here cat was 3750 yeah the word catalyst is in that so it printed it Catalyst 2960 the word catalyst is in that so we printed that but for the other two the router and the phone yeah it didn't have the word catalyst so we did not print those that is a four while loop that's not the only kind of Loops we can have but I wanted to give you an example of of one of them anyway now let's go back to our editor let's let's write some programs as we start to bring our little Python tutorial to a close here is a program that I that I'm just going to paste in just to save time and then we'll talk about it paste it in here I'm setting the value of inventory kind of like we did before here's what's in my inventory and I'm going to say the current inventory is and then we're going to print it pretty simple let's save let's run it and it says the current inventory is and it lists everything out now let's check out a new command that is going to be able to prompt the user for some input what if the user wants to add an item to the inventory let's go back and we'll say item equals now here's the new command I'm introducing you to this is a way for us to ask the user to enter data I'm saying item equal input so that's the keyword input item is the variable we get to call that anything but I'm saying I want the user to input something and they're going to be prompted with the string enter new inventory item colon space close quote close parenthesis and in order to add something to the variable of item I'm going to say inventory Dot append whatever the value is that the user had typed in and I'll say print the current inventory is and we'll print it out we'll say print inventory in fact you know what let me uh let me paste back what we had to start with so it's going to initially set the inventory it's going to show us the current inventory then it's going to prompt us to enter something new so let's save this let's run it and it says the current inventory is that and it says let's add something so I'm going to add a 79 [Music] 45 G actually we've already got one of those 65 G IP phone and it says okay the current inventory is and it shows the now expanded inventory so we were able to query the user for some input and and add that information now we might not want to add that initial data like the inventory into the program it's probably a lot more convenient if we just have that stored in a file somewhere it's going to be a lot easier to update that in order because remember at the very beginning of this I showed you that I had a file called inventory.txt what if I did this what if I pulled information in from that file so let's go back here delete all that I'm just going to paste in this little program and we're going to talk about it file open so I'm opening a a file that's in my current directory that's called inventory.txt I showed you the contents of that earlier and I'm opening with the r flag R means read as uh as opposed to W which means right as opposed to a which means append I'm just reading from this it's not destructive and I'm saying for item in file so it's looking at the very first item in that inventory and it says this is just getting rid of the extra an extra carriage return so this is just sort of a a cosmetic thing there we're stripping off the the slash in that would be there otherwise and we want to print that item and then we're going to close that file let's save this and run it and see what it did it went out to that file in my in my file system inventory.txt and it read in one atom at a time and it printed one item at a time nice let's see what should we uh yeah let's do one other thing let's read in the inventory items from the file and use those to populate a variable instead of just printing them out so here's what we can do I'm going to I'm going to create a variable that's called inventory so here's uh here's a here's a list variable and it's just called inventory and there's nothing in there it's Open Bracket close bracket but then I'm opening this file on the hard drive called open or called inventory.txt I'm just reading it and we're reading through each of those items and I'm saying inventory.apin whatever the value of that item is so I'm starting with a an empty list and I'm populating it and then when I'm done I'm going to print it let's save and run it and there we see the output okay one let's do ah that was going to make that my last one I want to show you one other thing though we haven't actually written anything to a file I want to show you how to do that let's see how to write a file and I don't want to be destructive and overwrite my inventory text file I just want to add something to it so instead of using the right mode I'm going to use the append mode I'll piece that in so I'm opening that file inventory.txt I'm appending it I'm not overriding it and I'm saying while true while there is an item that I can read it says the variable of new item is going to be whatever the user says it is so we're saying enter the new item and if they give the keyword of exit that means okay I'm done adding items and it will say all done and it will break out if we're not entering exit it's going to write that new item and put a carriage return after it or it's gonna it's gonna write that to the file so let's save and run that and it says what is your new inventory item and I'll say oh let's say it's a Cisco 7945 G IP phone let's say that um let's say my next item is a Cisco Unity Express module let's say my next item well let's say I'm done I'll just say exit let's go it says all done and it exercises uh it exits us all the way out now if I were to go back to my terminal if I were to let me open up another tab here if I were to look at the contents of inventory.txt you see that it is added those two items that I just uh that I was just prompted for so that my friends is a look at sort of a that is your crash course on on python I don't want to leave you with just that though let me give you some book recommendations one book recommendation that I have for you and this is a I would recommend you start with this one it's called Learn Python in one day and learn it well it is a it is a very thin book and this is a very digestible book I went cover to cover through this book it teaches you the basics it covers the kind of stuff we did today and and even a little bit more but this is a great first step in learning python again Learn Python in one day and learn it well another recommendation I have for you and this is I use this more as a reference it's not something I've read cover to cover but if I want to figure out how to do a particular function or a particular routine I might turn to this and it's called python crash course by Eric Matthews so there it is I'll give you a look at that python crash course by Eric Matthews so those are a couple of recommendations for your next steps in learning python hey welcome back from the break everybody I wanted to show you one more thing that I neglected to do before we went to the break we went through our our primer on python but I wanted to kind of tie it all together the connective tissue of the first part of class in the second part of class how can we now take a Python program and go make a configuration change so let's do that really quickly and then we'll wrap things up with cyber security we're still on track to be done three hours today so not much more to go I mentioned that we probably would not create a lot of programs from scratch we would start using our own code and just update that or we might go out to GitHub and get somebody's code I'm including a link in the download you'll get to if you stick around to the end today I'm including a link to this GitHub location and I don't know who this person is but they have some great code on here that shows how to deliver a Json formatted payload to a device you see this area right here that says payload this is being used to add a loopback interface kind of like we did with kind of like we did with Postman so we can take that code run it against our CSR 1000v and create a loopback interface so what I what you can do is you can say give me the raw format of that you can copy it and you can paste it into a text editor and update it for your own needs now notice a couple of things here it says import requests import Json if you run into some of these some of these programs some of these scripts out on GitHub and it's importing different python libraries I wanted to show you how you would install those so let me go to a terminal really quickly let's say that I did not have the uh the python package called requests I would say pip for package installer for python I'm running version 3.10 and I would say install requests if I didn't have that now for me it says yeah this this requirement is already satisfied I've already got it installed but you would use pip again pip stands for the package installer for Python and that's how you would get those now back to our back to our code for a second here they have hard coded the the domain name of the server of the router we're trying to configure they're giving the credentials here they're giving the port number here and then they're taking those variables that they're assigning as part of the code and they're putting those variables in this URL and they're setting the loop back to a loop back ten thousand there's a lot of stuff in other words that's hard-coded in here I thought wouldn't it be great if we could kind of update that based on what we just learned about Python Programming what if we updated that to where we would be prompted to enter that information oh oh I still have my face on screen let me let me get my face off the screen so you can see this a little bit better and what I want to show you right now is what I've included for you in your download today and it is if I can find it here it is here is my modification to that program on GitHub notice that I'm using a series of these input functions remember we saw that before the break I would say for example I have a variable name called you name for the username and I'm saying input and then I'm asking hey what's the what's the router username and I'm prompting for the password and I'm prompting for what port number we're using I'm prompting for all the information that that might be different so you can go through that at your leisure but let's run it what do you say and let's see if we can make an update to our CSR 1000v before just a like in a magic show let me show you I've got nothing at my sleeves right now let's take a look and see what interfaces we have now I'll do a show IP interface brief and right now I've just got those four gig interfaces I got rid of the one we created earlier I've got those four gig interfaces let's create a loopback interface shall we so let me go back to our foreign my python directory there we are if I do an LS minus L here's that program that I've update that I've that I'm going to use it's called add loopback Dot py and here's how we run it I'm simply going to say python 3.10 because that's the version I have installed and I'm going to say run add loopback Dot py and now it's going to interview me and ask these questions instead of instead of just having to go in and hard coded in the program itself so what is the IP address of the router well in my case it is 172.16.106.226 . you know what let me make sure that's let me make sure that's really true because I did reboot it I want to make sure it still has the same one one's uh yeah it sure does so that is the IP address of the router the username is Kevin the password is Cisco the port is 443. let's create loopback interface 22. and I'll give a comment I'll say added by Kevin during cyber Monday demo and it says what's the IP address of that blueback interface let's make it 22.22.22.22 we'll give it a 24-bit subnet mask 255.255.255.0 and it's off we'll give it a few seconds and it says successfully added the interface let's confirm that let's say show IP interface brief look at that there's a brand new there's a brand new loopback interface if I do a show run let's see if we can see our comment that we put in there yeah I ended by Kevin during Cyber Monday demo so let's just I know we've covered a lot with network programmability so let's just sum up really quickly here we started out by learning the benefits of sdn software-defined networking we learned some of the terms that were used to describe a software-defined network southbound interfaces Northbound interfaces rest apis we talked about netconf and rest conf and yang data models we uh we saw that a Yang data model specifically could be used to more generically represent the configuration of a device which in our case was a router and using that data model we saw how we could pull configuration information from and send information to a device using HTTP verbs that was an approach called rest representational State transfer we saw how we could use a utility like Postman to send a payload in Json format to that device but we wanted to be able to do that using a program so we spent some time learning the basics of python we saw how to install python how to use the interactive interpreter called uh we saw how to use idle which was sort of a better version of the interactive interpreter we saw different data types we talked about integer floating Point Boolean list dictionary we saw conditionals we saw how to do looping we saw how to get user input how to search a list how to print to the screen how to write a program instead of using the interactive interpreter how to read from and write to external files then we combined our knowledge of rest in Python to modify a python script that we got from GitHub to prompt us for that information about a router interface and we ran it and we were able to just by answering some simple questions through a program add an interface to a CSR 1000v router my friends we just did Network automation [Music] [Music]