Search code examples
linuxbashcopyhosthostname

Remote copy file to same directory to all hostnames


Am trying to write a distribute script to copy a file to the named hostname from the host file and goes into the same directory.

For example:

dist -hostname1 file.txt
dist -all file.txt

The -all should be able to distribute to all the hostnames on the network.

I know this can probably be done with a rcp command


Solution

  • You can use scp to do this. It uses ssh to securely copy the file. Nowadays rcp is just an alias for the scp command.

    user is the login on the remote machine.

    Copy file to host in /file-dest-dir

    scp file.txt user@host:/file-dest-dir
    

    A script to copy to a number of hosts:

    file=$1
    hosts="host1 host2 host3"
    for host in $hosts
    do
         scp "$1" user@${host}:
    done
    

    Get hosts from /etc/hosts

    hosts=$(awk '!/^#/ {print $2}' /etc/hosts)
    for host in $hosts
    do
         scp "$1" user@${host}:
    done