☰ Categories

Linux: Bash Script to ssh to multiple servers run a command and the seprately print the output for each server

Prerequisite: must have ssh key authentication configured

  1. Create a serverlist.txt file with a line by line list of servers.
  2. Create a shell file with the following code(eg. servicechecker.sh:

!/bin/bash

for host in $(cat serverlist.txt); do ssh “$host” “systemctl status isecespd” >”output.$host”; done

  1. chmod 755 script-filename
  2. ./script-filename