Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Pass local bash script to remote host via SSH

by puterboy (Scribe)
on Jun 24, 2020 at 04:16 UTC ( [id://11118402]=perlquestion: print w/replies, xml ) Need Help??

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

As part of a larger Perl program, I need to execute a local bash script on a remote machine. I would like to do the equivalent of:
#!/bin/bash ssh <remotehost> bash -s <<EOF <local bash script written here> EOF
but from within a perl script using either backticks or system to run the ssh command. Something like
#!/usr/bin/perl my $output = `ssh <remotehost> bash -s` <<EOF <local bash script written here> EOF
Note that I want to load the bash script from within the same perl file where the ssh command is run -- not load it from an external separate file. Note that I can't run Net::SSH since I don't have access to additional modules

Replies are listed 'Best First'.
Re: Pass local bash script to remote host via SSH (updated)
by soonix (Canon) on Jun 24, 2020 at 06:20 UTC
    use strict; use warnings; open(my $out_fh, "|-", qw'ssh <remotehost> bash -s') or die "Can't sta +rt ssh: $!"; print$out_fh <<EOF; <local bash script written here> EOF close $out_fh or die "Error flushing/closing pipe to ssh: $!";
    see open, and Quote and Quote like Operators (watch the semicolon after <<EOF - the <<EOF syntactically stands in for the complete string)

    Update:should you need variables interpolated in your 'ssh <remotehost> ...', you need to create the list in a different way, because qw doesn't interpolate variables.

        Good catch, updated
Re: Pass local bash script to remote host via SSH
by haukex (Archbishop) on Jun 24, 2020 at 09:55 UTC
    Note that I can't run Net::SSH since I don't have access to additional modules

    Yes, even you can use CPAN - Net::OpenSSH is a pure-Perl module with no dependencies, which means it can be simply copied over.

    use Net::OpenSSH; my $ssh = Net::OpenSSH->new($host); die "Couldn't establish SSH connection: ".$ssh->error if $ssh->error; my ($in, $pid) = $ssh->pipe_in('bash','-s') or die "pipe_in failed: ".$ssh->error; print $in <<'EOF'; # ... EOF close $in or die "close failed";
      Even shorter...
      use Net::OpenSSH; die "Couldn't establish SSH connection: ".$ssh->error if $ssh->error; $ssh->system({stdin_data => <<EOD}, 'bash', '-s') or die "command fail +ed: " . $ssh->error; # ... EOD
Re: Pass local bash script to remote host via SSH
by perlfan (Vicar) on Jun 24, 2020 at 11:48 UTC
    My recommendation is also to use something like Net::OpenSSH as mentioned by my fellow Monks, but coupled with $HOME/.ssh/config so that you remove the need to manage (most if not all) ssh options inside of your script. So <remotehost> becomes <ssh_config_alias> in your DWIW illustration above.

    In addition to this, there is nothing wrong with uploading a script file directly into $HOME/tmp then executing that script. In many ways this is much more clear and will be more easily understood by your future self during any maintenance of this program.

    Finally be sure to protect the script appropriately, for example I would not blindly upload it to /tmp and I would ensure that only the remote system $USER is able to read the script. Also, clean up after by deleting the script.

    For additional security, you may pass sensitive values to the script via the environment using the equivalent of ssh -o SendEnv=MYVAR directly or in your code. Any passwords or other secrets may be provided to the script this way and the script would just make use of them assuming they are defined. Do not send them to the script via commandline argument or by explicitly setting the value in the ssh command itself.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-19 03:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found