
Target:
If you have a jump-server with connection to you target hosts, it is better to do the same job (command) via script. The easiest way if you prepare a script on your jump-server which run you command on all of your host. The script’s output (result of command on remote host) will be logged in give path / file what you configured on script & it is visible also on stdout.
Script:
#!/bin/bash
USER=$1
TIMEOUT=$2
REM_COMMAND=$3
HOST_SOURCE=$4
LOG_DESTINATION=$5
if [ "$LOG_DESTINATION" == "" ] ; then
echo "Please use it with the following --> ./remote_command.sh [pmikaczo] [5] [command_with_spaces] [source] [log_destination]
- All command with this script will be run as root!
- Please be aware to run un-tested commands!
- You have to be in sudoers.
[pmikaczo] --> <username to SSH in to host>
[5] --> <timeout value in SEC> If host is not reachable after this time skip host and go to the next-one.
[command_with_spaces] --> <Command which will run on remote host>
[source] --> <source where from read hosts>
[log_destination] --> <log filename where to log>
HINT: You can declare more command & command with space if you put it inside quotation-marks."
exit
fi
#password="your_password" #If you enable this line, comment out /echo -e "Enter your password: "/stty -echo/read password/ lines
export GREP_COLOR='1;31'
echo -e "Enter your password: "
stty -echo
read password
export password
stty echo
while read line
do
echo "------------------------------------------------------------"
echo "$line"
timeout $TIMEOUT sshpass -p $password ssh -oStrictHostKeyChecking=no -T -l $USER $line << SSH
echo "Server: \$(hostname)"
echo $password | script -q -c "sudo -s $REM_COMMAND"
SSH
done < $HOST_SOURCE | tee -a $LOG_DESTINATION
exit
Explanation of script:
Required as in here you declare that you will use bash. If you write KornShell script, in here you will declare “ksh”
USER=$1
TIMEOUT=$2
REM_COMMAND=$3
HOST_SOURCE=$4
LOG_DESTINATION=$5
Define variables what we will invoke during script & assign these to positional parameter.
if [ "$LOG_DESTINATION" == "" ] ; then
echo "Please use it with the following --> ./remote_command.sh pmikaczo 5 command_with_spaces source log_destination
- All command with this script will be run as root!
- Please be aware to run un-tested commands!
- You have to be in sudoers.
[pmikaczo] --> <username to SSH in to host>
[5] --> <timeout value in SEC> If host is not reachable after this time skip host and go to the next-one.
[command_with_spaces] --> <Command which will run on remote host>
[source] --> <source where from read hosts>
[log_destination] --> <log filename where to log>
HINT: You can declare more command & command with space if you put it inside quotation-marks."
exit
fi
If our last positional parameter is empty (so not configured correctly) script prints out the basic usage of it & exit from script.
#password="your_password" #If you enable this line, comment out /echo -e "Enter your password: "/stty -echo/read password/ lines
export GREP_COLOR='1;31'
echo -e "Enter your password: "
stty -echo
read password
export password
stty echo
Password section of script.
At the moment script is configured to ask for password when you start to run it. It is the secure-way to run the script as the password will be stored in protected way, but in this mode user-interact needed (type password). The another way when you would like to run it fully automatically.
password="your_password" #If you enable this line, comment out /echo -e "Enter your password: "/stty -echo/read password/ lines
export GREP_COLOR='1;31'
#echo -e "Enter your password: "
#stty -echo
#read password
export password
stty echo
You can configure it to fully automatically (like when you would like to use it as a cron-job). In this mode the biggest disadvantage that you password will be store it in plain-text on your script file.
while read line
do
echo "------------------------------------------------------------"
echo "$line"
Start of the while loop. These command run on the host where you started the script.
timeout $TIMEOUT sshpass -p $password ssh -oStrictHostKeyChecking=no -T -l $USER $line << SSH
Command to ssh to the remote host. Timeout was used as in your list one of the host not answer, the script continue running.
echo "Server: \$(hostname)"
echo $password | script -q -c "sudo -s $REM_COMMAND"
SSH
Command which will be run on remote host(s) as root. Command will be used from “$REM_COMMAND” variable, which is the “$3” positional parameter.
done < $HOST_SOURCE | tee -a $LOG_DESTINATION
Close while loop which’s source is “$HOST_SOURCE” ($4) & log it to “$LOG_DESTINATION”
Usage:
To run it. you must to add executable-bit for the file & must tbe add five parameter for it. If you don’t know, which order, just start the script without parameters, it will print out the usage of it.
The source-file’ format:
test-host01
192.168.1.145
my-host
If you would like to use “/etc/hosts” as source, please change SSH-command line from :
timeout $TIMEOUT sshpass -p $password ssh -oStrictHostKeyChecking=no -T -l $USER $line << SSH
to:
timeout $TIMEOUT sshpass -p $password ssh -oStrictHostKeyChecking=no -T -l $USER $(echo $line | awk '{print $1}') << SSH
Like this:
Like Loading...