Run Linux commands on multiple hosts from a list.
Date: 2017-Nov-11
Author: ibm.ooty@gmail.com
Version: 0.1
This Python script is designed to run Linux SSH commands on a list of hosts and display outputs from all the hosts in the terminal. You can provide the list of host names to be checked in a plain CSV file with username and password.
For key-based authentication, install a key agent and import the required keys before running this script. In this case, you only need to provide the hostname and username in the input file.
This script extensively uses the pxssh module from the pexpect library. Ensure that this library is installed before using the script.
If you need to connect to only one system, you can use the -l option instead of creating an input file to specify the host, username, and password directly.
The input file should be kept under the input directory with the following format:
-
Password Authentication:
hostname,username,password -
Key-Based Authentication:
hostname,username
Each host's details (username and password) must be on a new line. Blank lines in the file are ignored.
For key-based authentication, if the key is not in the connecting host’s known_hosts file, it is added automatically. If this does not work, try connecting manually once and add the keys to known_hosts.
The script prints the output on the console and also saves it in the output directory. The filename of the output file follows the format:
YYYYMMDD-HHMMSS (Example: 20171101-152053)
Run a command on all hosts specified in an input file:
lcrun -i <input_file> 'command'Example usage:
lcrun -i hosts.csv 'uname -a'This connects to all hosts listed in hosts.csv, runs uname -a, and displays OS details.
lcrun -i hosts.csv 'uptime'Displays the uptime of all hosts in hosts.csv.
Connect to additional hosts:
lcrun -i hosts.csv -l localhost -u username -p password 'who'This connects to all hosts in hosts.csv, plus an additional host localhost, and displays the logged-in users.
lcrun -i <input_file> 'command'| Option | Description |
|---|---|
-h, --help |
Show help message and exit |
-i <input_file>, --inputs=INPUT_FILE |
Hosts file (each line: servername,username,password) |
-l <hostname>, --hostname=HOSTNAME |
Additional hostname (optional) |
-p <password>, --password=PASSWORD |
Password for additional host (used with -l, optional) |
-u <username>, --username=USERNAME |
Username for additional host (used with -l, optional) |
Ensure the following dependencies are installed before running the script:
pip install pexpectThis project is licensed under the MIT License.
Feel free to submit issues or contribute improvements via GitHub pull requests.