Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Practical Network Automation

You're reading from   Practical Network Automation Leverage the power of Python and Ansible to optimize your network

Arrow left icon
Product type Paperback
Published in Nov 2017
Publisher Packt
ISBN-13 9781788299466
Length 266 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Abhishek Ratan Abhishek Ratan
Author Profile Icon Abhishek Ratan
Abhishek Ratan
Arrow right icon
View More author details
Toc

Table of Contents (8) Chapters Close

Preface 1. Fundamental Concepts FREE CHAPTER 2. Python for Network Engineers 3. Accessing and Mining Data from Network 4. Web Framework for Automation Triggers 5. Ansible for Network Automation 6. Continuous Integration for Network Engineers 7. SDN Concepts in Network Automation

Language choices (Python/PowerShell)

Moving ahead, armed with the knowledge of how to write a program and an understanding best practices, we will now look at some scripting languages that suffice for our automation scripts. A basic difference between a scripting language and a programming language (such as C and C++) is that a scripting language is not compiled but interpreted through the underlying environment in which it is executed (in other words, a converter is required to convert the commands written in human-readable format to machine format by parsing one line at a time), whereas the programming language is primarily compiled and hence can be executed in multiple environments without the use of any specific underlying environment or requirements.

What this means is if I write a script in Python, PowerShell, or even Perl, I need to install that specific language in order to run the program or script that I have written. C or C++ code can be compiled to make an executable file (.exe) , and can run independently without the installation of any language. Additionally, a scripting language is less code-intensive, which means that it can automatically interpret some of the code written in a program depending on how it is called.

Let's consider an example. Here's how we declare a variable in scripting language:

x=5

OR

x="author"

OR

x=3.5

Whereas in a programming language, the same type of declaration would be made like this:

integer x=5
String x="author"
Float x=3.5

This states that depending on the value we assign to the variable, the variable type is automatically identified in an scripting language, whereas in a programming language the declarations are tightly controlled. In this case, if we declare a variable as a String, this clearly means that we cannot declare any other type of value in that variable unless we explicitly change the data type of that variable.

We have primarily three types of scripting language that are popular for creating programs and are mainly used for automation scripting or programming. These are Perl, Python, and PowerShell.

With support for the oldest language, Perl, diminishing, the focus is now on Python because of its open source support and on PowerShell because of its Microsoft, or .NET environment. Comparing both languages is not ideal because it's up to the reader which programming language they use to write their programs. As we have more than 70% of computers running Windows, and with a growing market of Microsoft Azure as a cloud operating system from Microsoft, PowerShell is the preferred language owing to the underlying .NET environment. As we create a program in PowerShell, it is easy to port that program and execute it on another machine running Windows without any special settings.

Python, on the other hand, is growing in popularity because of its open source approach. There are thousands of developers who contribute to enhancing Python by adding special functions for specific tasks. For example, there is a function or sub-program, called Paramiko, that is used to log into network routers. Another one is Netmiko, which is an enhanced version of Paramiko that is used to log into network devices based upon network hardware vendor and operating systems (such as Cisco iOS or Cisco NXOS). Python needs to be installed before writing a Python program and successfully executing it.

Going forward, our focus will be on Python, with additional tips and tricks on how to perform the same tasks using PowerShell instead of Python.

Writing your first program

Now, because we are starting from fresh, we need to understand how to write our first program and execute it. PowerShell comes pre-installed on a Windows machine. But we need to install Python by downloading it from the web ( https://www.python.org) and choosing the right version for your operating system. Once downloaded, it can installed just like any other application that is installed on a Windows machine.

On a Linux machine, the same holds true, but because of the .NET requirement, PowerShell will not be supported on Linux or Unix environments. Hence, if we are using a Unix or Linux environment, Python or Perl remain our preferences for scripting.

There are multiple Integrated Development Environments (IDEs) for both Python and PowerShell, but the default ones that come with these languages are also pretty helpful.

There are multiple versions of PowerShell and Python being used. When writing programs in higher versions, generally the backwards support is not very good, so make sure you note the users and environment before choosing a version.
In our case, we will be using PowerShell 4 and Python 3 onwards for writing programs. Some commands might not run in older versions of PowerShell and Python, and some syntax or commands are different in older versions.

PowerShell IDE

This can be invoked by clicking on the Start button and searching for Windows PowerShell ISE. Once invoked, the initial screen will look like this:

As we can see in the preceding screenshot, a PowerShell script is saved with a .ps1 extension. Once we write something in the IDE (or ISE, as it is called with PowerShell), it needs to be saved as somefilename.ps1 and then executed to see the result.

Let's take write a program called Hello World:

  • As we can see in our first program, we write two lines to print Hello World. In the ISE, we pass the commands to declare a variable (a variable is denoted by a dollar sign, $, in front of the variable in PowerShell), assigning the value Hello World to it. The next line is simply printing that variable by calling a method or function called Write-host, which is used to print values onscreen in PowerShell.
  • Once we write the program and save it, the next step is execution to see our result.
  • The green button at the top of the ISE is used to execute the script, and the result of the script is shown at the bottom of the screen. In this case, it was a simple Hello World output.

PowerShell scripts can also be invoked directly by the command line. As PowerShell is a scripting language and needs to be installed on a machine, we can directly call PowerShell from the Windows Command Prompt and execute the scripts and individual scripting commands from the PowerShell console itself.

This is how we can find out the version of PowerShell:

As we can see in the preceding screenshot, PowerShell is invoked by calling powershell directly from the Command Prompt in Windows. When PowerShell is invoked, we see PS before the command line, which confirms that we are now inside the PowerShell console. To see the version, we call a system variable, $psversiontable, which shows the version of PowerShell.

We can see that this is version 2.x (as shown in CLRVersion). System variables are special variables that have predefined values based upon the installation types. These special variables can be called at any time in our script to fetch their values and perform actions based upon the returned values.

The following example shows that we are using a higher version of PowerShell:

As we can see, the same variable now returns a value of 4.0 for PSVersion, which confirms that this is version 4 of PowerShell.

PowerShell 4.0 is the default installation from Windows 8.1 onwards on client operating system, and Windows Server 2012 R2 in a Server environment.

Python IDE

Similar to PowerShell, once Python is installed, it has its own IDE. It can be invoked by typing or calling IDLE (Python) from the Start menu:

The Python IDE, called IDLE, looks similar to the preceding screenshot when it is opened. The heading bar depicts the version of Python (which is 3.6.1 in this case) and the three greater than signs (>>>) show the command line, which is ready to accept Python commands and execute them. To write a program, we click on File | New File, which opens up a notepad in which we can write the program.

Lets see a similar hello world program in Python:

As we write a new program, the variable used is newvalue, and the value assigned to it is hello world. The next line is simply calling Python's print function to print the value inside the variable during the execution of the script.

Once we have written the program, we click on File | Save As in the window where we wrote the program, and save the script. The script is saved as filename.py, with the .py extension denoting a Python script. Once it is saved, we can press the F5 button on the keyboard or select Run | Run Module in the script window to run that specific script. The following window is the same window that was invoked when we first called the IDLE application from the Start menu, but now it has the output of that script that we wrote.

The output of hello world is now seen in the IDLE window. Once we are done with writing the script or Python commands, we can simply close the open command windows to close the application or Python interpreter.

Similar to PowerShell, we can also call python from the command line, as follows:

One additional thing to notice here is that to exit the Python interpreter, we call the exit() function. This tells Python to stop the execution and exit to the Command Prompt in Windows.

You have been reading a chapter from
Practical Network Automation
Published in: Nov 2017
Publisher: Packt
ISBN-13: 9781788299466
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime