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:


Forbes

Hackers Hide Malware in CCleaner Software...And Other Small Business Tech News This Week
Forbes
I write about technology developments for small business owners. Opinions expressed by Forbes Contributors are their own. (Photo by Steve Jennings/Getty Images for TechCrunch). Here are five things in technology that happened this past week and how ...
Hijacked software used to target tech giantsBBC News

all 49 news articles »

The News International

IT enthusiasts throng annual software show
The News International
Aptech, an institute for computer education, organised a software exhibition, titled 'Aptech Vision 2017: Exploring Ideas for Tomorrow', at a hotel on Saturday, attracting a large number of information technology enthusiasts. Around 50 software ...


The Jewish Press - JewishPress.com

German Software Company Buys Israeli Gigya for $350 Million
The Jewish Press - JewishPress.com
SAP, the largest software company in Europe and the fourth largest in the world, is acquiring Israeli software company Gigya for $350 million in cash, theMarker reported Saturday. The deal has been signed and will be finalized within a few weeks.
Israeli Firm Gigya Bought by Software Giant SAP for Over Billion Shekels in CashHaaretz

all 6 news articles »

Synopsys eyes software security firms
The Hindu
Synopsys Inc., a Nasdaq-listed firm and a maker of electronic design automation software products, is eyeing acquisitions in the software security space as it plans to repatriate up to $850 million of overseas earnings to take advantage of the proposed ...


Maui Now

GIS Software Free for all Hawai'i Schools
Maui Now
ESRI, the GIS software industry leader, is offering the new ArcGIS school bundle at no charge to all K12 public, private and charter schools throughout Hawai'i. The bundle includes three products: ArcGIS Desktop (includes ArcGIS Pro Advanced, ArcMap ...

and more »

FCW.com

A how-to software guide for federal managers
FCW.com
What: "Managing custom software development in government when you're not a software engineer," a blog post from 18F's Kaitlin Devine. Why: Custom software development is becoming increasingly aligned with the mission of many government programs.


FedScoop

IRS software isn't compliant with the MEGABYTE Act, report says
FedScoop
Despite efforts to improve its software license management, the IRS still doesn't have policies, guidance or tools to be in compliance with federal law, a watchdog found. The Treasury Inspector General for Tax Administration said in a Sept. 18 report ...


Locally developed software helps transportation companies connect
Youngstown Vindicator
Burgoyne formed Vantage Solutions LLC in 2014 and got connected to his software developer Ash Gaur. Burgoyne had the idea and Gaur made it a reality. “What we have found in the intermodal industry is there is a lack of communication,” Gaur said.

and more »

Seeking Alpha

Micro Focus International - The Biggest Software Company You've Never Heard Of
Seeking Alpha
MFGP is now a $19bn EV enterprise software business following its acquisition of Hewlett Packard Enterprise's Software division. Trading at an EV of 4.3x TTM revenue and 13.4x TTM EBITDA, the company has pro-forma combined TTM revenues of $4.4bn ...


The Sun

What is Greyball and why is the Uber software so controversial? All you need to know
The Sun
TRANSPORT for London has ruled that taxi app Uber will not be able to renew its operating licence after it expires at the end of the month. Among the reasons behind the decision was Uber's use of its Greyball software. Here's what it does and why it's ...
What is the Greyball software Uber used to avoid the law?Metro
What is Greyball? Inside the covert software that helped to get Uber banned in LondonMirror.co.uk
Greyball: What is the creepy feature that got Uber banned in London?The Independent
T.co
all 900 news articles »
Google News

Microsoft CRM ? Typical Customizations

Microsoft CRM was designed to be easily customizable. Microsoft CRM... Read More

Basic Steps To Optimize Your Internet Security

After seeing many people complain about their weak Internet security... Read More

Microsoft Small Business Manager Customization Options - Overview

Microsoft Business Solutions Small Business Manager is Great Plains Dexterity... Read More

Five Tips For A Great Software Demo

Whether you need to close a sale, gather end-user feedback,... Read More

Algebra Help Software

Need help making sense of algebra? Have algebra lectures in... Read More

How to Choose the BEST Charting Software

I suggest that you do not spend a lot of... Read More

Microsoft CRM Integration with Microsoft Retail Management System (RMS) ? Overview

Microsoft Client Relation Management system (Microsoft CRM) and Microsoft RMS... Read More

How to Make Attractive and Effective PowerPoint Presentations

Microsoft PowerPoint has dramatically changed the way in which academic... Read More

Microsoft Great Plains: If You are Orphan Client ? What to Do and FAQ

Microsoft Business Solutions Great Plains, former Great Plains Software eEnterprise,... Read More

What is Fleet Maintenance Software?

Simply put, fleet maintenance allows companies to monitor and maintain... Read More

Software For Your Hard-Drive

All your software is stored on a hard-drive. But how... Read More

Things You Can Do With Windows XP!

Did you ever give a thought to the number of... Read More

10 Ways to Learn a Software

Following tips help you to learn a software in lesser... Read More

How to Make Own CMS

Every day millions of new web documents emerge on the... Read More

Microsoft Great Plains Reporting ? Overview for Developer

Looks like Microsoft Great Plains becomes more and more... Read More

Software: What Suits Me?

Almost all new and major brand of PCs come with... Read More

Microsoft CRM Customization ? Programming Closed Email Activity

Microsoft CRM is CRM answer from Microsoft and attempt to... Read More

Snort for Network IDS

What is Snort?Snort is an open source network intrusion detection... Read More

Story Development Software: Good or Evil?

In the early days of the personal computer, we're talking... Read More

Make or Break Factors - When Considering Estimating Software

Make-or-Break Factors in Success and ProfitabilityFor quick printers, estimating can... Read More

What You Should Know About Installing Screensavers

Do you remember that frustrating feeling when you find an... Read More

6 Easy Steps for a Smoother CD/DVD Order

It's all about turn times in the eMedia industry! The... Read More

Tripwire for Linux File Integrity

What is Tripwire?Tripwire is a form intrusion detection system (IDS)... Read More

Microsoft Great Plains: Customization Upgrade & Recovery ? Visual Studio VB 6.0

Microsoft Great Plains, former Great Plains Software Dynamics, eEnterprise has... Read More

Getting Technical Support Help Online

Sometimes your PC will start acting strange for no apparent... Read More