remote server execution

Execute commands at remote server(s), the easy way :)

Logging in to remote servers to run just one (or a few) commands always annoyed me in some way; it usually involves a lot of typing.
So it would be a lot easier if it could look like you are working on your local machine, without having to use these lengthy commands. So I decided to make a little script.

Normal way to run a remote command is for example:

lxer@local:~$ ssh lxer@server1 -p 88 ls -l

I got a bit tired of typing this every time I just needed to look for a file. So, an alias (in .bashrc) could make my live easier. But then I noticed that there is also not really a short way for copying files t a remote server. Using scp or ssh is just not an easy way for doing so.

lxer@local:~$ scp -P 88 file lxer@server1:file2
lxer@local:~$ ssh -p 88 lx@server1 -p 88 cat file " file2

What I wanted my script to do is basically 3 things with simple commands: 1. login 2. copy a file, or 3. execute remote commands

The name of the script is the servername (server1)
simply log in to server:

lxer@local:~$ server1

copy file to server1:
lxer@local:~$ server1 -c

copy file to server1 and rename to :
lxer@local:~$ server1 -c -t

execute command at server1, and returning to shell: lxer@local:~$ server1

(Some more often used commands will be added in the future.)

This script is created in Python but uses some Bash commands. Bash doesn't handle the order of commandline-arguments very well, so I had to rewrite this to python.

#!/usr/bin/python2.5

import os, sys, optparse, string
##############################
server="server1"
port="88"
user = 'lxer'
##############################
sshSt= "ssh "+user+"@"+server + " -p "+port

def remCommand():
clist = string.join(sys.argv[1:]) # arguments to commands list
os.system(sshStr +" "+ clist) # execute commands

parser = optparse.OptionParser()
if len(sys.argv) > 1:
if sys.argv[1].startswith('-'): # options
parser.add_option('-c', '--copy', # copy file to server
dest="copy",
default=False,
)
parser.add_option('-t', '--target',#rename file after copy
dest="target",
default=False,
)

options, remainder = parser.parse_args()

if options.copy != False :
sourcefile = options.copy
if options.target != False :
target = options.target
comm = sshStr + " cat < "+ sourcefile +" \">\" " + target
os.system(comm)
else:
target = sourcefile
comm = sshStr + " cat < "+ sourcefile +" \">\" " + target
os.system(comm)

else:
remCommand()
else:
remCommand()

Now put this script somewhere in your path (like /usr/bin/), rename it to your (remote)servername, and make it executable (chmod 755).

Your remote server will still ask for your SSH password. If you want to avoid this, simply copy your .ssh/id_rsa.pub to your server:

lxer@local:~$ server1 -c .ssh/id_rsa.pub
lxer@local:~$ server1
lxer@server1:~$ cat id_rsa.pub >> .ssh/known_hosts

Unfortunately, the characters < , <<, >, >> and " need to be escaped if you use this command (I hope that will be fixed later on.)

Post your comment

Comments

No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments