Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Python Network Programming Techniques
Python Network Programming Techniques

Python Network Programming Techniques: 50 real-world recipes to automate infrastructure networks and overcome networking challenges with Python

eBook
$35.98 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Python Network Programming Techniques

Chapter 2: Connecting to Network Devices via SSH Using Paramiko

When administrating IT devices from a remote location, be it network equipment or servers, SSH has become the standard. With its secure transport and various authentication methods, it's a safe choice that is widely used to this day to administer and configure servers or network devices. It is thus only natural when getting started with programmability and network automation, to find a way of issuing SSH commands not by hand but from a script. With this, you can take a sequence of commands you used to type into the device by hand and execute them programmatically on one or more devices. The last part is crucial. With a script that executes commands for you, you can easily apply the same sequence of commands to another device.

While we could implement the SSH protocol ourselves, this would be cumbersome work. Luckily, the Python community has already developed an SSH client library that is available for our use, called Paramiko.

In this chapter, we are going to learn the basics of programmatically connecting to a network device using SSH. We are going to use Cisco devices for our examples but the workflow is the same regardless of the vendor.

In this chapter, we will work through the following recipes:

  • Initiating an SSH session with Paramiko
  • Executing a command via SSH
  • Reading the output of an executed command
  • Executing the same command against multiple devices
  • Executing a sequence of commands
  • Using public/private keys for authentication
  • Loading local SSH configuration

Technical requirements

For this section and the remainder of the book, you'll need an installation of Python. Specifically, you'll need a Python interpreter of version 3.6.1 or higher. This book makes use of language constructs of Python 3 and thus is incompatible with Python 2.x. If you haven't done so already in the previous chapter, please go ahead and install the Paramiko package (python3 -m pip install paramiko). At the time of writing, we are using the latest version of Paramiko, version 2.7.1. You may install this exact version by issuing python3 -m pip install paramiko==2.7.1.

You also want a code editor. Popular choices include Microsoft Visual Studio Code or Notepad++. Additionally, you'll need a device (virtual or physical) that you can log into via SSH.

You can view this chapter's code in action here: https://bit.ly/37Ih46N

Initiating an SSH session with Paramiko

The basis of connecting to a device via SSH with Python and Paramiko is the SSHClient object of the library. We will use this object to create an initial connection to the SSH server and later we will use the functions of this object to execute commands on the device.

In this recipe, you will see how to programmatically open an SSH connection.

Getting ready

Open your code editor and start by creating a file called initiating.py. Next, navigate your terminal to the same directory that you just created the initiating.py file in.

How to do it...

Let's start by importing the Paramiko library and creating a client object. We will also specify the host, username, and password in variables and then initiate a connection to the specified host:

  1. Import the Paramiko library:
    from paramiko.client import SSHClient
  2. Specify the host, username, and password. You can name these variables however you like. In the Python community, it has become standard to uppercase these global variables. The three variables SSH_USER, SSH_PASSWORD, and SSH_HOST are variables of type string and we thus use double quotes to mark them. The SSH_PORT variable is an integer and thus does not use double quotes:
    SSH_USER = "<Insert your ssh user here>"
    SSH_PASSWORD = "<Insert your ssh password here>"
    SSH_HOST = "<Insert the IP/host of your device/server here>"
    SSH_PORT = 22 # Change this if your SSH port is different
  3. Create an SSHClient object, which we just imported from Paramiko:
    client = SSHClient()
  4. While we have created our client object, we have not yet connected to the device. We will use the connect method of the client object to do so. Before actually connecting, we will need to make sure that our client knows the host keys:
    client.load_system_host_keys()
    try:
        client.connect(SSH_HOST, port=SSH_PORT,
                                 username=SSH_USER,
                                 password=SSH_PASSWORD,
                                 look_for_keys=False)
        print("Connected successfully!")
    except Exception:
        print("Failed to establish connection.")
  5. Finally, we have created our connection. It is a good habit to close connections after we are done using them. To do so, we can use the close() function of our client:
    finally:
        client.close()
  6. To run this script, go to your terminal and execute it with the following:
    python3 initiating.py

How it works...

In this example, we first imported the Paramiko library's SSHClient class. Next, we set up our connection details. While you could also provide these details directly when calling the connect method on the client object, it is good practice to put them into variables. This means that if you are creating multiple client objects at different points of your script, you don't have to change the username/password and host in each of these calls but just once in the variables. Be careful when submitting these scripts to your colleagues or uploading them to code hosting services though, as they do contain your login details. You can have a look at the There's more section of this recipe to see how you can either prompt for this information interactively or get it from your environment.

Before connecting to the device, using the connect method, we load the host keys. SSH uses host keys and the fingerprints of an SSH server to make sure that the IP you are connecting to is the server you connected to before. When connecting to a brand-new device, you'll have to accept this new host key. While we can keep a separate set of host keys for our Paramiko client, it is usually best to just use the host keys that the user executing the script has. By doing this, you can connect to every device you have previously connected to from your command line, also from your Paramiko scripts.

To do this loading, we are using the load_system_host_keys() function. This function searches the default known hosts file used by OpenSSH and copies them over into Paramiko. See the following section for an example of how to make Paramiko accept new keys by default.

With our host keys configured, we can now actually connect to the device that we have specified. To do so, we are using a try-catch block. This is a Python construct that allows us to catch exceptions, errors that can be raised by any part of the code we are using, and handle them. Python will attempt to execute the instructions in the try block. If any part of that code, in our example the connect() method, errors out and raises an exception, Python will jump into the except block and execute the instructions within that block. In our example, we are only printing out a message that our connection was unsuccessful. The third block, our finally block, will be executed both when the connection has been successful (our except block was not executed) as well as after a failed connection (our except block was executed). This allows us to clean up after both a successful and unsuccessful connection and avoids dangling SSH connections.

There's more...

In this example, we relied on the user having already logged into the device from their command line in order for the host to be known. If we use the preceding code to connect to a device that was not previously known, the code will fail with an exception.

The way Paramiko handles unknown host keys can be specified using a policy. One of these policies, AutoAddPolicy, allows us to just add unknown host keys to our scripts set of host keys:

from paramiko.client import SSHClient, AutoAddPolicy
SSH_USER = "<Insert your ssh user here>"
SSH_PASSWORD = "<Insert your ssh password here>"
SSH_HOST = "<Insert the IP/host of your device/server here>"
SSH_PORT = 22 # Change this if your SSH port is different
client = SSHClient()
client.set_missing_host_key_policy(AutoAddPolicy())
client.connect(SSH_HOST, port=SSH_PORT,
                         username=SSH_USER,
                         password=SSH_PASSWORD)

The preceding code will automatically add these host keys. Be aware that this might be a potential security risk since you are not verifying that the host you are connecting to is the one you connected to last time.

In this example, we passed connection details such as username, hostname, and password directly as a variable in the script. While this is great for testing, you might want to have your script prompt you for a variable upon execution. For non-secret variables such as the username, host, and port, we can use the built-in input() function, but for passwords, it's better to use a dedicated password prompt that hides what you have typed so that someone looking over your console history can't retrieve your password. For this purpose, Python has the built-in getpass module.

Follow these steps to retrieve the configuration variables necessary, not as static information in the script but rather interactively from the user using a combination of input and the getpass module:

import getpass
SSH_PASSWORD = getpass.getpass(prompt='Password: ', stream=None)
SSH_USER = input("Username: ")
SSH_HOST = input("Host: ")
SSH_PORT = int(input("Port: "))

Executing a command via SSH

With our connection now open, we can go ahead and execute a command on our remote device. Similar to the way we deal with issuing commands on a remote device by hand, we have three different streams that come back to us: the standard out (or stdout), which is the normal output, the standard error (or stderr), which is the default stream for the system to return errors on, and the standard in (or stdin), which is the stream used to send text back into the executed command. This can be useful if, in your workflow, you would normally interact with the command line.

In this recipe, you will see how to programmatically open an SSH connection and then send a command of your choice to the device.

Getting ready

Open your code editor and start by creating a file called command.py. Next, navigate your terminal to the same directory that you just created the command.py file in.

How to do it...

Let's start by importing the Paramiko library and create a client object as seen in the last recipe. We'll then execute a single command of your choice on this device:

  1. Import the Paramiko library:
    from paramiko.client import SSHClient
  2. Specify the host, username, and password. You can name these variables however you like. In the Python community, it has become a standard to uppercase these global variables:
    SSH_USER = "<Insert your ssh user here>"
    SSH_PASSWORD = "<Insert your ssh password here>"
    SSH_HOST = "<Insert the IP/host of your device/server here>"
    SSH_PORT = 22 # Change this if your SSH port is different
  3. Create an SSHClient object, which we just imported from Paramiko:
    client = SSHClient()
  4. While we have created our client object, we have not yet connected to the device. We will use the connect method of the client object to do so. Before actually connecting, we will need to make sure that our client knows the host keys:
    client.load_system_host_keys()
    client.connect(SSH_HOST, port=SSH_PORT,
                             username=SSH_USER,
                             password=SSH_PASSWORD)
  5. Finally, we can use the client to execute a command. Executing a command will return three different file-like objects to us representing stdin, stdout, and stderr:
    CMD = "show ip interface brief" # You can issue any command you want
    stdin, stdout, stderr = client.exec_command(CMD)
    client.close()
  6. To run this script, go to your terminal and execute it with this:
    python3 command.py

How it works...

In this example, we first created a new client as seen in the previous example. We then used the exec_command() method to execute a command of our choice.

The function returns three different file-like objects for the three different streams: stdin, stdout, and stderr. In the next recipe, Reading the output of an executed command, we will use this to read back the output that was provided when executing a command.

Reading the output of an executed command

In the previous recipe, we saw how to first connect to a device and then execute a command. So far, we have ignored the output though.

In this recipe, you will see how to programmatically open an SSH connection, send a command, and then write the output of that command back to a file. We will use this to back up a running configuration.

Getting ready

Open your code editor and start by creating a file called read_out.py. Next, navigate your terminal to the same directory that you just created the read_out.py file in.

How to do it...

Let's start by importing the Paramiko library and create a client object as seen in the last recipe. Then execute a single command of your choice on this device and save the output:

  1. Import the Paramiko library:
    from paramiko.client import SSHClient
  2. Specify the host, username, and password. You can name these variables however you like. In the Python community, it has become a standard to uppercase these global variables:
    SSH_USER = "<Insert your ssh user here>"
    SSH_PASSWORD = "<Insert your ssh password here>"
    SSH_HOST = "<Insert the IP/host of your device/server here>"
    SSH_PORT = 22 # Change this if your SSH port is different
  3. Create an SSHClient object, which we just imported from Paramiko:
    client = SSHClient()
  4. While we have created our client object, we have not yet connected to the device. We will use the connect method of the client object to do so. Before actually connecting, we will need to make sure that our client knows the host keys:
    client.load_system_host_keys()
    client.connect(SSH_HOST, port=SSH_PORT,
                             username=SSH_USER,
                             password=SSH_PASSWORD)
  5. Finally, we can use the client to execute a command. Executing a command will return three different file-like objects to us representing stdin, stdout, and stderr:
    CMD = "show running-config" 
    stdin, stdout, stderr = client.exec_command(CMD)
  6. We will use the stdout object to retrieve what the command has returned:
    output = stdout.readlines()
  7. Next, we write the output back to a file:
    with open("backup.txt", "w") as out_file
        for line in output:
            out_file.write(line)
  8. To run this script, go to your terminal and execute it with this:
    python3 read_out.py

How it works...

In this example, we first created a new client as seen in the previous example. We then used the exec_command() method to execute a command of our choice. We then used the returned file-like object for the standard out to read everything that our remote device printed into the standard out and write it into a file.

A file-like object in the context of Python is an object that offers the same functions as a file object. When using the open() function to create a new file object to read/write from locally, we are offered some functions such as write() or readlines(). These functions allow us to read or write from or to a file. A file-like object offers the same functions and can thus be used as if it was a remote file. We use the readlines() function on the stdout object to read everything returned – in this example, the running configuration of our device – and write it line by line to a local file.

There's more...

If you prefer to just see the output of your command instead of writing it to a file, you can also use the following construct, instead of the code provided in step 7 of the recipe, to print out your entire configuration:

for line in output:
    print(line.strip())  

The strip() method used in the preceding example will delete any unnecessary leading or trailing whitespaces.

Executing the same command against multiple devices

In the previous recipes, we have always only dealt with a single device. Quite often we have a fleet of similar devices that we want to configure in unison.

In this recipe, you will see how to programmatically open an SSH connection to multiple devices, issue the same command to all of them, and then save the output. We will again use this example to back up the running configuration of multiple devices.

Getting ready

Open your code editor and start by creating a file called exec_multiple.py. Next, navigate your terminal to the same directory that you just created the exec_multiple.py file in. Additionally, create a file called credentials.json. We will use this file to retrieve credentials such as the username and password of our devices.

How to do it...

Let's start by creating our credentials file. We will then read that file from our Python script, create clients for each of these devices, and finally execute a command while also saving the output back to our file:

  1. Import the required libraries, Paramiko and json:
    import json
    from paramiko.client import SSHClient
  2. Open up the credentials.json file and provide the credentials to your device(s) in the format shown in the following code. You can specify as many devices as you want:
    [
     {
       "name": "<insert a unique name of your device>",
       "host": "<insert the host of your device>",
       "username": "<insert the username>",
       "password": "<insert the password",
       "port": 22
     },
     {
       "name": "<insert a unique name of your device>",
       "host": "<insert the host of your device>",
       "username": "<insert the username>",
       "password": "<insert the password",
       "port": 22
     }
    ]
  3. Go back to your exec_multiple.py file. We will now open the JSON file in our Python script:
    credentials = {}
    with open("credentials.json") as fh:
         json.load(fh)
  4. Create a variable holding the command you want to execute. We will then loop over all the devices specified in our credentials.json file and create an SSH client object. Additionally, we will create an individual output file for each of our devices based on the name we specified in the JSON file:
    CMD = "show running-config"
    for cred in credentials:
         out_file_name = str(cred['name']) + ".txt"
         client = SSHClient()
         client.load_system_host_keys()
         client.connect(SSH_HOST, port=cred['port'],
                                  username=cred['username'],
                                  password=cred['password'])
         stdin, stdout, stderr = client.exec_command(CMD)
         out_file = open(out_file_name, "w")
         output = stdout.readlines()
         for line in output:
              out_file.write(line)
         out_file.close()
         client.close()
         print("Executed command on " + cred['name'])
  5. To run this script, go to your terminal and execute it with this:
    python3 exec_multiple.py

How it works...

In this example, we first create a JSON file containing the credentials and connection details for all of our devices. We can view this file as a type of inventory. In general, it is good practice to keep this information separate from the Python code that is acting upon it.

We then use the built-in json module to read the credentials file into a list of dictionaries that we can loop over.

Based on the credentials specified in the credentials file, we then open up a new Paramiko connection, execute the command, and write the output to a new log file, that is, by including the name we set for our device in the JSON file, which is unique for each of the devices.

With this, we can back up the running configuration of an entire fleet of devices from one simple script.

Executing a sequence of commands

In the previous recipes, we have always only dealt with a single command that we wanted to execute. Maybe you have tried adding another call to the exec_command() function to the client already and have run into an error telling you that the session is closed. It is indeed correct that, once your command is done executing, the connection will close.

But quite often we don't want to execute only one but a sequence of commands one after another. We could reconnect for each of the commands, but this is a workaround that would create a lot of unneeded disconnecting and reconnecting. What we can do instead is, if the target device's configuration allows it, open up a shell session. This shell session is a single command, and we can then use stdin, stderr, and stdout to send multiple commands in the same session.

In this recipe, you will see how to programmatically open an SSH connection to a device, open a shell, and then send a list of commands to the device before closing the connection.

Getting ready

Open your code editor and start by creating a file called exec_multiple_commands.py. Next, navigate your terminal to the same directory that you just created the exec_multiple_commands.py file in.

How to do it...

Let's start by creating our credentials file. We will then read that file from our Python script, create clients for each of these devices, and finally execute a command while also saving the output back to our file:

  1. Import the Paramiko library. We will also need the built-in time library:
    from paramiko.client import SSHClient
    import time
  2. Specify the host, username, and password. You can name these variables however you like. In the Python community, it has become a standard to uppercase these global variables:
    SSH_USER = "<Insert your ssh user here>"
    SSH_PASSWORD = "<Insert your ssh password here>"
    SSH_HOST = "<Insert the IP/host of your device/server here>"
    SSH_PORT = 22 # Change this if your SSH port is different
  3. Create an SSHClient object, which we just imported from Paramiko:
    client = SSHClient()
  4. While we have created our client object, we have not yet connected to the device. We will use the connect method of the client object to do so. Before actually connecting, we will need to make sure that our client knows the host keys:
    client.load_system_host_keys()
    client.connect(SSH_HOST, port=SSH_PORT,
                             username=SSH_USER,
                             password=SSH_PASSWORD)
  5. Open up an interactive shell session and a channel that we can use to retrieve the output:
    channel = client.get_transport().open_session()
    shell = channel.invoke_shell()
  6. Next, specify the list of commands we want to execute on the device:
    commands = [
         "configure terminal",
         "hostname test"
    ]
  7. Iterate over each of the commands, execute them, and then wait for 2 seconds:
    for cmd in commands:
         shell.send(cmd + "\n")
          out = shell.recv(1024)
          print(out)
         time.sleep(1)
  8. Finally, we need to close the connection:
    client.close()
  9. To run this script, go to your terminal and execute it with this:
    python3 exec_multiple_commands.py

How it works...

In this example, we use Paramiko's concept of an interactive shell to send multiple commands to the remote device one after another.

When invoking a shell with Paramiko, we will retrieve a channel. This channel was used by Paramiko in the background all along to execute our commands but has been hidden from us so far. The channel takes care of low-level aspects such as sending and receiving the data to and from the raw network connection to our device. The channel function we use in this example is the send() function, which sends a string to the remote device. Mind the carriage return we added to the command. The same way you, when connecting to a device via SSH, have to execute a command by typing enter, the interactive session has to indicate that via a linebreak, which is the same symbol sent by your interactive session when hitting the Enter key.

We are using the sleep function here to wait for the commands you have passed to have finished executing. For short-running commands, you can get away with not using this sleep function.

Using public/private keys for authentication

So far, we have always used a username/password combination to connect to our device. This is not the most secure way, however, and many security policies advocate using public-private key pairs instead of a static password.

In this recipe, you will see how to programmatically open an SSH connection using a password-protected private key.

Getting ready

Open your code editor and start by creating a file called key_file.py. Next, navigate your terminal to the same directory that you just created the key_file.py file in.

You'll also need a password-protected private key for the device/server you are trying to connect to and have the device/server configured to allow or require key-based logins.

How to do it...

Let's start by importing the required libraries, define our new connection details, and finally open up a connection using key-based authentication:

  1. Import the Paramiko library:
    from paramiko.client import SSHClient
  2. Specify the host and username. You can name these variables however you like. In the Python community, it has become a standard to uppercase these global variables. Instead of the device password, we will now need two new variables – the path to the private key file that we want to use to authenticate and the password for that private key file:
    SSH_USER = "<Insert your ssh user here>"
    SSH_HOST = "<Insert the IP/host of your device/server here>"
    SSH_PORT = 22 # Change this if your SSH port is different
    SSH_KEY = "<Insert the name of your private key here>"
    SSH_KEY_PASSWORD = "<Insert the password here>"
  3. Create an SSHClient object, which we just imported from Paramiko:
    client = SSHClient()
  4. While we have created our client object, we have not yet connected to the device. We will use the connect method of the client object to do so. Before actually connecting, we will still need to make sure that our client knows the host keys:
    client.load_system_host_keys()
    client.connect(SSH_HOST, port=SSH_PORT,
                             username=SSH_USER,
                             look_for_keys=True,
                             key_filename=SSH_KEY,
                             passphrase=SSH_KEY_PASSWORD)
  5. As seen before, we can now execute a command once the connection is established:
    stdin, stdout, stderr = client.exec_command('<your command>')
  6. Finally, we need to close the connection:
    client.close()
  7. To run this script, go to your terminal and execute it with this:
    python3 key_file.py

How it works...

In this example, we use Paramiko's ability to load RSA keys for authentication to avoid using a username/password combination for authentication.

We need to import the same packages we have used before. The difference lies in the parameters that we pass to the connect function. Instead of specifying a password, we specify the name of our ssh key. The library will then, as indicated by setting the look_for_keys flag to true, search common places such as ~/.ssh for keys and match them with the name provided. The passphrase argument is used to provide the passphrase used to decode the private key. If your private key does not have a passphrase, you can omit this argument.

Once the connection is established, we can use the client in the same way as we did before, when dealing with username-password authentication.

There's more...

In the preceding example, we relied on the key file being present in one of the known paths. Sometimes you might want to explicitly specify the path you are loading a key file from. You can do so by, instead of just specifying the filename in the key_filename attribute, specifying the entire path where Paramiko can find your private key.

For example, if your private key is in /home/user/my_keys/id_rsa, you could modify the preceding example like so:

SSH_KEY = "/home/user/my_keys/id_rsa"

If you want to connect to different devices and have multiple keys, one for each device, you can also pass a list of key names or paths to ssh keys to the key_filename attribute:

SSH_KEY = [
           "/home/user/my_keys/device_1",
           "/home/user/my_keys/device_2",
           "/home/user/my_keys/device_3"
]

Paramiko will then try out all the keys in the provided list for each device you are connecting to.

Loading local SSH configuration

When dealing with multiple different devices and connecting to them via SSH, it can be convenient to specify information such as the hostname, port, username, or identity file to use in a specific configuration file. The OpenSSH implementation stores this file in a file called config in the .ssh directory in your home directory (~/.ssh/config on macOS and Linux).

While we could copy and paste this information into our Python scripts or try to write a parsing function for the format ourselves, it is easier and more convenient to use Paramiko's SSH configuration parser.

In this recipe, you will see how to programmatically parse your SSHConfig file, extract the relevant information based on a host, and store it in a dictionary.

Getting ready

Open your code editor and start by creating a file called parse_config.py. Next, navigate your terminal to the same directory that you just created the parse_config.py file in.

You'll also need an SSH config file for the device you are trying to connect to. In this example, we will be using a config file that has the following content:

Host example
     Host <insert your host address here>
     User <insert your user here>
     Port <insert the port here>
     IdentityFile <insert the path to your private key here>

How to do it...

Let's start by importing the required libraries and defining the path to our SSH configuration:

  1. Import the Paramiko library:
    from paramiko.client import SSHClient
    from paramiko import SSHConfig
  2. Specify the path to your SSH config file and the name of your host as it appears in your SSH configuration (example in this snippet). We will populate all the other variables from the configuration we are reading:
    SSH_CONFIG = "<insert path to ssh config here>"
    SSH_HOST = "example"
  3. Create an SSHConfig object, which we just imported from Paramiko, and create a local file object with the path to our SSH configuration:
    config = SSHConfig()
    config_file = open(SSH_CONFIG)
  4. Next, we need to tell the SSHConfig object to load and parse the configuration file:
    config.parse(config_file)
  5. With the config parsed, we can now do a lookup on this configuration object to extract all information stored in the configuration itself. The lookup function will return a dictionary:
    dev_config = config.lookup(SSH_HOST)
  6. With our device configuration extracted from the SSH config we can go ahead and fill our connection details with what we have extracted from the SSH configuration file:
    client.load_system_host_keys()
    HOST = dev_config['hostname'],
    client.connect(HOST, port=int(dev_config['port']),
                         username=dev_config['user'],
                         key_filename=dev_config['identityfile'])
  7. With the connection established, we can do all the different things we discovered in previous recipes before finally closing the connection:
    client.close()
  8. To run this script, go to your terminal and execute it with this:
    python3 parse_config.py

How it works...

In this example, we use Paramiko's ability to parse an SSH configuration file to not define this information in multiple different locations.

We start by importing the SSHConfig class, in addition to the already established SSHClient class. Instead of manually specifying the host, username, and key file information, we now create a local file object that points to our SSH configuration.

With that file opened, we can now have Paramiko parse this configuration. The SSHConfig object now contains all the different information for each of the hosts. We can then do a lookup on our host – in this recipe, the host is called example – and extract all configuration variables that are known in the configuration file.

From that, we proceed to providing that information to the SSHClient. Instead of statically specifying it, we just access it from the dictionary that was returned by Paramiko when doing the lookup on the host.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Explore different Python packages to automate your infrastructure
  • Leverage AWS APIs and the Python library Boto3 to administer your public cloud network efficiently
  • Get started with infrastructure automation by enhancing your network programming knowledge

Description

Network automation offers a powerful new way of changing your infrastructure network. Gone are the days of manually logging on to different devices to type the same configuration commands over and over again. With this book, you'll find out how you can automate your network infrastructure using Python. You'll get started on your network automation journey with a hands-on introduction to the network programming basics to complement your infrastructure knowledge. You'll learn how to tackle different aspects of network automation using Python programming and a variety of open source libraries. In the book, you'll learn everything from templating, testing, and deploying your configuration on a device-by-device basis to using high-level REST APIs to manage your cloud-based infrastructure. Finally, you'll see how to automate network security with Cisco’s Firepower APIs. By the end of this Python network programming book, you'll have not only gained a holistic overview of the different methods to automate the configuration and maintenance of network devices, but also learned how to automate simple to complex networking tasks and overcome common network programming challenges.

Who is this book for?

This book is for network engineers who want to make the most of Python to automate their infrastructure. A basic understanding of Python programming and common networking principles is necessary.

What you will learn

  • Programmatically connect to network devices using SSH (secure shell) to execute commands
  • Create complex configuration templates using Python
  • Manage multi-vendor or multi-device environments using network controller APIs or unified interfaces
  • Use model-driven programmability to retrieve and change device configurations
  • Discover how to automate post modification network infrastructure tests
  • Automate your network security using Python and Firepower APIs
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Oct 08, 2021
Length: 340 pages
Edition : 1st
Language : English
ISBN-13 : 9781838646639
Languages :
Concepts :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Publication date : Oct 08, 2021
Length: 340 pages
Edition : 1st
Language : English
ISBN-13 : 9781838646639
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $105.95 $117.97 $12.02 saved
Python Network Programming Techniques
$48.99
Learn Python Programming, 3rd edition
$46.99
Linux for Networking Professionals
$48.99
Total $105.95$117.97 $12.02 saved Stars icon

Table of Contents

13 Chapters
Chapter 1: A Primer on Python 3 Chevron down icon Chevron up icon
Chapter 2: Connecting to Network Devices via SSH Using Paramiko Chevron down icon Chevron up icon
Chapter 3: Building Configuration Templates Using Jinja2 Chevron down icon Chevron up icon
Chapter 4: Configuring Network Devices Using Netmiko Chevron down icon Chevron up icon
Chapter 5: Model-Driven Programmability with NETCONF and ncclient Chevron down icon Chevron up icon
Chapter 6: Automating Complex Multi-Vendor Networks with NAPALM Chevron down icon Chevron up icon
Chapter 7: Automating Your Network Tests and Deployments with pyATS and Genie Chevron down icon Chevron up icon
Chapter 8: Configuring Devices Using RESTCONF and requests Chevron down icon Chevron up icon
Chapter 9: Consuming Controllers and High-Level Networking APIs with requests Chevron down icon Chevron up icon
Chapter 10: Incorporating your Python Scripts into an Existing Workflow by Writing Custom Ansible Modules Chevron down icon Chevron up icon
Chapter 11: Automating AWS Cloud Networking Infrastructure Using the AWS Python SDK Chevron down icon Chevron up icon
Chapter 12: Automating your Network Security Using Python and the Firepower APIs Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(5 Ratings)
5 star 60%
4 star 20%
3 star 0%
2 star 0%
1 star 20%
katrina Nov 10, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book does a good job of introducing beginner and intermediate Python, but goes on to really shine when it covers popular and lesser-known network tools using Python: using SSH with Paramiko, Jinja 2, Netmiko, Netconf, ncclient, and more. Almost every chapter has sections that start with "how to do it" and "how it works". Clearly the author has been a network engineer and is sharing their extensive knowledge.
Amazon Verified review Amazon
Amazon Customer Oct 27, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I'm the technical reviewer of this book. This book covers all the basic and advanced topics about Netdevops, most of the scripts in this books are run and demonstrated on Cisco devices.You may find out that you are already familiar with some of the chapters in this book such as Paramiko and Netmiko as they are the most famous and most popular modules for network engineers who already have some basic knowledge about Python. Besides that, Marcel did a great job introducing the powerful but less well-known Netdevops tools such as Jinja2, NETCONF, RESTCONF, pyATS/Genie, EC2 (for AWS Cloud Networking automation) and Firepower API.I highly recommend this book to network engineers who want to develop the necessary skills and maintain a cutting edge in this Netdevops era, no matter if you are a newbie or seasoned network engineer.
Amazon Verified review Amazon
rockstar82 Nov 21, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The book is dedicated to CISCO network engineers that need to know Python to deliver certain milestones. The book is also great for new devops, if they do not necessarily have a lot of experience with network programming (though I would assume that's not the case anymore.The book starts gently with a primer on Python which can be easily skipped if you are already versatile in Python. The primer is well done, but it would but it is recommended that you run at least some of the code snippets provided so you get the gist of how Python works.Once the introduction is out of the way, the book delves straight into networks programming topics like connecting via SSH to other devices with Paramiko, Jinja 2 configuration or network devices configuration with Netmiko, netconf config, etc. The approach for these chapters is very hands-on, code-focused, therefore more technical, as opposed to the images heavy approach of the early technical books on the subject. It is assumed you have some knowledge about networks. If you do not have that knowledge, it would be best to get it from another book.The last two thirds of the books are getting into the meat of the subject with complex configs with NAPALSM, automated test deployments with Genie(especially import for devops), configuring devices with a REST interface, consuming high-level networking APIs with requests, custom Ansible modules, AWS infrastructure, etc. This section of the book will help you decide if you want to continue being a network engineer or if you want to take on a more traditional development role like devops (well, yes the devops as a job exists for a decade, but such jobs of maintaining and running online systems existed for decades in some capacity).Overall a great book that can fill in your missing knowledge or help you decide which career path you would like to follow!
Amazon Verified review Amazon
jml Oct 21, 2021
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
A cookbook-style reference, Python Network Programming Techniques provides the reader with introductory material, step-by-step instructions, and an explanatory section for each “recipe”. The book walks the reader through a Python 3 refresher, then moves on to basic connectivity via the Paramiko SSH library; a detour through Jinja (which is used later on) is followed by a very logical progression through Netmiko, NETCONF, Napalm, and the HTTP/REST-based engines. Ansible and cloud services via AWS are also covered in easily-followed procedures. The explanatory material is more than sufficent to ensure that even readers with little or no experience can get up and running with the tools.Engineers wanting to script configuration and maintenance tasks will find the book useful right from the start. There are some hidden gems here; a nice explanation of Python variadic arguments (for those who were a little weak in that area), great coverage of the oddly-named YANG (Yet Another Next Generation) modeling language standard, and some quick-and-dirty tricks with pyATS / Genie that will come in handy for many routine tasks.The book tends to be rather Cisco-specific in places, and if you’re going to follow along, you will need some resources to play with. For the first few chapters, a Raspberry PI and a low-end Cisco router will suffice, but later on an AWS account, Meraki, Ansible, and Firepower will be necessary. A couple of the examples may leave you scratching your head a bit due to minor typos and such but figuring those out ends up being good practice.All in all, Python Network Programming Techniques nicely fills an important gap between the Python and network-management worlds. Those wanting to automate their network configuration and management tasks will find it a readable, practical, and quite useful resource.
Amazon Verified review Amazon
wyzarddoc Feb 22, 2023
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
Code is very incomplete Author is not aware of their subject or how to write Python code.wasteof me
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the digital copy I get with my Print order? Chevron down icon Chevron up icon

When you buy any Print edition of our Books, you can redeem (for free) the eBook edition of the Print Book you’ve purchased. This gives you instant access to your book when you make an order via PDF, EPUB or our online Reader experience.

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela