Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

execute remote perl script via ssh

by vinoth.ree (Monsignor)
on Jul 26, 2020 at 09:47 UTC ( #11119825=perlquestion: print w/replies, xml ) Need Help??

vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

I have a perl script on unix server and running it from windows machine. I have already synced the public key generated from windows machine to unix server so it does not ask for password.

ssh.exe username@xxx.xxx.xxx.xxx /fullpath/check_user_exists.pl userna +me

The remote perl script does checks weather the username name exists in a csv file in the unix server that's it. If it exists it exits with 0 otherwise 1.

This is run by all the users in my team it works most of the users for particular user it does not works. When I run this command via command line manually it return 0, but when I call via perl script it returns 255.

What could be the issue? how to debug this issue?

This is how i call this command locally from a perl script on windows side.

sub executeCommand{ my $command = join ' ', @_; $prot and print("Command: $command\n"); my $response = qx($command 2>&1); my $status = $? >> 8; return ($status, $response); } my @src_cmd = ('ssh.exe ', $ssh_user.'@'.$hostname ." /fullpath/check_ +user_exists.pl " . "$user"); my ($status, $output) = &executeCommand(@src_cmd);


All is well. I learn by answering your questions...

Replies are listed 'Best First'.
Re: execute remote perl script via ssh
by haukex (Bishop) on Jul 26, 2020 at 09:58 UTC

    Have you looked at the values of $ssh_user, $hostname, and $user via e.g. Data::Dumper? Do you know whether ssh.exe is always in the PATH? Also, you should definitely be using a module to run external commands more safely, for example in this case I think IPC::Run3 might be appropriate (more info and sample code here). At the very least, use Win32::ShellQuote to build the command line.

      Hi haukex

      I can see the value of $ssh_user, $hostname, and $user for the particular user

      I will try with IPC::Run3 instead of using qx().

      All is well. I learn by answering your questions...
        Hi Monks,

        I have identified the issue for that particular user, As I already said we use public key method to auto login without asking for the password, there is the problem, his user account is being shared by another user, so his private key is not secure and ssh fails with below error

        debug3: Bad permissions. Try removing permissions for user: XXXXX on f +ile C:\\Users\\XXXXXX/.ssh/id_rsa. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: UNPROTECTED PRIVATE KEY FILE! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Permissions for 'C:\\Users\\XXXXXX/.ssh/id_rsa' are too open. It is required that your private key files are NOT accessible by other +s. This private key will be ignored.

        After removing the permission for the other user, it works fine.


        All is well. I learn by answering your questions...
Re: execute remote perl script via ssh
by jcb (Vicar) on Jul 27, 2020 at 03:06 UTC

    If it works for most users, but fails for one particular case, you probably have either a problem with that user's name containing shell metacharacters, or something is wrong with the check_user_exists.pl script on the server.

Re: execute remote perl script via ssh
by karlgoethebier (Abbot) on Jul 26, 2020 at 18:32 UTC

    ssh -v

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: execute remote perl script via ssh
by perlfan (Priest) on Jul 26, 2020 at 22:04 UTC
    Make it easy on yourself and set up an ssh config. It might be worth using cygwin or even Docker and a super light alpine image running cron.
      Make it easy on yourself and set up an ssh config. It might be worth using Cygwin

      On my Windows 7 box I use Cygwin in precisely that way for ssh connections to various nix machines. It works well. I envisage that it would also work equally well from an MSYS2 installation.

      For scripted communication between Windows and nix I usually use Net::SSH2 or Net::SSH::Any (which utilizes Net::SSH2 via a more user-friendly interface), run from a native Windows cmd.exe shell.

      Cheers,
      Rob

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11119825]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2020-09-19 02:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I don’t succeed, I …










    Results (114 votes). Check out past polls.

    Notices?