Beginner python script to ssh into remote network device and run multiple comman

When working with network devices, it is often necessary to remotely access them and execute multiple commands. In Python, this can be achieved using various libraries and methods. In this article, we will explore three different approaches to solve this problem.

Approach 1: Paramiko Library

The Paramiko library is a popular choice for SSH connectivity in Python. It provides a high-level API for SSH communication and allows us to execute commands on remote devices.

import paramiko

# Establish SSH connection
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('remote_device_ip', username='username', password='password')

# Execute multiple commands
commands = ['command1', 'command2', 'command3']
for command in commands:
    stdin, stdout, stderr = ssh.exec_command(command)
    output = stdout.read().decode()
    print(output)

# Close SSH connection
ssh.close()

This approach uses the Paramiko library to establish an SSH connection with the remote device. We then iterate over a list of commands and execute them one by one. The output of each command is printed to the console. Finally, we close the SSH connection.

Approach 2: Fabric Library

Fabric is another powerful library for SSH automation in Python. It provides a higher-level interface compared to Paramiko and simplifies the process of executing commands on remote devices.

from fabric import Connection

# Establish SSH connection
c = Connection('username@remote_device_ip', connect_kwargs={'password': 'password'})

# Execute multiple commands
commands = ['command1', 'command2', 'command3']
for command in commands:
    result = c.run(command, hide=True)
    print(result.stdout)

# Close SSH connection
c.close()

In this approach, we use the Fabric library to establish an SSH connection with the remote device. We create a Connection object by specifying the username, remote device IP, and password. We then iterate over the list of commands and execute them using the run() method. The output of each command is printed to the console. Finally, we close the SSH connection.

Approach 3: Paramiko and Netmiko Combination

Netmiko is a multi-vendor library that builds on top of Paramiko and provides additional features for network automation. It simplifies the process of connecting to network devices and executing commands.

from netmiko import ConnectHandler

# Device details
device = {
    'device_type': 'cisco_ios',
    'ip': 'remote_device_ip',
    'username': 'username',
    'password': 'password',
}

# Establish SSH connection
ssh = ConnectHandler(**device)

# Execute multiple commands
commands = ['command1', 'command2', 'command3']
output = ssh.send_config_set(commands)
print(output)

# Close SSH connection
ssh.disconnect()

In this approach, we use both Paramiko and Netmiko libraries. We define the device details in a dictionary and establish an SSH connection using the ConnectHandler class from Netmiko. We then execute multiple commands using the send_config_set() method and print the output. Finally, we disconnect from the remote device.

After exploring these three approaches, it is evident that the third approach using Paramiko and Netmiko combination provides a more streamlined and simplified solution. It abstracts away the complexities of establishing an SSH connection and executing commands, making it easier to work with network devices. Therefore, the third option is the better choice for solving this Python question.

Rate this post

2 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents