Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Net::SSH::Perl - want to launch remote cmd and return 1

Status
Not open for further replies.

goBoating

Programmer
Feb 8, 2000
1,606
US
Hello All,

I have inherited 14 linux and 2 HPUX production level machines in a testing environment. I need to be able to log into each of the 16 machines, launch a process to run in the background, and then return immediately while the program runs. The program takes 30-45 minutes to run. So, I'm try to ssh to the target machines and launch each run with hohup. In the code below, the first ssh->cmd runs in a few seconds. So, I don't care if I have to wait on that. The second ssh->cmd() is what runs a long time and I can't wait.

Also, so far, I'm just trying to get this to work hitting one target machine. I'll wrap a loop around it once I get the basics to work.

How do I get the ssh connection on the target/client end to execute the program in the background and close/release the ssh connection immediately?

Code:
#!/usr/bin/perl 
use strict;
use Net::SSH::Perl;
use Term::ReadKey;
$| = 1;

print "TARGET HOST: ";
my $host = <STDIN>;
print "USER: ";
my $user = <STDIN>;
print "PASSWORD: ";
ReadMode('noecho');
my $pass = ReadLine(0);
ReadMode('normal');
print "\nApplication Version to Run: ";
my $dev = <STDIN>;
chomp ($host, $user, $pass, $dev);
my $installer = '/redacted path info/'.$dev;

my $cmd  = "$installer -a -b /opt/app/redacted/";
print "Install CMD: $cmd\n";
my $ssh = Net::SSH::Perl->new($host, {protocol=>2}) or die "Failed to create object, $!\n";
unless ($ssh->login($user, $pass)) { die "Failed to login, $!\n"; }
my ($stdout, $stderr, $exit) = $ssh->cmd($cmd);

$cmd = 'nohup /redacted path/executable_file_here -a 2>&1 &';
print "Run CMD: $cmd\n";

##  I want the next line to launch the cmd in the 
##  background on the remote machine and return immediately.
my ($stdout, $stderr, $exit) = $ssh->cmd($cmd);

$stdout = ($stdout =~ /\w/) ? $stdout : 'Non-sensical response.' ;
$stderr = ($stderr =~ /\w/) ? $stderr : 'None.' ;

print "Response:\n$stdout\nErrors: $stderr\n";

I have done this before and made it work and it is driving me crazy that I can not remember the details.....

Anyway, thanks.

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
I'm another step closer. From a command prompt, if I give ssh the '-t' switch this trick works.

Code:
prompt> ssh -2qt user@host 'nohup /path/to/command' &

Now, I want to use the Net::SSH::perl module. How do I use the ssh '-t' flag with that module.



'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
you don't really need to use nohup, just redirect the IO channels of the remote process to /dev/null:

ssh foo@host '/remote/command </dev/null >/dev/null 2>&1 &'
 
The -n option also helps encourage ssh to detach from its controlling terminal.

Annihilannic.
 
Got it working... .
Thanks guys.

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top