Webserver Management using SSH

November 22, 2008 by John Jenkins · Leave a Comment
Filed under: Tools 

My webserver permits access via ssh or ftp. I like to use ssh and scp for the additional security. I could use a graphical client to transfer my files to and from the webserver but I find the command-line more comfortable. However, the full command to transfer a file to the webserver is not easy to remember.

$ scp me_mysite@long.and.difficult.servername:~/

I create shortcuts to both scp (copy) and ssh (connect to remote server) that mean I only need to enter myscp filename mysite. The shortcut will also echo the password to the screen so I don’t need to remember it. To avoid security problems, I only interact with the webserver in my own home. When I am out and about, I develop using the local server. There is a way of setting up ssh so that you don’t need to enter a password at all and the server validates the client using a shared certificate but so far I have not needed to set this up.

I split my shortcuts and variables into seperate files.

.shell-fns

myscp()
{
    FILE=$1
    SITE=$2

    echo "Password is: $SSH_PASSWORD"
    scp $FILE ${SSH_USER}_${SITE}@${SSH_SERVER}:~/
}

myssh()
{
    SITE=$1

    echo "Password is: $SSH_PASSWORD"
    ssh ${SSH_USER}_${SITE}@${SSH_SERVER}
}

Update: I often find I want to transfer multiple files at the same time and it would be useful if I could do this with a single myscp command. I fixed the function to do this, but I’m not particularly happy with the method of extracting the parameters using sed. Perhaps there is a better way.

myscp()
{
    FILES=`echo "$@" | sed -e 's/  *\w\w* *$//'`
    SITE=`echo "$@" | sed -e 's/.*  *\(\w\w*\)$/\1/'`

    echo "Password is: $PASSWORD"
    scp $FILES ${USER}_${SITE}@${SSH_SERVER}:~/
}

.env

PACKAGES=/u/packages

PATH=$HOME/bin:$PACKAGES/bin:$PATH
PYTHONPATH=$PACKAGES/lib/python:$PYTHONPATH

HISTSIZE=10000
HISTFILESIZE=$HISTSIZE

PS1="`whoami`@`hostname` \$ "

export PATH PYTHONPATH HISTSIZE HISTFILESIZE

SSH_USER=not_my_real_user_name
SSH_PASSWORD=not_my_real_password
SSH_SERVER=very.long.and.difficult.to.remember.servername

Then add the following to the .bashrc file and it is ready

. $HOME/.env
. $HOME/.shell-fns

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!