2

How can I run command via SSH on remote device?

I'm trying next ideas:

1:

[root@0400smarts-voip01 test]# ssh user@IP "my command"
user@IP's password:
Connection to IP closed by remote host.

2:

[root@0400smarts-voip01 test]# ssh -t user@IP "my command"
user@IP's password:
Connection to IP closed by remote host.
Connection to IP closed.

Also i'm trying run command via Perl script:

#!/bin/perl
use strict;

use Net::OpenSSH;

my $hostname = "IP";
my $username = "user";
my $password = "password";
my $cmd = "command";

my $ssh = Net::OpenSSH->new(host=>"$hostname", user=>"$username", port=>22, password=>"$password");
$ssh->system($cmd);

But this method also doesn't work:

[root@0400smarts-voip01 test]# ./test.pl
Connection to IP closed by remote host.

How can I solve this problem?

  • 1
    That doesn't look like a programming problem to me. – melpomene Jan 27 '18 at 12:42
  • 1
    Your example #1 is essentially the correct way to run a command on a remote system. There are reasons why it might not work, mostly involving how the remote system is configured. There's no way to way what's going wrong for you, given the small amount of information that you've provided. – Kenster Jan 27 '18 at 12:45
  • 4
    So the *real* question here is: "Why can't I log in to a remote system using SSH?". You'll need to check system logs and configuration for that, which does require that you have access to it. Can you actually log in properly? – code_dredd Jan 27 '18 at 12:45
  • 2
    First thing you should try is running `ssh` in verbose mode, it will tell you what it is doing under the hood: `ssh -vvvv ...` – salva Jan 27 '18 at 18:06

0 Answers0