Skip to content
CBT Nuggets
DemoBook a Demo

Introduction to Python

This skill, led by John McGovern, introduces Python programming with a focus on network automation for network engineers. It covers the basics of Python, setting up development environments on various operating systems, and using tools like GitHub and Cisco DevNet sandboxes. The course emphasizes practical applications of Python in automating network tasks, enhancing efficiency, and improving reliability.

Full skill from Python Network Automation. Preview the training IT teams trust.

Updated

59m

Skill 1 of 23 in Python Network Automation

What you'll learn

  • Explain why programs need explicit, literal instructions and what it means that Python is an interpreted, high-level language
  • Describe how network automation improves efficiency and where Python fits for network engineers without a programming background
  • Set up a Python 3 programming environment on an Ubuntu Linux virtual machine and explain why Linux is the preferred platform
  • Compare Windows setup options, including WSL1, WSL2, and a remote Ubuntu VM, and explain the Hyper-V conflict with nested virtualization
  • Configure VS Code with Python 3 as the interpreter and install extensions such as Cisco IOS syntax highlighting for Jinja templates
  • Access Cisco DevNet always-on sandboxes to test scripts against Cisco devices without booking a topology or using a VPN

Free 59-minute video lesson with a full transcript and knowledge checks. No account needed to watch.

Overview

Join John McGovern as he walks through the basics of Python.

Recommended Experience

  • At least six months of Python programming experience is recommended, but not required
  • An understanding of concepts taught in Cisco CCNA is recommended, but not required

Related Job Functions

  • Network engineer
  • DevOps engineer

John McGovern has been a CBT Nuggets trainer since 2020. His areas of expertise include Python, networking, network automation, and cybersecurity.

Introduction to Network Automation

Join John as he introduces you to this skill and the benefits of automation within the world of networking.

Python & Programming

Python is one of the most popular programming languages in the world. Learn why and how you can benefit from learning how to use it.

Knowledge Check

This course will be focused around Python2. True or false?

Linux Setup

Learn how to set up your programming environment within a Linux operating system.

Knowledge Check

Which of the following commands was used to install Visual Studio Code?

macOS Setup

Learn how to set up your programming environment within a macOS operating system.

Knowledge Check

Which version of Python was installed in this Nugget?

Windows Setup

Learn how to set up your programming environment within a Windows operating system.

Knowledge Check

Which command was used to install an SSH server on the Ubuntu virtual machine?

Useful Extensions

Extensions allow you to speed up the development of your scripts and programs and help you minimize errors. Learn all about them in this video.

Knowledge Check

All Visual Studio Code extensions must be purchased prior to installation. True or false?

Cloning Code from Github

Github allows you, or anyone else, to store code in an accessible place so that anyone in the world can download your code and use it in their own projects.

Knowledge Check

Which of the following commands will allow a user to download and copy a remote repository?

Cisco DevNet Sandboxes

Note: This section is specifically designed for those preparing for a Cisco exam or planning to work with Cisco products. If you're focusing on general Python skills or network automation concepts outside the Cisco ecosystem, you may skip this section without missing any key content.

Learn how you can gain practical, hands-on experience by using Cisco DevNet sandboxes.

Knowledge Check

The Cisco DevNet Always-On sandboxes must be booked prior to use and accessed via a VPN. True or false?

Running Our First Script

Learn how to run a script on your machine.

Knowledge Check

Which of the following commands would execute a python script?

Conclusion

I hope this has been informative for you and I would like to thank you for consuming.

View Transcript

Introduction to Network Automation

0:12Hey, guys, and welcome to the CBT Nuggets course on learning Python for network engineers. So first, let's start by addressing who exactly is this course designed for. Well, the clue is in the name. It's designed for you, the network engineer. But why would you as a network engineer, the type of person who deals with access control lists, who configures routing protocols and VLANs, why exactly would someone like you want to learn a programming language like Python?

0:43Well, I'm sure you've already been keeping up with the latest news. Without doubt, the biggest buzz within the networking industry is the concept of network automation. Network automation is absolutely everywhere. And the significance of this change was really hammered home when Cisco announced its DevNet certifications. When this happened, people really started to take notice and realize that this was not a fad, it was not just hype, and it was not going away. Now, quite understandably, many within the networking industry were a little bit uncertain about this news.

1:18Networking is a really hard profession. It's not easy, as you know. How on Earth are you going to remember all of that crucial networking information, and then squeeze in a whole new profession? Do you have to be a developer now? A developer and a network engineer? Well, that's two separate professions. How much time in the day does someone really have, honestly? Well, here is the good news when it comes to network automation. Network engineers can learn the Python programming language to effectively do their network engineering job more efficiently.

1:51So the focus still is the network. Your network and information doesn't just go out the window. It doesn't just become antiquated and old hat. Certainly not at all. This is still going to be of primary importance. But what about this Python stuff. How much do we really need to learn? Do we have to become experts in programming? Well, again, this is the good news. You really don't have to learn an awful lot of Python to get a lot of mileage within the realm of network automation. So pretty much if you're just starting your career as a network automation engineer and you have some programming experience, well, this course certainly should help you solidify this information, and maybe help build upon this knowledge, and help give you ideas how to apply this knowledge to the network.

2:38But here's the thing. Even if you have no knowledge of programming whatsoever, i.e. zip, nada, none, then my goal for this course is to be able to help you get from ground zero up to learning the fundamentals of Python. And like I say, even a small amount of Python can go a long way in the network automation space. So ultimately, throughout this course, we're going to be discussing Python concepts such as lists, and dictionaries, and loops, and a whole lot more. But what my goal here as a network engineer primarily is to try to teach these concepts, not from the traditional programming point of view, my goal is to help you understand these concepts and see how they can be applicable to the network.

3:23Because it was only when I saw how it could be applied to the network, that was when my enthusiasm for the programming language really accelerated. Suddenly it felt like I had this big new superpower to be able to manage the network. And let's be honest, that sounds like fun. Now quite frankly, some concepts can't really be taught from a network-centric point of view. Sometimes we will fall back into more classical programming examples, but ultimately, we're always going to be trying to tie that information up and bring it back into the networking world where we can so you can really get a sense of the applicability here.

3:59So that is the rough goal for this course. But what about network automation generally. Why did this take off? Why is it such a big deal? Why can't you go on Twitter or LinkedIn without hearing a constant buzz about network automation from your colleagues? Well, at a glance, being able to program your networks is going to make your efficiency skyrocket. You know those mundane tasks you hate to do, perhaps maybe like documenting the network, how many devices do we have, and what are the serial numbers, and what are the IP addresses.

4:29And gather them all together and put them into an Excel spreadsheet. Wouldn't it be cool if you could just write a little program and then within 10 minutes, sit back, enjoy a coffee, and have a computer go and gather all that information all at once, put it into a spreadsheet, and send it off to your boss. Well, by learning Python, this is exactly the type of thing you will be able to do, no problem at all. And related to an increase in efficiency, we're also going to see an increase in speed. So it's not just about you being able to put your feet up and maybe go do another task at the same time as your program is executing.

5:03Simply put, what may take you three weeks to do manually, a program may be able to do within 10 minutes. Similarly, if you happen to make a mistake within the network, you can reverse those changes much faster as well. And if you know-- as a network engineer if you melt down that network, it can be a pretty stressful event. So if you can have an undo button, which can go out and pull the network back to a safe state, well, that's probably a good thing for your blood pressure. So tying in with that concept as well is we're going to see an increase in reliability.

5:36Teams that implement automation have much more stable networks. They have less downtime. And quite honestly, it really does encourage network development and growth. What do I mean when I say that? Many networking teams almost adopt a posture of fear. They don't want to make a change because if a change goes wrong, well, it might be hard to recover and re-converge the network. However, with automation, being able to make these changes much more reliably as well as having the option to easily roll back upon misconfiguration, it gives you a lot more freedom to try to optimize the network as opposed to just leaving it in a still stagnant state that you know is pretty safe, but not really optimal or efficient.

6:18And again, how much Python do you need to know to be able to do these types of things? Well, the good news again, I say, really not that much. And now lastly, almost selfishly, it's going to be really good for your career. Programming skills are really in demands, and the ability to be able to apply these skills to automate out networks so that they can be deployed faster, rolled back easier, more stable, save the company money, all of these things make you a valuable employee because if you can leverage the power of Python to be able to do the workload of 10, 20, 30 regular network engineers, then you can imagine that companies will pay you a lot of money to be able to do that.

6:57So I really do think this is going to be a great journey for you to embark on. There is so much upside and it really is a lot of fun. Now, if you are like me and you come from a networking background and you know nothing about Python, then just like me I expect you to find some of the concepts a little difficult at first. This is plenty normal. It's no different than when you first learned how to subnet. The key here really is just simply practice. So throughout this course, I'm really going to try to make it as hands-on as possible for you, so that you're getting your hands dirty, you're typing up code, and you're beginning to see the applicability of what you're doing and how it actually pertains to the network.

7:35So suffice to say I'm really excited to get going with you. And I hope this has been informative for you, and I'd like to thank you for viewing.

Python & Programming

0:12Hey, guys and welcome back. So, let's talk about this strange concept known as programming. When I first heard about programming, in my head I had this image of a lot of green text on a black terminal, and a lot of ones and zeros flying around and just a lot of magic happening. It just seems so esoteric and just so strange. But once you understand the concepts behind programming, everything becomes a lot clearer. Now, the first thing that you will notice when you began programming is that computers are pretty stupid and they are very, very literal.

0:47You know the way we as humans communicate with each other? This is not the way that programming works. You might hear a good song and say it's kick-[garbled], but to a computer, kick-[garbled] is committing an assault on someone's backside. Same way if you say a song is pure fire. Pure fire to a computer would be an emergency. That would be something is actually burning down. Computers are really, really literal. So, if I had to ask you, how do you make a cup of coffee? And I asked you to write down the steps of what you do. So, you might say, you know, you boil water.

1:18And then you say you put coffee in the cup. And then you pour boiled water into the cup. And then maybe you add some milk because you don't like it too hot. And then you add sugar, maybe, if you've got a sweet tooth like me. And then you drink the coffee. Now these are perfectly reasonable instructions you could give to a friend who doesn't really drink coffee, and they could take these instructions, go into your kitchen and probably make a pretty decent cup of coffee. OK, maybe not decent, but still a cup of coffee. But if you gave these types of instructions to a computer, you would have to be way more explicit.

1:51The computer would want to know where is the water coming from? How were you going to boil the water? What are you going to use? How are you going to get the water into the kettle? How can you tell if the water is boiled or not? Where did you get the coffee from? How do you know it's coffee and not tea? How do you know that the water has boiled? Add milk to cup. Add milk to which cup? The same as the coffee's in? Add sugar. Add sugar where? To the cup? To the table? Drink? Who's the drinker? And how do you drink it? What does it even mean to drink? Now, this all may sound a little crazy because you as a human don't think this way, but honestly, it's this type of literalism that you really have to kind of adapt to when you're dealing with programs.

2:33Again, the good news is, is that this is something you can definitely get used to and it really is quite fun to learn. So, that is a more broad overview of programming in general. But what about this language right here, Python? Well, some brief details about Python is that it is what's called an interpreted language. And you'll get to see what this means as we progress throughout this course. It's also called a high-level programming language. What does this mean? Well, is good news for me and you. It makes our job a lot easier.

3:04There's a higher level of abstraction. Python hides away a lot of the lower-level details of what's really going on under the hood. Now, ultimately, this makes Python a very readable programming language. Now, that doesn't mean that all Python code, you can just read it like you could read a book. But in the world of programming, Python code is vastly more readable than something like say, assembly language or Java. So, this makes a really good fit for us as network engineers who maybe don't have the classical programming background.

3:36Now, another great feature about Python is that it is so, so flexible. And this is something you're going to find out hopefully within this course is that with Python, you can do an almost limitless amount of things. And one of the reasons why this is possible is number one, the way the language has been designed. And two, because Python has a huge, huge community. So, how does a community actually help us as Python programmers? Well, basically, what people do is, they write their own Python code to create Python tools and modules.

4:09Here's an example. Someone in the Python community created a module called IP addr. Now, this is very useful for us as network engineers because with this module, you can just feed it a particular sub net and this module will automatically tell you the host range, the network address, the broadcast address, and many, many more. Ultimately, what this translates to is that, people have already done so much of the heavy lifting for us, we don't have to code all this ourselves.

4:40We can just make use of the great work of the wider Python community. And like I said, the community is so large. There are so many useful modules like IP addr which is going to make your job much, much easier. And in fact, you don't have to write the tools. If you just learn how to use the tools then you can really improve your efficiency in managing your network. Now, there's an important thing to note with Python. Is that there are two main branches of Python. First one is Python 2.

5:10You might still see this. And the second one is Python 3. Crucially, within the year 2020, i.e., last year, Python 2 has been completely deprecated. There is no longer support for this. The difference between Python 2 and Python 3 is that Python 2 was an older implementation of the language. There were some slightly different rules, some of the syntax was different, but you might still see some Python 2 code out in the wild. In fact, even if you happen to install maybe Ubuntu, by default, Python may actually be used in Python 2.

5:44This is something you want to watch out for because what we are going to be focusing on is the new Python 3. And when I say, new, Python 3 has been it for many, many years. But on this day in 2021, Python 3 should really be the only type of Python you are using. And it's going to be Python 3 which we are going to be exclusively focusing on within this course. But how on earth do we set up Python 3 and make sure it's not Python 2? Don't worry, we'll figure that out in the very next Nugget. So, I hope this has been informative for you, and I'd like to thank you for viewing.

Linux Setup

0:00[MUSIC PLAYING] Hey, guys, and welcome back. So in this Nugget, what we're going to be discussing is how we can set up a programming environment within a Linux system. Now, I'm also going to be walking through how we can set up our environment for a Mac OS system as well as a Windows system. But I really would advise that you use a Linux system because so many of the tools with a network automation are tailored to be run on Linux.

0:36Now, Mac OS is also a Unix-based system so it's pretty close to just using Linux. So you could probably get away with using Mac OS but ultimately, if you can, I really would stress to try to use a Linux environment. So as you can probably tell from the top of the screen, this is not actually a native installation on the baremetal of my computer. Instead, this is just a Linux virtual machine. But quite frankly, this is a really great way to use your automation in my opinion, at least for lobbying, because virtual machines are easy to snapshot and roll back, so on and so forth, they're very, very to manage.

1:12And like I said, you get the benefit of a full Linux system. So what I'm going to do here is to walk you through the basic installation, which we need to do to get our Python environment up and running. So let me just log in to this Ubuntu machine. Now, this is an absolute fresh installation as virtual machines. There's nothing on this at all. OK, so we've now logged in. What I'm going to do it as just escape here. Hit on whatever that, click Next, and Next, Next and this is all done now. So the very first thing which I'm going to do is go to the bottom left of the corner here, click this.

1:45And what I'll search for is terminal and to Enter. So now we have a Linux terminal, let me just full-screen this. And what I'll do here to make us a little bit more readable is go to Preferences, I'll go to Unnamed, and then I'll go into Custom Font, click on this and it'll just increase the font size. It's a little bit easier for you to see. So I'll say 18 that's nice and large, click Select. OK. Now the version of Python that we are going to be using within this course is going to be Python 3.9 so let's get this installed on our system, so what I'm going to say is 'sudo apt - add' and I'll say 'repository' and do a space and then say 'ppa : deadsnakes' which is a strange name, and then we'll do 'ppa' OK, perfect.

2:30So what I need to do is to type in my sudo password, which is my administrator password OK and I'll just hit Enter. OK, so forget that's a lot of money to install here. Now what I'm going to say is 'sudo apt install Python 3.main' and then hit Enter. Say 'Y' for yes, hit Enter again and that should now install Python 3.9. OK, so let's clear the screen. So if we say 'Python 3.9 - - version' hit Enter, you can see that we have Python 3.9 and this Python 3.9.6 we've actually got here.

3:04Now what we want to do is to install something called an IDE, this is an Integrated Development Environment. Basically, this is a program designed for code development. In real terms it's like a text editor, but it's designed for codes. You'll get syntax highlighting top completion and many, many more features. Now, the IDE which I'm going to recommend, you install as one called VS code, Visual Studio code. And the way we can get this is say 'sudo snap install' and we'll say '- - classic code' hit Enter and that should begin installing.

3:39Just give this maybe a minute or so. And as simple as that, Virtual Studio code has now been installed. So what we can do here is just type the word 'code' and thus should launch Virtual Studio code within Linux. And from there if we just full-screen this and we just double click here at the site, that should open up a new file. And if we click Select Your Language, what I'm going to do is select Python so let's click Python. And down the bottom here, that's going to offer us do we want to install the recommended extensions for Python, so we'll just say Install.

4:11And you'll notice that it says here that it's installing. Now here's the thing, it says 'No Python interpreter is selected.' You need to select one. So what we'll do here is click Select Python Interpreter, and what we are going to do is select Python 3.9.6 here so click on this. And now if you just reload the window, let me just zoom in a little bit here, you can see that down the bottom left here we're actually now running Python 3.9.6. Perfect, so we now have Visual Studio code installed within Ubuntu and we have Python 3.9 running with a nut.

4:44Now the next thing that we are going to install is something called Git. You may have heard of the website called GitHub. Well, we will be using Git in conjunction with GitHub as a way to manage your code and we'll see more of that later on within our scale. But for now, I'm going to have to install Git, so I'll say 'sudo apt install' and I'll say 'git' and then hit Enter, and that's going to install. If I just see 'Y' for yes to accept, hit Enter again, perfect. And if we now say 'git - - version,' Perfect. So Git has been installed.

5:15Now, just for posterity, if you want to know what Ubuntu Image I'm actually using, let me just show you that. If we go to our browser and let's go to Google, and if I just search for download Ubuntu and click the top link here, I'll accept all. This is the version which I'm running, Ubuntu 20.04.2.0 LTS. LTS is the stable version, that's Long Term Support. So this is the version I recommend that you download, either you put it onto baremetal onto your laptop directly or your PC, or you do as I've done here is download this image and create a virtual machine.

5:52Now down here, that's going to tell you the recommended system requirements to download this operating system. The main point here is 4 Gig RAM, that's the minimum you want. That or more. And again 25 Gig of free hard drive space. Once again, you really probably want a little bit more than that given that we won't be installed until throughout this course. But ultimately, the good news is that if you can get as far as this, then you are ready to begin programming with Python and Linux Ubuntu. Let's see how we can do similar setups on different operating systems.

6:25And that's what we'll be doing next. I hope this has been informative for you, and I'd like to thank you for viewing.

macOS Setup

0:12Hey, guys, and welcome back. So in the previous Nugget, we had just walked through a very basic set up of our Linux environment using Ubuntu. In this Nugget right here, we're going to be focusing on setting up our environment for a Mac OS. So let's walk through the installation of first Python, and then we'll go through the installation of VS Code then. So what I'm going to do here is go to my Safari browser down here and just open this. And let me just fullscreen this. So what I'll search for is download Python 3.9 Mac OS.

0:44So if I click on this top line here, download Python. So clearly here, we can see the link for download Python 3.9.6. Now depending on when you're watching us, this might not be the latest link. So if you want, you can just scroll on down a little bit here. And you can look for a specific release. So we had 3.9.6, so there is 3.9.6, and we've also got say, for example, 3.9.5. But as long as you've got 3.9, you should be pretty OK. OK, so we can get down to 3.9.6. If I click Download here and scroll on down-- OK, so I'm going to choose Mac OS 64-bit Intel installer, because I have an Intel machine.

1:20Let me just click this. Do I want to allow downloads? I'll say allow. And it's just going to download at the top right, you can see up here. Perfect. So if I click on this link here and I just right click and Show In Finder. Now that's going to open this up here. So all I'm going to do here is double click this, and it tells me it's going to install Python 3.9.6 for Mac OS. Let me just click Continue here. Now you want to read through all of the stuff, wink wink, that nobody actually ever does, and just click Continue OK, history and license, same again.

1:50Click Continue. And I'm going to click Agree. So finally, again, you can choose the installation location if you want to. I'm just going to leave it at the default, and click Install. So now I need to enter my password, so I'll just pop this in. And I'll choose Install Software. So now it's preparing to install. It's going to access my files and my downloads folder, I'll say OK. Now it's going to write these files. And you should give this maybe a minute or two just to finalize and complete. OK, perfect. So if we just click on this Python launcher here, it's going to show us where our actual interpreter is.

2:25So it's in user local bin Python 3. So now what we want to do is to install VS Code. So again, let's go back to Safari. Let's go to Google. And we'll search and download VS Code for Mac. So if we click this top link up here and I choose this link here for Mac, course. We'll say Allow. Again, at the top right here, we can see that we are downloading. So again, give this a little minute just to finalize. OK. So if I click Show Downloads, again, let's right click this here. And Show In Finder.

2:55OK, so let's just double click on this to unzip it. That's going to expand this out. Give it a little second. So if I just right click this and choose Open, that should now launch VS Code. OK, so it's going to verify. We're going to say Open. And now we have VS Code installed. Let me just fullscreen this temporarily. I'll just zoom in a little bit. OK, so I'll just double click up here to open up a file. And again, I'll choose Select A Language. I'm going to search for Python once again. Now again, it's going to say, do you want to install the recommended extensions for Python?

3:27We're going to say install here. So that's now installed. Now it's going to prompt you to install these command line developer tools. Now, I'm actually using a virtual machine. This is not a native installation of Mac OS. When I try to install these tools, it's just giving me an error. You should not have the same type of problem. So if you go on and just choose Install, there should install just fine. But for now, what I'm going to focus on is selecting the Python interpreter. So let me go over here and select this. I'll just cancel you. So this is the path which I want to select here.

3:59That's the path that we saw earlier, user local bin Python 3. That's just going to be a Python that we want. And we can see it in the bottom left here, we have Python 3.9.6. Perfect, so let's just close this down. Now, like I say, because I'm actually operating on a virtual environment, I'm a little bit limited in what I can demonstrate here. I'm trying my best. But ultimately, if you have a regular Mac OS system, then you shouldn't be experiencing the issues I have. Effectively, if you want to be able to install Git, you should be able to install Git view brew, the brew package manager.

4:29So if you just type in brew install git and hit Enter, that should run through the installation. And hopefully, you should all Git up and running. But again, I do stress that I recommend everyone instead uses an Ubuntu machine, either a regular machine or a virtual machine, since that's really what'll give us parity across what I'm doing on the lesson and what you're doing watching at home. But nevertheless, if you want to use a Mac machine, this is how you can get Python installed, VS code. And like I say, by using brew, you should be able to install Git just as well.

5:01Okeydoke, so I hope this has been informative for you, and I'd like to thank you for viewing.

Windows Setup

0:00[MUSIC PLAYING] Hey, guys and welcome back. So in the previous Nuggets, we had seen how we could install our environment within a Linux environment, as well as within Mac OS. And this Nugget right here, we're going to be focusing on using Windows. Now, I will say, off the bat, is that Windows as the operating system which I use-- I have Windows 10.

0:31But here's the thing, I'm really going to strongly recommend that you don't use Windows for your network automation. Simply put, there are just far too many network automation libraries that are just not going to work on Windows. You're going to have to use a Mac or a Linux environment. But here's the thing-- you can actually set up your environment in such a way that you're kind of using Windows, but logging remotely into a Ubuntu virtual machine. Now this is the setup that I actually use myself. So I want to walk you through it, because I do think [INAUDIBLE] needs compromise.

1:02So the plan is here is we're going to create an Ubuntu VM. And from our host machine, we are going to be able to log in remotely and manage it from our Windows system. Now, like I say, this is the preferred method in which I use, but just be aware that actually there's another solution that you could use called Windows Subsystem for Linux, otherwise known as WSL. This effectively does a similar thing. It gives you an Ubuntu terminal within your Windows machine. Now, the reason why I'm not recommending you do this type of installation is not necessarily because it's bad-- in fact, it's really good.

1:38The issue is that once the newer version, WSL2, because if you want to use nested virtualization within VMware-- say, for example, you want to automate topologies using Cisco's CMLP, which is their virtualization product allowing you to create virtual networks, well, you can't run VMware with nested virtualization at the same time as WSL2, because WSL2 requires Hyper-V. Now you can find workarounds. That's where you can use the old version of WSL, just WSL1. But honestly, I find that the cleanest way to do this is just to install the Ubuntu VM, and log in remotely.

2:14That way we're going to have no problems at all running VMware with any type of nested virtualization within that, whether we're using GNS3, whether we're using EVE-NG, whether we're using CMLP. These are all technologies that we can use to create virtual topologies, topologies which we can practice automating. So let me just walk you through the actual set up that I have within my environment here. OK, so the first thing we're going to do, as you can see here, is go to Google. And we'll search for download Ubuntu, as we've seen before. Let's actually walk through this installation then.

2:45OK, so let's Accept All and visit site. And what I'm going to do is to download this version here, 20.04.2.0. We're looking for, long-term support. That is a stable build. So download this, and just click Save, and now that should begin downloading for you-- that's ISO file. OK, so that is now completed downloading, I'm going to right-click, and then choose Open Containing Folder. And here's SSMH here. So I'll just cut out, and I'm going to go to my D drive. And I've created this folder called Example-Ubuntu. Let me just paste this image in here so it's nice and clear of what it is.

3:18OK, perfect. So now let's go to VMware. And what I'll do is create a new virtual machine here. Now, where do I want to install? Let me browse to this location. So I'll go into example-Ubuntu. Click this image here that I've just downloaded and choose Open. Next what I'll choose is Next. And then I'll just fill in some details. So I'll just say my name as ipvzero. Username, just keep it as ipvzero. And the password will be super secure. It'll be password123. So again, confirm-- password123.

3:48And choose Next. Now it's going to tell me the location where it's going to save this virtual machine. By default it's going to install on the C drive, though my C drive happens to be running a little bit low so I'm going to move it to the D drive. So I'll Browse here, go into my D drive, and I'll just go down to my Example-Ubuntu. So the same folder where my actual ISO resides. So choose OK, and then I'll say Next. In fact, I'll go back and I'll just change the name of the machine. I'll just call this Example Ubuntu Machine. I'll choose Next.

4:18Maximum desk size, let's make it 30. And we'll leave it as split virtual desk into multiple files. And we'll choose Next. If you want to add in more things, like, say, for example, crank the RAM up. You can customize the hardware, and you can maybe move the RAM up to whatever you want. I'll move it up to six. Network adapter is going to be NAT. Processors, I'm going to leave at two. So that will do us. So what I'm going to do now here is just click Finish, and this is going to run through the installation pretty much itself. If you give it maybe, say, 10, 15, 20 minutes, depending on how fast your machine is, this virtual machine should pretty much be set up for you.

4:52So what I'll do right now is pause the video and come back when we're ready to rock. OK, so now Ubuntu should be installed for you. What we're going to do now is to install VS Code within our Windows host machine. So let's just minimize this. Let's go back to Google, and we'll just search for Virtual Studio Code. OK, so let's click on Download here, and we'll choose Windows this time. So let's save this. So the setup is now installed. Let's just open this. Double-click it. And we're going to accept the agreement. Click Next. And again, choose the installation of where you want the program to be installed, or just leave it at the default. Click Next.

5:25We'll leave the folder as Virtual Studio Code. Click Next once again. And what I'll also do is create a desktop icon, and let's select these "Open with Code" actions, and just click Next. And then just choose Install. So that should begin extracting the files, and the installation should complete pretty shortly. OK, so we'll make sure at launch Virtual Studio Code has been ticked, and click Finish. OK, so my Visual Studio Code installation is going to look a little bit different from yours. That's because my old settings have actually been cached. So when I reinstall Visual Studio Code, my old settings just come back to me just like that.

5:59But really, that is not a problem at all. What you're going to do here is we'll go to File, then Preferences, and then Extensions. Now, as you can see, I've already got some things installed. So if I clicked Python and click on this here, you can see that has already been installed. But if it hasn't for you, just click the Install button. My option here is Uninstall. So you would just choose this, but the thing I really want to point out to you is something called Remote SSH. So let's just choose this, and it tells us right here we can open any folder on a remote machine using SSH and take advantage of VS Code's full feature set.

6:34That means that we can just run our development environment live on our Windows machine. So this is a Windows installation of VS Code, and we can use this Windows VS Code installation to log in to a remote machine, i.e. the Ubuntu VM that we had just created. And we can do this using SSH. So like I say, it already says I have mine installed here because I don't have the installation option. I've only got Disable and Uninstall. If you haven't already installed this, then one of these buttons will say Install.

7:05All we need to do is just click that button and wait a little second, and Remote SSH will be available for you. OK, so let me show you how we can use this Windows installation of VS Code to connect to this remote Ubuntu machine then. So let's go to our Ubuntu machine. OK, so I'll just log in here with my password, which was password123 I believe-- again, super secure as per usual. OK, now just like we saw with the Linux installation, let's just skip through this part here. So what we're going to do is go to our terminal.

7:36OK, now unlike the Linux installation, we're not going to be installing VS Code on this machine, but what we do need to have is to have SSH enabled, so let's just make sure we can log in via SSH. So what I'm going to say first is sudo apt updates, then I'll type in my password, password123. Hit Enter. OK, now what I want to say is sudo apt install ssh-server. Hit Enter. Oh, and that's a mistake. I should actually say openssh-server. Apologies about that. Try again.

8:06That was a little bit better. Push Y. Hit Enter. And just give this a minute or so just to install. OK, so now that we have SSH enabled on the VM, what we want to be able to do is to log in to this machine, so we need the IP address of this machine. So we'll say ip addr. And hit Enter. And we can see the IP address right here. So it's 192-168-31-146. And my username is ipvzero. So we want to login as ipvzero to this address, and we want to do that from the Windows installation of VS Code.

8:40So let's go back to our Visual Studio Code then. Now what I'm going to do is on the left-hand side here, we have this option here saying Remote Explorer. Let's click on this. Now again, I already have these cached connections because of my old installation. It's just came back to me, effectively. What you want to do here is go up to Remote Explorer, and we're going to choose SSH targets. Now again, I've got some previous targets saved. You won't have them; don't worry about it at all. What we're going to do is click the plus sign here, and we're going to add in your new host.

9:10So I'm going to say it as SSH and then the name of the user. So my username on the Ubuntu machine was ipvzero, and the IP address we found out was 192-168-31-146. So we'll hit Enter. OK, select the configuration file. Just choose this one here. So now the host has been added. Let's right-click this and Connect to Host in New Window here. So let's open this up, and it's going to say select the platform. We're going to choose Linux. It's telling us the fingerprint up here.

9:41We'll just accept this by saying Continue. Now we need to enter the password, and my password for that VM was password123. Hit Enter. And at the bottom right, it says setting up host. It's installing VS Code server. OK, great, so this looks good. So if we look down here-- let me just move this over and drag this-- down in the bottom, we actually have a terminal here. And this is the terminal we've effectively logged in. What's happened right now is we're working within our Windows environment. This is the Windows installation of VS Code. But we also have access to this terminal of that VM, of that Ubuntu machine.

10:16So say, for example, if I just create a folder just saying Testfolder, and I cd into this test folder. And I create a file just called thisisatestfile.txt. And I just nano this, and I just say, just testing that this works. Push control autosave, Control X. So now if I actually minimize this, and I go to my actual VM-- look at this, let me clear the screen. If I do an ls, see that? We now have this new test folder that's been created remotely from the Windows machine.

10:48So we can see the end to this, ls. There's that file, and we can cat this file, and there we go. So this means that we can work within our Windows machine. Just open up VS Code as per normal, and we can remotely install things and activate things and run code from within this Ubuntu virtual machine. We don't have to worry about any type of clashing between Windows and these new network automation tools. We're going to bypass that completely. Everything is going to be installed, even though we are effectively operating from a Windows environment-- super, super cool, super, super handy.

11:22So this is the actual setup, which I use personally. It's what I'm going to recommend that you use. And for the record, you could do the exact same thing on Mac OS. You could just create a virtual machine within Mac OS using VirtualBox or VMware. Install VS Code within your Mac. And again, use that remote SSH extension to just log in to the Ubuntu VM. This means that you and I are using the exact same operating system. It's going to minimize any type of clash and any type of weird little errors or issues. This will make things as easy as possible, in my opinion.

11:54Okey doke, so I hope this has been informative for you, and I'd like to thank you for viewing.

Useful Extensions

0:00[MUSIC PLAYING] Hey, guys, and welcome back. So in the previous Nugget, we had just walked through the installation of our Windows setup and how we're going to integrate an Ubuntu VM, a remote machine, into this Windows environment. And to do that, we installed the Remote SSH extension.

0:30Similarly, we also installed the Python extension within VS Code. This gave us access to things like Pylance, which is going to help us out with things like auto-completion. Now, because we are ultimately working towards an end goal of automating networks, what I want to do is just highlight some of just a few little extensions I use pretty regularly to help me do just that. So outside the Python extension, outside the open SSH extension, the next extension I want to get here is one for Cisco IOS.

1:02Basically, it is a way to get nice syntax highlighting, which is going to be pretty useful for us. So again, like I said in the previous Nugget, my old extensions and my old settings have already been cached. They've already been installed. You won't have them, but let me show you how you can get them. So let's go up to File here, and then choose Preferences, and then Extensions. Now, you can probably see it's already installed. But what I'm going to search for is Cisco IOS syntax and click this here. So as you can see here, it is already installed for me.

1:32I can only disable or uninstall it. If you haven't got it installed, the option will simply be to install. You just click it, and then it's going to install. Now, once you've installed this little plugin here, basically if you take any file and append it with the .ios extension, then we're going to get Cisco-specific color highlighting. That is going to be very, very useful when we're trying to create templates of our configuration. Now again, this might not make sense quite yet-- how do you templatize a configuration?

2:03Again, don't worry about that at all. We'll certainly get to that in due time. Just know that this type of thing is going to make your job a little bit easier in the future when we get to it. So let me just quickly show you how this works then. OK, so what I've got here is an EVE-NG topology. This is basically a network emulator you can use to build virtual networks. Same type of thing as GNS3 or Cisco's CML-P. Again, the details of this are not too important, but right here I have a virtual router.

2:33What I'm going to do here is just click on this to open it up. And what I'm going to do here is just issue a show run. So let me just full-screen this right now, and I'll scroll up to the top here. So let me just copy all this right here and scroll right down to the bottom. OK, so let's just copy here, and let's go to VS Code. And let me create a file called, config.txt. So what I'll do is go to File, click Open File, scroll on down, and go to config.txt. So if I just paste this in here, and I go up here and click Save.

3:05So look at this, here is the configuration here. Now, it looks quite fine. But as you can see, we have no highlighting at all. This isn't inherently a problem. But you'll realize, later on, when you're trying to templatize configurations, what you want to do is to eyeball commonalities amongst configurations. Basically, we'll be comparing different types of configuration to one another. And quite honestly, using highlighting is going to make this a lot easier as opposed to just looking at black and white text. So like I said, the extension here is .txt.

3:37But now that we have Cisco IOS syntax plugin installed, if we just change this extension to .ios, watch what happens. Let's save this file as config.ios and hit Enter. And look at this. We now get nice coloration through the configuration. Now, this makes things way, way, way easier when we're trying to compare across multiple configurations to try to derive commonalities. So that is the first plugin I'm going to suggest to you, on top of, of course, Python, as well as Remote SSH.

4:09Now, the other one is going to be-- if we go to Extensions, let's go to Preferences, and again, Extensions. And again, this is already going to be installed for me, but I'm going to search for something called Better Jinja. So I'm going to click on this. Again, as you can see here, this is already installed for me. For you, you can just click the Install button. And then within maybe 10 seconds or so, Better Jinja will also be installed for you. Now, it's very reasonable that you don't have a clue what Jinja is quite just yet. That's perfectly OK.

4:40We'll certainly get to that in depth in due time. All you need to know is that this is the templating language that we are going to be using to build our templatized configurations. And again, all this plugin does is it gives us nice syntax highlighting. So say, for example, if I go and create a new file here, and let me just save it as example2, and I use the .j2 extension, then we're going to get this nice highlighting. So if I just say things like-- and again, this won't make any sense for you-- for network in networks, you can see that we're actually getting different coloration for different keywords.

5:16And again, this doesn't have to make any sense at all to you just now. Just know that this extension is going to make your life a little bit easier when we come to this. So as we are setting things up, this is definitely an extension I would recommend that you install. And for now, really, that kind of does it. I would recommend you have the Python installation, which is going to give you things like Pylance, i.e. your auto-completion. We're going to install Remote SSH so we can remotely manage our Ubuntu server. We're going to install Better Jinja, which is going to give us nice syntax highlighting for our Jinja templating language.

5:52Again, don't worry about that. We'll get to it later. And fourth, we want to get Cisco IOS Syntax. This is going to give us an easier way to save our files with the extension, .ios, and get Cisco-specific syntax highlighting, making your job of templatizing your configurations with Jinja much, much easier. So go ahead and make sure these are all installed, and you will have a pretty solid environment to go about learning how to automate your networks with Python. So I hope this has been informative for you, and I'd like to thank you for viewing.

Cloning Code from Github

0:12Hey, guys. Now remember we talked about installing Git within our environments. Now if you're unsure of what Git is, then don't worry. We'll get to this later on in more detail within this course. But what you might have heard of, probably more likely, is GitHub. This is a large website on the internet where people store a lot of their codes. And this is where I'm going to be storing a lot of the examples that you're going to be seeing throughout this course. Basically, you're going to go to my GitHub repository and then clone what is called the Repository.

0:45Now the way we can transfer the code which I've saved onto to GitHub onto the local machine is to use Git. So Git is something we are using on the command line to get the code which is stored remotely on GitHub. Now as of right now, I don't really have any kind of code saved for this course at all. What's going to be happening is that I'm going to be slowly adding to this as we progress throughout this course skill by skill. But nevertheless, let's follow the process to see how we can do this. So what I'm going to do is to go to my terminal here.

1:17Now remember, this is actually a remote session through this new Ubuntu virtual machine. Now this virtual machine doesn't actually have Git installed. So let's quickly install it. So what we'll say is sudo apt update as we saw before. And hit Enter. Type in my password of password123. Hit Enter. And the update shall begin. And now what I'm going to do is install Git. So what I'll say is, sudo apt install git. And I'll say, yes. Hit Enter. And that will just run through the installation here.

1:47OK. So we know how to have Git installed within a remote Ubuntu virtual machine. What we want to do is to use Git now to pull back the information from the remote GitHub repository where I'm going to be storing all of these examples. So what I'll do here is go to my web browser. And this is just a browser on my normal Windows machine. And what I'll search for is ipvzero GitHub. This is my own GitHub here. Now the repository we want to go to is this one here, CodeSamples. Let's click on this link. And you see this big green button here.

2:17Let's click on this. If we highlight this here and then copy it, now this link has been copied. So let's take this to our Visual Studio code then. And let me just clear the screen. Now the way we can copy this is just by saying, if I'm doing ls, what you can see, the folder structure. Now when I say Git clone, and I paste this in and just hit Enter, this is going to clone the repository. And now we're going to have a new folder called CodeSamples. If we do an ls, look at this here. We now have CodeSamples.

2:49So if we cd to go enter this directory and we do an ls, you can see a bunch of different folders. Now the folder where I'm going to be storing the examples pertaining to this course is going to be in this one here, Intro_Python_Net_Eng. So if we cd Intro, this directory here, and I do an ls, for now I've only got one Python file. So what t can do here is just open this file here. So let's go into CodeSamples. We'll go into Intro_Python_Net_Eng. And let's open hello.py.

3:20And we have a really simple Python script here. We just have two print statements here, "welcome to CBT Nuggets. And have fun automating!" Now how to use Git and GitHub more in-depth, how you can store your own code remotely on GitHub, that is something we will certainly get to a little bit later within this course. But for now, just understanding how to install Git and to pull down example code from GitHub using git clone, this is going to provide you a really easy way to access all of the scripts we're going to be writing together throughout this course.

3:52And no just one more thing, just before I forget actually, remember we were talking about installing useful plug-ins, useful extensions, should I say. If I get down to Preferences here and go to Extensions-- this is certainly not mandatory, but I would recommend you install one called Monakai Pro. This is effectively the theme which I'm using. So like I said, mine is already installed. I would suggest you search for this installer and use this as your theme, the reason being, is that it's going to give you the exact same color highlighting as I have.

4:24So that's just totally optional. But it's going to make it a little bit easier for you to eyeball what you're writing on your screen and compare it to what you're seeing on my screen. So just what I remember there when I looked at that code, Monokai Pro is a really nice theme. And it probably will make your life a little bit easier just so that we have total parity. But for now, we have used Git to pull our GitHub repository down. And that is how you're going to be able to easily get copies of all the codes. And like I said, as we progress throughout this course, these examples are just going to keep growing and growing.

4:54So I hope this has been informative for you. And I'd like to thank you for viewing.

Cisco DevNet Sandboxes

0:00[MUSIC PLAYING] Hey, guys, and welcome back. So what I want to talk to you about in this Nugget is a really useful utility that you can use for free to be able to easily access Cisco devices that we can run our steps on as tests. What am I talking about? Well, I'm talking about the Cisco DevNet sandbox.

0:30Now, the really cool thing about the Cisco DevNet sandbox is that we do have two options. The first option is that we can book a topology, and this requires a VPN client to get your own private session. However, what we can also do, the second way, and this is what we are going to explore, is to check out the always-on sandboxes. Just like it sounds, these sandboxes are always on. You don't have to book them. You don't have to have a VPN. Simply put, an internet connection to access them is all you need.

1:01The only thing you have to realize here is that they are a shared resource, so there might be a little bit of limitations about what you can actually do. But generally speaking, these are going to be more than enough to give us access to some devices that we can test our Python scripts against. So let me show you how you can easily access these devices then. So if you search for "cisco devnet sandbox," and we click this top link here-- so I'm going to click Get Started with Sandbox. Now, to log in, you're going to have to use some type of account-- maybe a Google account or a Facebook account or a Cisco ID.

1:36What I'm going to do is to log in with my GitHub, so I'll just click this. So what I'm going to do is search for an IOS XE device. So if I just search for XE, if we look here, and let me just zoom in a little bit, you can see here we have an option to reserve some sandboxes. Now, when you see the Reserve button, these are the type of sandboxes you're going to have to book for a particular time, and you're going to have to log in with a VPN connection. But you can certainly explore this. If you want to have more access to a device, maybe you could have eight devices at once, but for ease of setup, what I'm going to recommend is that you just check out the always-on sandbox.

2:14How do we access this? Well, it's really quite simple. Let's check it out. Let's click on an always-on one. So down the left-hand side here, we're going to see the login details here. So here, as the login address here, this is basically the IP address. It's just like going to youtube.com. This is how we're going to access the sandbox, so if we want to access it over SSH, we're going to go over port 22. What's the user name? Developer. And the password login is going to be C1sco12345 with a one for the i here.

2:45So let's actually copy these details over and try to log into a device. So again, no VPN. Just an internet access. Let's copy this here. Right click, copy here, and let's go back to Visual Studio Code. So let me clear here, and in fact, let me just move this over a bit so it's a bit clearer. So I'll just say CD to take me back to my home directory, and I'll just say SSH, and the username was developer@, and I'll paste in the address. Hit Enter. Do I want to accept this fingerprint? I'll say, yes, and now, I'm going to type in that password of C-1-S-C-O-1-2-3-4-5, as we saw on the website.

3:22Hit Enter, and look at this. We have access to a Cisco device. We can do a show run, if I can type show run. There we go. So we can also do some configuration. So let's say conf t, and I'll create a loopback of loopback, let's maybe say 56. And I'll create an IP address of 56.56.56, and I'll make it slash 32 mask. And I'll also add a description on this as a test loopback. So if I do a show run once again, you can see that we have created this loopback on this device.

3:56So what we are going to do throughout this course is to use these always-on sandboxes, and we're going to try to configure changes and pull information using Python. And like I said, all it requires is an internet connection. Now, there are more than Cisco XE devices. We also have Cisco XR devices as well as Nexus devices, and throughout this course, we'll be using all of these sandboxes. So there you have it-- really easy access. Almost no setup. Just find the login details, have an internet connection, and off you go.

4:27So I hope this has been informative for you, and I'd like to thank you for viewing.

Running Our First Script

0:00[AUDIO LOGO] Hey, guys, and welcome back. So to round off the central skill of this course, how about we rate and execute our first Python script. OK, so let's create your first script then. So we'll go to File, click New File and we'll say Save As. And let's just put in our Home directory for simplicity, and let's just save this as example1.py.

0:34We want the py extension because it's going to be a Python file. And all I'm going to say is 'print' and then open my parentheses, and then within inverted commas, I'm going to say what I want to print to the screen. So if I say, "My name is John and I love Python," so if I just save this. And now I go back to my terminal here, we do an 'ls.' we now have this new file here called example1.py. So how on earth do we execute it? Well, it's quite simple. We just say python3 and then follow up with the name of the file.

1:08So we say example1.py. If we hit Enter, bam, there you go. You've just created and executed your very first Python script. So let's add a little bit more to it. We'll say 'print' and we'll do, "...And I also love automating networks." And let's save this. Let's clear the screen, arrow up, re-run the script, hit Enter. And bam, there we go. Not so scary is it, really? Now of course, there is a lot more to Python programming than what we just saw here.

1:38But I do believe that you're going to be pleasantly surprised that it's not quite the scary subject that you might have built it up to be in your own head. I know when I first started programming with Python, I for sure was a little bit intimidated. But don't worry, we're going to be taking things step by step, nice and slowly, and we will gradually build upon our skills so that with time and a little bit of practice, we will begin to be able to automate our networks effectively. Okedoke, so I hope this has been informative for you, and I would like to thank you for viewing.

Frequently asked questions

What does the Introduction to Python lesson in Python for Network Engineers cover?

The lesson explains why programs need explicit, literal instructions and what it means that Python is an interpreted, high-level language, then shows where network automation improves efficiency. Learners then set up a Python 3 environment on an Ubuntu Linux virtual machine, configure VS Code with Python 3 as the interpreter and useful extensions, and access Cisco DevNet always-on sandboxes for testing scripts.

Do network engineers need programming experience before starting this Python lesson?

No. The trainer states the goal is to take learners with no programming knowledge from ground zero to Python fundamentals, and the course is aimed at network administrators with 1 to 3 years of networking experience but little or no Python background. Engineers who already have some programming experience can use it to solidify that knowledge and apply it to the network.

Why does the lesson recommend an Ubuntu Linux VM instead of WSL2 on Windows for Python network automation?

Many network automation tools are tailored to run on Linux, and a VM is easy to snapshot and roll back. On Windows, WSL2 requires Hyper-V, which conflicts with running VMware nested virtualization for tools such as Cisco CML, so the trainer finds a remote Ubuntu VM the cleanest option, with WSL1 as a workaround.

How long is the Introduction to Python lesson and how does it fit into the full course?

This lesson runs about 59 minutes and is the starting point of a course that takes roughly 22 hours per learner. After the setup and fundamentals here, the course moves into data structures, functions, text files, network automation libraries, Jinja templates, REST APIs, spreadsheets, troubleshooting, testing, and concurrent connections.

What are Cisco DevNet always-on sandboxes and how are they used in this lesson?

Cisco DevNet sandboxes give learners access to Cisco devices for testing scripts. The lesson focuses on the always-on sandboxes, which do not need to be booked and do not require a VPN client, unlike reserved topologies, so team members can test automation without building their own lab first.

How can a Training Manager assign this course and track a network team's progress?

Training Managers can use CBT Nuggets Playlists to sequence this course with related automation training, and Team Reporting shows per-learner progress and completion, cohort and group views, and completion exports for audit evidence. Team Leads can use the course as a baseline before asking staff to contribute automation scripts in production environments.

Team training path

Turn this skill into assignable team training

This free skill is a preview of the courses your team can assign, track, and report on with CBT Nuggets.

What's next?

Ready to keep going?

For your team

Bring this training to your team

See how CBT Nuggets helps IT teams close skills gaps, hit compliance targets, and prove training ROI.

Book a Demo
Just need Python Network Automation?

Learning on your own? Browse individual plans ($49/month, billed annually) →

Not ready to buy?
with no purchase required. Already have an account?
Book a Demo