user2131116
user2131116

Reputation: 2861

`ssh host command` can't return the result of the command

There are several computers , I want to use who command to see who is online . I write a script can let me check all the computers but it seems don't return the information of who is online ....

The computer just pause .

I try type this command => ssh f-001 who , and it works . But when I write it to the script , it fails .

here is my code

@Hosts = ("f-001","f-002","f-003","f-004","f-005");

for($i=0;$i<=$#Hosts;$i++)
{
        `ssh $Hosts[$i] who`;
        getc();
}

thanks ~ ~

Upvotes: 0

Views: 129

Answers (4)

G. Cito
G. Cito

Reputation: 6378

I think the answers here cover what you need but would emphasize the value of using foreach e.g.:

foreach my $host ("mail1", "san", "ws100.internal"){ say qx/ping -c1 $host/}

How do you plan to deal with the output? Unless you are watching the terminal you're going to want to log or write the results somewhere. Log::Dispatch is pretty simple but you can make your script log to files, rotate them, send email etc.

If you are going to do a lots of remote execution and monitoring be sure to take a look at Rex https://metacpan.org/pod/Rex (and http://www.rexify.com).

Upvotes: 1

Nikhil Jain
Nikhil Jain

Reputation: 8332

I would like to add one thing over here is that if you want to do further processiong with the data coming out from the command then remember you need to capture the output like

my @users = `ssh $Hosts[$i] who`;

Upvotes: 1

ceving
ceving

Reputation: 23774

Use system() instead:

@Hosts = ("f-001","f-002","f-003","f-004","f-005");

foreach $host (@Hosts)
{
    system ("ssh $host who");
}

And please do not iterate with $i.

Run3 is even more convenient.

Upvotes: 3

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81674

The results aren't displayed because while you're executing the command, you're not actually displaying its output; you'd need to do something like

print `ssh $Hosts[$i] who`;

Assuming you're using ssh-agent, Kerberos, or something else that lets you login without giving a password, the pause is just the `getc().

Upvotes: 3

Related Questions