I was trying to copy the SSH host keys from 10 user desktops to a remote server. So I figured I would write a small shell script to SCP the files to the directory without having to do a lot of manual work. The script is simple and it could use some more work, like adding a loop to go through the list of hosts instead of specifying each host manually.#!/bin/sh
# Define the system binaries. Of course, you may have to change it depending on the OS
CHMOD=/bin/chmod
MKDIR=/bin/mkdir
SCP=/usr/bin/scp
LS=/bin/ls
# Specify the directory where you want to create the host directory and store the keys. In my case I’m
# using /var/tmp
RDIR=/var/tmp/keys
# Specify the remote directory where the SSH keys are stored
LDIR=’/ssh/keys’
# Specify what files to get. In this case, anything that has the name host in it
LFILE=’*host*’
# Pass the user variable from the command line
USER=$1
# Pass the host variable from the command line
HOST=$2
# Create directory for the host
$MKDIR $RDIR/$HOST
# Change the directory permission (I needed this so it would be group writable)
$CHMOD -R 0775 $RDIR/$HOST
# Now copy the files from remote to the local directory
scp -p $USER@$HOST:$LDIR/$LFILE $RDIR/$HOST
That’s pretty much it. In my case, I had to specify the pass phrase each time the script would SCP to the host but that is okay. If you get really lazy, you can add your host key to the authorized_keys so you won’t be prompted. I like to specify the pass phrase due to security.
If you see anything wrong with the script, please let me know.