Running a Program on a Remote Server Using SSH

Nov 5
22:00

2003

C.S. Deam

C.S. Deam

  • Share this article on Facebook
  • Share this article on Twitter
  • Share this article on Linkedin

How do you run a program on a remote server using ssh? For this example we'll have two servers, one named Johnny and another named Cash. Both are running openssh. Our goal is to have a program on John

mediaimage

How do you run a program on a remote server using ssh?

For this example we'll have two servers,Running a Program on a Remote Server Using SSH Articles one named Johnny and another named Cash. Both are running openssh. Our goal is to have a program on Johnny login to Cash and run a program on Cash. To make the task a little more complex we'll be using different users on each machine.

The first thing we'll need to do is generate public and private keys on Johnny. So, logged into Johnny as user 'boy' we create public and private keys by creating them in the .ssh directory as follows:

Johnny$> pwd
/home/boy/.ssh
Johnny$> ssh-keygen -t rsa -f sue
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in sue.
Your public key has been saved in sue.pub.
The key fingerprint is:
8d:e9:c0:g1:c7:1f:e3:b3:2f:38:12:aa:b5:3b:2e:b3 boy@Johnny
Johnny$>

In the example above we picked an arbitrary name, sue, to identify the
files that hold the generated keys. When prompted to enter a
passphrase (twice) we simply hit enter twice.

As the output of ssh-keygen indicates, the public key has been saved in a file named sue.pub.

The output of ssh-keygen implies, but doesn't indicate directly, that the private key is in a file named sue (Yes, a user named boy created a file named sue.)

Johnny$>ls -l
-rwx------ 1 sue suegrp 887 Oct 17 14:27 sue
-rwx------ 1 sue suegrp 223 Oct 17 14:27 sue.pub

The private key file, sue, will remain on Johnny for the reaminder of this exercise, but the public key must be moved to the remote server, Cash. Note that the .ssh directory itself, as well as the sue and sue.pub files should have permissions of 700.

Now you'll need to ftp the sue.pub file from Johnny to Cash. The user on Cash that we'll login as is user 'named'.

Johnny$> ftp Cash
Connected to Cash
220 Cash - Propery of Xyz. - Authorized users only
Name (Cash:boy): named
331 Password required for named.
Password:
230-Last unsuccessful login: Fri Oct 17 13:12:55 2003 on ftp from Johnny 230-Last login: Fri Oct 17 16:02:11 2003 on /dev/pts/1 from Johnny
230 User named logged in.
ftp> cd .ssh
ftp> mput sue.pub
mput sue.pub? y
200 PORT command successful.
150 Opening data connection for sue.pub.
226 Transfer complete.
224 bytes sent in 0.000781 seconds (280.1 Kbytes/s)
local: sue.pub remote: sue.pub
ftp>bye
221 Goodbye.
Johnny$>

Now we'll telnet into Cash and concatenate the sue.pub file into /home/named/.ssh/authorized_keys file.

Cash$> pwd
/home/named/.ssh
Cash$> cat sue.pub >> authorized_keys
Cash$> exit
Connection closed.
Johnny$>

Let's recap what we've done so far.
1) We've created public and private keys on Johnny.
2) We've ftp'd the public key file, sue.pub, from Johnny to Cash.
3) We've telnetted to Cash, and concatenated the contents of sue.pub
into authorized_keys

We're now ready to manually login from Johnny to Cash using ssh.

Johnny$> ssh -i /home/boy/.ssh/sue named@Cash
The authenticity of host 'Cash (xxx.yyy.zzz.aaa)' can't be established.
RSA key fingerprint is 65:11:7d:ef:ed:a3:cc:34:d1:b5:ba:c9:16:22:31:23.
Are you sure you want to continue connecting (yes/no)? yes
================================================================

*** NOTICE TO ALL USERS ***

================================================================
Cash$>exit
Connection to Cash closed.
Johnny$>

Now on Johnny, create a shell script called 'boynamedsue.sh' with the following one line of contents and chmod the script to 777.

ssh -i /home/boy/.ssh/sue named@Cash /usr/bin/ls -l

Next, execute the script on Johnny.

Johnny$> boynamedsue.sh
================================================================

*** NOTICE TO ALL USERS ***

================================================================
-rwxrwxr-x 1 named namedgrp 10020 Oct 17 14:35 namedfile1.txt
-rw-r--r-- 1 named namedgrp 680 Aug 14 16:18 namedfile.html
-rw------- 1 named namedgrp 1148 Aug 18 09:51 mbox
drwxr-xr-x 2 named namedgrp 512 Jun 17 13:38 old
Johnny$>

You just executed a program on Johnny, that logged into Cash and ran a program (unix 'ls -l').

The next step you'll want to take is to replace the '/usr/bin/ls -l' command in the boynamedsue.sh program with the path and name of the program that you want to run.