Skip to main content

shell script to run command in remote servers using ssh

 Hello Techie's,


what if, You have a single shell script whics helps to exexute any command on the remote servers ? 

Sound's Cool ?

Let's Say you want to check the Java version in your RHEL servers or you just want to know the contents of the particular directory or current backround process running on the server or any commands which comes to your mind, Below shell script is your one stop solution.

-------------------------------------------------------------------------------------------

#!/bin/bash
#This script helps to execute commands on multiple servers with the help of ssh
#List of servers - one server per line
SERVER_LIST=/home/directory/server
#option for the SSH commands
SSH_OPTIONS='-o ConnectTimeout=2'
#Displaying the usage of the script and exit messages
usage() {echo "usage: ${0} { -nsv } [-f FILE] CMD" >&2
echo "Execute CMD as a single command on every server" >&2 
echo "  -f FILE use FILE for the list of servers. Default ${SERVER_LIST}." >&2
echo "  -n Dry run mode, Given command will be displayed not executed." >&2
echo "  -s Run the command with sudo user privillege." >&2
echo "  -v Enables verbose mode to display the name of server in which cmd is executed." >&2
exit 1
}
#make sure the script is not excecuted with sudo user privilege.
if [[ "${UID}" -eq 0 ]]
then 
echo " Do not execute this script as root. use the option -s instead." >&2
usage
fi
#Parse the options
while getopts f:nsv OPTION
do
case ${OPTION} in
f) SERVER_LIST="${OPTARG}" ;;
n) DRY_RUN='true' ;;
s) SUDO='sudo' ;;
v) VERBOSE='true' ;;
?) usage ;;
esac
done
#Remove the options while leaving the remaining arguements.
shift "$(( OPTIND -1 ))"
#If the user not provided atleast one arguements. give them help.
if [[ "$#" -lt 1 ]]
then
usage
fi
#Anything that remains on the command line to be treated as single command.
COMMAND="${@}"
#Make sure the serverlist file exists.
if [[ ! -e "${SERVER_LIST}" ]]
then 
echo "cannot open server list file ${SERVER_LIST}." >&2
exit 1
fi
#If command failed in any server below condition helps
EXIT_STATUS='0'
#Loop through the server list
for SERVER in $(cat ${SERVER_LIST})
do 
if [[ "${VERBOSE}" = 'true' ]]
then
echo "${SERVER}"
fi
SSH_COMMAND="ssh ${SSH_OPTIONS} ${SERVER} ${SUDO} ${COMMAND}"
#if it is a dry run, do not execute anything, just echo it.
if [[ "${DRY_RUN}" = 'true' ]]
then
echo "DRY_RUN: ${SSH_COMMAND}"
else
${SSH_COMMAND}
SSH_EXIT_STATUS="${?}"
#capture non-zero exit status from the SSH_CMD and report to the user.
if [[ "${SSH_EXIT_STATUS}" -ne 0 ]]
then 
EXIT_STATUS="${SSH_EXIT_STATUS}"
echo "Execution on ${SERVER} failed." > &2
fi
fi
done
exit ${EXIT_STATUS}

----------------------------------------------------------------------------------------------------------------------------

Want to know The most useful linux commands ?

Comments

Popular posts from this blog

How to create a job in autosys using sample jil file

  Hello Techie's I know you are new to Autosys and want to know How to Create Job in Autosys using a jil file, Don't worry we are here with wonderful example and sample jil file. Before going into the sample jil, you need some script to run via Autosys. you can use the sample powershell script written in our blog  How to call stored procedure using powershell scripting using try and catch (vichietechie.blogspot.com)   or if you have your own script, you can use that as well. Problem Statement: 1. you need to Run particular script on sheduled time. 2. you need to know whether the script executed without errors. 2. If executed with errors the sheduled job should send notification. Prerequisties: 1. you must have CA Workload Automation Tool installed in your machine. 2. you must know the hostname & Uid of that machine. 3. you should have script to run in autosys. what is the difference between box and job in autosys ? A box is used to organize and control process flow of...

Curl command to check if file exists in s3 bucket

 Hello Techie's,  we can use below Shell script to check if particular file present in s3 bucket. # -------------------------------------------------------------------- #s3 Bucket Credentials #-------------------------------------------------------------------- s3_access_key=XXXXXXXXXXX s3_secret_key=XXXXXXXXXXX host=s3_bucket_api_url bucket=s3_bucket_name folder_name=name_of_the folder_in_bucket file_name=name_of_the_file_to_check #-------------------------------------------------------------------- #curl command variables #-------------------------------------------------------------------- dateValue=`date -R` contentType="application/xml" filepath="/${bucket}/${folder_name}/${file_name} signature_string="GET\n\${contentType}\n${dateValue}\n${filepath}" signature_hash=`echo -en ${signature_string} | openssl sha1 -hmac ${s3_secret_key} -binary | base64` #-------------------------------------------------------------------- #Curl command to check if file exi...

How to link image pull secret to service account in openshift

Hello Techie's, If your services are running in ECS - Openshift container platform and you need to edit the credentials of the Image pull secret, please follow the below steps. What is Image Pull Secret ? It is used to pull an image from a private container image registry or repository to the deployment. How does the yaml file of Image Pull secret looks ? apiVersion: v1 kind: Secret metadata:   ...   name: secret_name   ... data:   .dockerconfigjson: eyJodHRwczovL2luZGV4L ... J0QUl6RTIifX0= type: kubernetes.io/dockerconfigjson Problem Statement: Let's say, you are updating the existing Image Pull Secret values -  for example, changing docker credentials of your repository or updating the uid password from 8 character password to 15 character.  As you make changes in exising secret, Deployments or build which you trigger will obviously fail.  To overcome that you need to link the updated Image Pull secret to the service account which are using that secr...