Sunday, 13 August 2017

Remotely sending and executing files to Linux servers

1 - Create a file with a list of server names. For this example I will use /home/scott/servers.txt as my list. Each server name should be on a separate line

2 - Setup passwordless login to remote servers using SSH keys unless you want to keep typing in passwords

3 - Create this script for sending the file out to servers. I call this script SEND:

[ $# -lt 2 ] && { echo 'usage: SEND <file to send> <path at remote server>'
echo '  e.g. SEND file1 /home/test '
exit 0
}
echo "Send ${1} to ${2} all servers listed in /home/scott/servers.txt?    y/n "
read i
if test "$i" = y
        then
        for SERVER in `cat /home/scott/servers.txt`
        do
        echo "Sending to ${SERVER}"
        scp -p $1 $SERVER:$2
        done
exit 0
fi
echo 'Not sent!'



4 - Create this script for executing the file you have just sent. I call this script RCMD

[ $# -lt 1 ] && { echo 'usage: RCMD <command-to-run>'
echo ' e.g. RCMD l /home/test '
exit 0
}
echo "Run  ${1} ${2} ${3} ${4} on all servers listed in /home/scott/servers.txt?    y/n "
read i
if test "$i" = y
        then
        for SERVER in `cat /home/scott/servers.txt`
        do
        echo "Running at ${SERVER}"
        ssh $SERVER ${1} ${2} ${3} ${4}
        done
exit 0
fi
echo 'Not run!'




5 - For the file you want to send, run /Path/To/SEND/Script '/Path/To/File/You/Are/Sending' /Where/You/Want/To/Put/It/On/Remote/Server

6 - To execute the file you have just sent, run /Path/To/RCMD/Script '/Path/To/File/On/Remote/Server'

No comments:

Post a Comment