Running a Program on a Remote Server Using SSH

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 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 [email protected]


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 [email protected]


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< /p>


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 [email protected] /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.

About The Author

C.S. Deam is a small business owner. His eBook Computer Nuggets: Non-Techie Internet Tips For In-Laws, Out-Laws, and the Rest of Society is a great gift for non-techie family members and is available at www.LinkertonPublishing.com where you can sign up for FREE E-Courses & Newsletters to help you on your path to self-employment.

In The News:


The Register

Green software blacked out Australian State
The Register
Something good is going to come out of last year's “Black System” in the Australian State of South Australia: the global wind power industry has learned how to do better modelling for systems under attack from repeated failures. South Australia last ...

and more »

TNW

3 software development trends you don't want to miss
TNW
This has led to more and more companies adopting team chat applications to assist in conducting key business operations, including the building and shipping software applications. Interacting with your team members using a chat application is only ...


New York Times

Software Engineer Starts Unlikely Business: A Weekly Newspaper
New York Times
The investment — by a software engineer who studied artificial intelligence, no less — seems like a quixotic one when so many newspapers are struggling and many readers prefer to catch up on town news on Facebook. But the engineer, Heeten Choxi, ...

and more »

Siemens' software donation to benefit Connecticut community college students
New Haven Register
The intial befeficiaries of Siemens' product lifecycle management software will be students in the Advanced Manufacturing Technology Centers at Three Rivers, Manchester, Asnuntuck and Gateway community colleges. Use of the software Siemens is making ...
Siemens Gives Community Colleges $315 Million Worth of Advanced Manufacturing SoftwareHartford Courant

all 4 news articles »

Brownwood Bulletin

Commissioners OK use of forfeiture funds for prosecutors' software
Brownwood Bulletin
Brown County Commissioners Court members on Monday authorized District Attorney Micheal Murray's office to spend just over $94,000 in forfeiture funds to buy software and equipment that will streamline the process of providing discovery to defense ...
Brown County DA getting new software to increase transparencyKTXS

all 2 news articles »

PCWorld

Switching from Mac to PC, Part 3: The software challenge | PCWorld
PCWorld
When you switch from Mac to PC, you'll be leaving some software behind. We've found some solid replacements.

and more »

GeekWire

How this legal software startup used its funding round to support legal aid under Trump budget cuts
GeekWire
But MetaJure, a Seattle startup that provides software to help lawyers manage their documents and email, is starting early. As early as the company's Series A funding round, in fact. MetaJure used its $2.6 million round as an opportunity for what its ...

and more »

Market Realist

How BBM Enterprise Could Impact BlackBerry's Software Business
Market Realist
By opening BBM Enterprise SDK (software development kit) to third-party developers, BlackBerry has also unlocked a new revenue stream in its Software and Services segment. The company could take a cut from the sales of third-party apps integrated with ...

and more »

Daily Mail

The 'magic carpet' software that helps fighter jet pilots make a perfect landing on a warship in ANY conditions
Daily Mail
The US Navy has revealed a radical new system allowing fighter pilots to land on deck safely. Called 'Magic Carpet' the software is being tested by F/A-18E/F Super Hornet and EA-18G Growler pilots across the Navy's fleet. It effectively acts as an ...

and more »

9 to 5 Mac

Apple rolling out macOS 10.12.4 software update with Night Shift for Mac
9 to 5 Mac
funny, that you mention case sensitivity. That is actually the biggest reason why I want APFS as soon as possible, because I'd really like a case sensitive filesystem for web development. I tried using HFS+ in it's case sensitive mode but a lot of ...

and more »
Google News

Need of Document Management System (DMS)

Document Management or Enterprise Information Management is perhaps one of... Read More

Internet Relay Chat - A Basic Introduction

What is IRC?IRC is Internet Relay Chat. It is a... Read More

Hubris - Definition: Microsofts Passport

Before September of 1995, Microsoft ignored the Internet because their... Read More

10 Things You Can Do With Photoshop CS2 That You Couldnt Do Before Now

Is Photoshop CS2 worth the upgrade? You bet it is!... Read More

Selecting Microsoft Great Plains Partner/VAR/Reseller: ERP Implementation & Customization ? Overview

In the case when you represent mid-size or mid-size-to-large business,... Read More

RSS: Get Notified When Your Favorite Websites Are Updated

RSS (Really Simple Syndication) is a way for a site... Read More

Microsoft Great Plains Payroll Module Customization Scenarios

It is now common thing when large corporation selects mid-market... Read More

How to Tell You Have Spyware, Ad-ware or Viruses

Usually, the easiest way to tell you have spyware is... Read More

Recovering Microsoft Great Plains Customization ? Tips for IT Director

Remember nice and prosperous Clinton era? When you implemented innovative... Read More

10 Steps To Secure And Manage Your Passwords

Passwords protect your most sensitive personal, financial and business information.... Read More

C++ Tutorials: 3, Program Flow (If, Else, While, For)

Program Flow is what you think it is. How the... Read More

Cisco Certification: Five Things To Do DURING Your CCNA Exam

There are plenty of articles out there about how to... Read More

Database Guru James F. Koopmann Reviews DBxtra Reporting and Query Tool

DBxtra is a powerful query and reporting tool that hides... Read More

Microsoft Great Plains Implementation ? Overview for IT Director/Controller

What is installation in the language of technology? Installation... Read More

Free PDF Publishing Software

In a previous article, I wrote about OpenOffice... Read More

Razzle Dazzle Them

Once upon a time not so long ago, there was... Read More

Inherent Dangers Of File Sharing Via The Internet.

Cyberspace has opened up a new frontier with exciting possibilities... Read More

Great Plains Customization Upgrade? Overview For CIO/IT Director

Around the same time Microsoft made its move with .Net... Read More

Groupware as a Document Manager: Collaboration Series #3

This article is the third of a series of articles... Read More

Microsoft CRM Implementation for Large Corporation ? overview

Microsoft Business Solutions CRM is now approaching the phase of... Read More

Great Plains Dexterity Development: FAQ

Microsoft Business Solutions Great Plains is Dexterity-written application and currently... Read More

Outlook... Not Just for Email! Using Your Outlook Calendar

Microsoft Outlook is one of the most widely used software... Read More

What is Fleet Maintenance Management?

Fleet Maintenance Management is a critical position in any company... Read More

Microsoft Great Plains on Ctree or Pervasive SQL ? What to Do ? Tips for IT Manager

As you probably know, when Microsoft purchased Great Plains Software... Read More

Put Some Pizazz in Your Software Arsenal

The software giants don't do everything and don't always produce... Read More