Python Script To SSH and Run Multiple Commands in Linux – Save hours in less than 80 lines

Python Script To ssh and Run Multiple Commands in Linux

Every Linux admin wants a python script to ssh and runs multiple commands in Linux. Here we will show and give a python script to run multiple commands on multiple servers and get output in excel.

Python Script To SSH and Run Multiple Commands in Linux - Save hours in less than 80 lines

To run commands by ssh over remote Linux servers python module used is as paramiko and by using paramiko we can run commands on remote Linux servers and even we get output and error messages. So let’s start with the script.

What is Paramiko?

Paramiko is a Python library that provides a means of securely and remotely connecting to and interacting with a device using the SSH (Secure Shell) protocol. It is often used for automating tasks, such as deploying software updates or gathering data from remote servers. Paramiko supports a number of authentication protocols and can be used to connect to a wide range of Linux servers, network switches, and routers. In short which devices your SSH to connect to paramiko is your best friend. It is a popular choice for network administrators and system engineers who need to automate tasks or access remote devices in a secure manner. Even If you are using Ansible without paramiko ansible is also useless because Ansible also uses the Paramiko library to connect to remote servers.

Python Script to SSH and Run Command

Python Script To SSH and Run Multiple Commands in Linux from explinux

Make sure you have all libraries installed on the server or in your Linux system. First, let’s start with preparation.

Preparation

  • Install pip if not in your machine. Check as per your Linux distro

for example rpm base system

yum install python-pip 
or
yum install python3-pip

for Debian based system

apt install python-pip
or
apt install python3-pip

You can install it according to your distro

  • Install the python package, and make sure you have internet active on the server
# pip install paramiko

The above python library is enough to connect remote server by ssh but if you want to export results in excel install openpyxl optional package

# pip install openpyxl

To support this python library mostly the time we face issues with the lxml python library because by default it has an older version installed on Linux.

# pip install --upgrade lxml

Finally, we have installed 3 python library

# pip install paramiko
# pip install openpyxl
# pip install --upgrade lxml

Start Scripting

Create a file with any name with .py extension here we will take the server_health.py file.

# vi server_health.py

We are showing commands for the Linux vi editor you can use your IDE or any editor.

For writing in the vi editor press “i” and to save data press the “esc” key then write in the lower left corner “:wq” and press enter to save and exit.

Related article:- Vi editor cheatsheet

Import Python Modules

First import python modules which are required in the script

import paramiko
from paramiko.ssh_exception import BadHostKeyException, AuthenticationException, SSHException, NoValidConnectionsError
import openpyxl
from os import path,rename

Now first we will write the script in a simple form then we will add a function to use multiple times.

Script Part

host = 'server1'
user = 'MyUserName'
password = 'MySecreteComplexPassword'
cmd = 'systemctl status sshd'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=host, username=user, password=password)
cmd=cmd
stdin, stdout, stderr = ssh.exec_command(cmd)
print(stdout.read())
print(stderr.read())
ssh.close()

Above script, we have defined 4 variables for the server host or hostname or IP, the user who is able to log in to the server, the password for the user, and cmd or command which we want to run on the remote server.

In the above script, we can see two outputs stdout which is the output of a successful command run, and stderr which is the output of an error in command. We are reading the output and printing them.

How to Run Multiple SSH Command On Remote Linux Machine

Our target is to run multiple commands on a Linux server for this we need to run through the python loop and we need a list of commands. Below is the script for a run multiple commands.

First, we need to convert our script to function and we need cmd list too

def getdata(host,user,password,cmd):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=host, username=user, password=password)
    cmd=cmd
    stdin, stdout, stderr = ssh.exec_command(cmd)
    #stdout=stdout.read().splitlines()
    ot = stdout.read()
    oterr = stderr.read()
    print("--stdout--")
    print(ot)
    print("--stderr--")
    print(oterr)
    print(type(ot))
    ssh.close()

We have converted a python simple script to a function to use in a python for loop with the list of multiple commands.

# variables 
host = 'server1'
user = 'MyUserName'
password = 'MySecreteComplexPassword'
command = ['systemctl status sshd','systemctl status rsyslog']

# get data from remote host
def getdata(host,user,password,cmd):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=host, username=user, password=password)
    cmd=cmd
    stdin, stdout, stderr = ssh.exec_command(cmd)
    #stdout=stdout.read().splitlines()
    ot = stdout.read()
    oterr = stderr.read()
    print("--stdout--")
    print(ot)
    print("--stderr--")
    print(oterr)
    print(type(ot))
    ssh.close()
 
# for loop with command list 
def cmdlist(command,host,user,password):
    if len(command) != 0:
        for cmd in command:
            getdata(host,user,password,cmd)
    else:
        print("Command length is 0 -ZERO. Please input atlease 1 command")

cmdlist(command,host,user,password)

Above script, we have changed the cmd variable to command and created a list of commands. And created a for loop with the list of commands and every time we loop we run the get data command which gives the output on each command. We are checking if the length of the command is zero then it will show us a warning for we need to input the command.

Here one question can rise why did not we use && or; to run commands in the same function, reason for that is in the loop data handling will be more complex if you run the command in that manner. But if you want to execute only and output data is not important then you can use && or “;”. For that case, you can add a command variable as below

cmd = 'systemctl status sshd rsyslog; nproc;free -m'

How to Run Multiple SSH Command On Multiple Remote Linux Machines

Now we will run multiple commands in multiple Linux machines for that we need to make our server variable into a list of variables and create a python for loop with the list of hosts or servers or machines.

# variables 
host = ['server1','server2','server3']
user = 'MyUserName'
password = 'MySecreteComplexPassword'
command = ['systemctl status sshd','systemctl status rsyslog']

# get data from remote host
def getdata(host,user,password,cmd):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=host, username=user, password=password)
    cmd=cmd
    stdin, stdout, stderr = ssh.exec_command(cmd)
    #stdout=stdout.read().splitlines()
    ot = stdout.read()
    oterr = stderr.read()
    print("--stdout--")
    print(ot)
    print("--stderr--")
    print(oterr)
    print(type(ot))
    ssh.close()

# for loop with command list and server list
def cmdlist(command,host,user,password):
    if len(command) != 0:
        for h in host:
            for cmd in command:
                getdata(h,user,password,cmd)
    else:
        print("Command length is 0 -ZERO. Please input atlease 1 command")

cmdlist(command,host,user,password)

In the above script, we have converted the variable host to a list of hosts. and created a loop for the host list. This will go to all hosts and for each host all command run and get us the output.

Export Paramiko Output to an Excel File

As we all know you can not skip Excel or spreadsheet in any job so before someone ask for it export directly to excel with the help of a script. We use the python openpyxl library to export in excel.

def getandcreatewb():
    heading = ("hostname","command","output","Error")
    if path.exists("Health_Result.xlsx"):
        rename("Health_Result.xlsx","Health_Result_old.xlsx")
    workbook = openpyxl.Workbook()
    workbook.create_sheet("result")
    print(workbook.get_sheet_names())
    fsh=workbook.get_sheet_by_name('Sheet')
    workbook.remove_sheet(fsh)
    sheet = workbook["result"]
    sheet.append(heading)
    workbook.save("Health_Result.xlsx")

In the above script we are creating an excel file with the name Health_Result.xlsx if that file exists it renames it to Health_Result_old.xlsx. If you have more space you can rename it with DateTime and create a sheet name result and append a heading “hostname, command, output, Error”.

Here is a complete python script to ssh and run multiple commands in Linux and export in excel.

# variables 
host = ['server1','server2','server3']
user = 'MyUserName'
password = 'MySecreteComplexPassword'
command = ['systemctl status sshd','systemctl status rsyslog']

def getandcreatewb():
    heading = ("hostname","command","output","Error")
    if path.exists("Health_Result.xlsx"):
        rename("Health_Result.xlsx","Health_Result_old.xlsx")
    workbook = openpyxl.Workbook()
    workbook.create_sheet("result")
    print(workbook.get_sheet_names())
    fsh=workbook.get_sheet_by_name('Sheet')
    print(fsh)
    workbook.remove_sheet(fsh)
    sheet = workbook["result"]
    sheet.append(heading)
    workbook.save("Health_Result.xlsx")

# get data from remote host and append to excel workbook
def getdata(host,user,password,cmd):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=host, username=user, password=password)
    cmd=cmd
    stdin, stdout, stderr = ssh.exec_command(cmd)
    ot = stdout.read()
    oterr = stderr.read()
    heading = ("hostname","command","output","Error")
    data = [(host,cmd,ot,oterr)]
    wb = openpyxl.load_workbook("Health_Result.xlsx")
    sheet = wb["result"]
    for d in data:
        sheet.append(d)
    wb.save("Health_Result.xlsx")
    ssh.close()


# for loop with command list and server list and create workbook
def cmdlist(command,host,user,password):
    if len(command) != 0:
        getandcreatewb()
        for h in host:
            for cmd in command:
                getdata(h,user,password,cmd)
    else:
        print("Command length is 0 -ZERO. Please input atlease 1 command")




cmdlist(command,host,user,password)

In the above script, we are calling create workbook function to our cmdlist loop function and appending our data in the paramiko get data function.

Conclusion

In this article, we have learned how to use paramiko to ssh and run multiple commands by the python script, and the best part we have exported it to excel. Next, we can use argparse to get input from the user and encrypt the password to use in the production environment.

Hope you find this article helpful, If yes share it with your friends.

Leave a Comment

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