Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Net::OpenSSH pass options to ssh

by zn553 (Initiate)
on Mar 18, 2019 at 14:01 UTC ( [id://1231396]=perlquestion: print w/replies, xml ) Need Help??

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

Hello how do I pass several options to ssh using Net::OpenSSH module i have this:
my $ssh = Net::OpenSSH->new($host, user => $user, master_opts => ['-vvv'], key_path= $private_key_path, default_ssh_opts=>[-o =>'Port=24'] ); $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error;
I would like to do something similar to this: ssh -o ConnectTimeout=60 -oport=24 when I try it is still port 22 being used. thank you

Replies are listed 'Best First'.
Re: Net::OpenSSH pass options to ssh
by salva (Canon) on Mar 18, 2019 at 15:08 UTC
    $ssh = Net::OpenSSH->new(..., master_opts => ['-vvv', '-oPort=24', '-oConne +ctTimeout=60']);
    or...
    $ssh = Net::OpenSSH->new(..., port => 24, master_opts => ['-vvv', '-oConnectTimeout=60' +]);
Re: Net::OpenSSH pass options to ssh
by thanos1983 (Parson) on Mar 18, 2019 at 14:48 UTC
      Hello my question is more related to pass several options to SSH using default_ssh_opts
        Net::OpenSSH constructor accepts two different sets of options to be passed to ssh: master_opts and default_ssh_opts.

        The first set goes into the ssh call that connects to the remote machine and creates the multiplexing socket.

        The second set goes into the ssh calls that are used to run the remote commands (when calling system or capture, for instance). They reuse the connection established by the master ssh process, and so at that point it is usually too late to set most of the options supported by ssh. In practice, using default_ssh_opts is very uncommon.

        Hello zn553,

        I was under the impression that you asked how to pass multiple commands, this is why I post my answer. Well fellow Monk salva has answered your question already :).

        BR / Thanos

        Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Net::OpenSSH pass options to ssh
by kcott (Archbishop) on Mar 22, 2019 at 08:51 UTC

    G'day zn553,

    Welcome to the Monastery.

    As I can see no other mention of it, I thought I'd point out that your first statement has:

    ... key_path= $private_key_path, ...

    Here's how Perl sees that statement:

    $ perl -MO=Deparse,-p -e 'my $ssh = Net::OpenSSH->new($host, user => $ +user, master_opts => ["-vvv"], key_path= $private_key_path, default_s +sh_opts=>[-o =>"Port=24"]);' Can't modify constant item in scalar assignment at -e line 1, near "$p +rivate_key_path," -e had compilation errors. (my $ssh = 'Net::OpenSSH'->new($host, 'user', $user, 'master_opts', [' +-vvv'], ('key_path' = $private_key_path), 'default_ssh_opts', [(-'o') +, 'Port=24']));

    Changing '=' to '=>':

    $ perl -MO=Deparse,-p -e 'my $ssh = Net::OpenSSH->new($host, user => $ +user, master_opts => ["-vvv"], key_path=> $private_key_path, default_ +ssh_opts=>[-o =>"Port=24"]);' (my $ssh = 'Net::OpenSSH'->new($host, 'user', $user, 'master_opts', [' +-vvv'], 'key_path', $private_key_path, 'default_ssh_opts', ['-o', 'Po +rt=24'])); -e syntax OK

    This comment is not intended to invalidate other responses. However, as you've shown no surrounding code — which could perhaps capture the error in an eval, or similar, block — nor any error or warning output, this may be an additional problem you'll need to address.

    If you're unfamiliar with the code I've used, take a look at B::Deparse; and, in case you need it, perlrun has more information on Command Switches.

    — Ken

Log In?
Username:
Password:

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

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

    No recent polls found