WTem has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I would like to do a simple script that will send a command to a server through ssh from a windows machine. I am positive that the ssh connexion is established but I can't see if the command was really sent, I just got no result at all. My ref doc for doing this script: https://metacpan.org/pod/Net::SSH2 Here is my script:

#!/usr/bin/perl -w #### Bibliothèques use strict; use warnings; use Net::SSH2; #### Variables my $ip='X.X.X.X'; my $user='user'; my $password='passwd'; #print "IMEI: "; #my $imei = <>; #chomp($imei); #my $cmd = "/opt/LSS/bin/ueadmin_cli -o delete -I $imei"; my $cmd = "ls"; # this command for testing purpose only #### Open SSH connexion and sending command my $ssh = Net::SSH2->new(); $ssh->connect("$ip") or die "SSH connexion Failed.\n"; if ($ssh->auth_password("$user","$password")) { my $chan = $ssh->channel(); $chan->exec($cmd); print while <$chan>; $chan->close(); }else {print "authentication failed.\n";} $ssh->disconnect;

Replies are listed 'Best First'.
Re: Exec command through ssh connexion from window
by haukex (Archbishop) on May 28, 2022 at 08:32 UTC
    I am positive that the ssh connexion is established but I can't see if the command was really sent, I just got no result at all.

    The code you posted works for me. I would suggest that you need to debug further - for example, you say you get "no result at all", are you referring to the ls command? If yes, have you compared this to the output of ssh X.X.X.X ls (becuase some users' home directories are empty except for .dotfiles that don't show up in a regular ls)? Have you tried ssh user@X.X.X.X "/opt/LSS/bin/ueadmin_cli -o delete -I IMEI" from the command line? I also note you don't check the return value of the ->channel and ->exec calls as shown in the Net::SSH2 documentation, I strongly recommend you do that as well.