Skip to content

Instantly share code, notes, and snippets.

@shinenelson
Created April 19, 2016 22:50
Show Gist options
  • Save shinenelson/436f022f735b1c3454a58925707b88de to your computer and use it in GitHub Desktop.
Save shinenelson/436f022f735b1c3454a58925707b88de to your computer and use it in GitHub Desktop.
Multiple destination SCP - Copy file from one location to multiple machines in one go
#!/bin/bash
# This bash script copies a file from a specified file path
# and places it in the same path on each of the specified hosts
# The script requires at least 3 arguments :
# - Filepath
# - Filename
# - Host(s) to copy the file
# The arguments should be passed in the order :
# file-path, file-name, host(s)
# Now, you must be wondering why do you need
# the filepath and filename as separate arguments
# and why not in one argument.
# Well, not everyone is like you who would be kind enough
# to pass the absolute paths of the file to copy.
# Some people would even pass relative paths,
# and that would obviously break the function of the script.
# Variables & Arguments
filepath=$1 # The variable name describes it already.
# But for the sake of it...
# This is the absolute path of the file.
filename=$2 # Same with this variable.
# Variable name describes it all...
# The name of the file to be copied.
file=$1/$2 # Concatenating the filepath and filename
hosts=${@:3} # This variable takes all the arguments
# starting from the third argument
# and copies the file securely
# to the specified path on all hosts.
# Verification of Input Arguments
# Checking whether there are enough arguements to perform the copy
if [[ $# -lt 3 ]]; then
echo "Hey, that's not fair!"
echo "You didn't provide enough arguments to perform this operation."
echo "Try again with at least the file path, name and one host to copy the file to"
echo "I'm taking a break for now. Bye."
exit 1
fi
# Verifying integrity of arguments passed
# Filepath integrity
if ! [[ -d $1 ]]; then
if [[ -f $1 ]]; then
echo "Hey, that's a file. You're supposed to specify the file's path first"
else
echo "Hey, that doesn't look like a filepath to me."
echo "You're supposed to specify the file's path first"
echo "Try again by specifying the file's path first."
echo "I'm taking a break for now. Bye."
exit 2
fi
fi
# Filename checking
if ! [[ -f $file ]]; then
if [[ -d $file ]]; then
echo "Hey, that's a directory. You're supposed to give me the file now."
else
echo "Hey, that doesn't look like a file to me."
echo "You're supposed to give me the file now."
echo "Try again by specifying the file's name second."
echo "I'm taking a break for now. Bye."
exit 3
fi
fi
# Hostname verification
# Frankly, I have no idea how to verify this
# because people can make up all kinds of hosts
# IPs (IPv4 or IPv6), virtual host entries, and what not
# I don't know how to verify all of that, so here's what I'm going to do :
echo "By the way, since I don't know how to verify what kind of hostname you've provided as arguments, I'm going to take your word and start copying. You better be right or I'll just get stuck and you'll have to clean up the mess"
# The actual stuff...
for host in $hosts; do
echo "Copying $file securely to $host"
scp $file $host:$filepath
done
# Just in case you didn't figure out yet, you cannot use this
# script to copy a file from a remote machine to your local
# even though scp supports it.
# The sole purpose of the script is to enable replication of
# the same file to multiple machines.
# If you want more, you should just use scp.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment