Net::SFTP ssh_args => [ ] syntax question...

I

Icari

I am fairly new to Perl this being my first scripting project so I'm
still in my 'baby-talk' stage, but any suggestions/input would be most
helpful!

There's an example psftp script in the Net::SFTP module directory
which defines the following argument hash to pass into sftp:

my %args = (ssh_args => []); # from the perl man pages, it looks like
# ssh_args can be any arguments
# accepted by Net::SSH, so I guess
# I can put a hash list within
# a hash?

I wasn't able to run that script cleanly to test if my Net::SFTP
module was installed correctly so I wrote a smaller script to test:

#!/usr/local/bin/perl
use strict;

use Net::SFTP;

<$host, $user and $pass definined here>
my %args = (user => "$user", password => "$pass");
my $sftp = Net::SFTP->new($host, %args);
$sftp->put("testfile", "/var/tmp/remotetest");

When I ran my transfer program, it exits with a complaint that it does
not know what port 'ssh' is and made reference to the Net::SSH::perl
pm file...

I added ssh port 22 in my /etc/services file to temporarily fix this,
but I am really curious as to how I can define the ssh port within my
argument hash?

my %args = (user => "$user", password => "$pass", ssh_args => [port =>
22]);

Is the above right? I don't see the [ ] signs used too often outside
of pattern class definitions, the only time I saw this was with slices
so I'm not sure what the correct way to input additional ssh arguments
into the ssh_args option.

Thanks,
Icari.
 
D

David K. Wall

There's an example psftp script in the Net::SFTP module directory
which defines the following argument hash to pass into sftp:

my %args = (ssh_args => []); # from the perl man pages, it looks
like
# ssh_args can be any arguments
# accepted by Net::SSH, so I guess
# I can put a hash list within
# a hash?

I'm not sure I understand the question, plus you mention Net::SSH
when Net::SFTP uses Net::SSH::perl instead of Net::SSH. But, yes, you
can put a hash inside the anonymous array following 'ssh_args', e.g.;

my %params = ( port => 22 );
my %args = (ssh_args => [%params]);

But that's a rather long-winded way of doing it, and in my opinion
it's a bit obfuscated. More plainly: yecch! :)
I wasn't able to run that script cleanly to test if my Net::SFTP
module was installed correctly so I wrote a smaller script to
test:

#!/usr/local/bin/perl
use strict;

use Net::SFTP;

<$host, $user and $pass definined here>
my %args = (user => "$user", password => "$pass");
my $sftp = Net::SFTP->new($host, %args);
$sftp->put("testfile", "/var/tmp/remotetest");

When I ran my transfer program, it exits with a complaint that it
does not know what port 'ssh' is and made reference to the
Net::SSH::perl pm file...

The docs for Net::SFTP also refer you to Net::SSH::perl....
I added ssh port 22 in my /etc/services file to temporarily fix
this, but I am really curious as to how I can define the ssh port
within my argument hash?

It seems you read the docs for Net::SFTP and Net::SSH::perl, because
the code below defines the ssh port just as the docs recommend. But
ssh_args is not really associated with a hash, but a scalar: a
reference to an anonymous array. You might benefit from reading
perlreftut and perlref. In particular, see "Make Rule 2" in
perlreftut.
my %args = (user => "$user", password => "$pass", ssh_args =>
[port => 22]);

$user and $pass don't need to have quotes around them, though. See
'perldoc -q quoting' for why this is generally a bad idea.
Is the above right? I don't see the [ ] signs used too often
outside of pattern class definitions, the only time I saw this was
with slices so I'm not sure what the correct way to input
additional ssh arguments into the ssh_args option.

The docs call for a reference to a list of named arguments; [] is the
most convenient way to write this. If you also wanted to turn on
compression, here's one way to write it:

my $sftp = Net::SFTP->new( $host,
user => $user,
password => $pass,
ssh_args => [port => 22, compression => 1]
);

Caveat: I've never used Net::SFTP, the above code is untested and
based purely on a quick reading of the docs.
 
I

Icari

David K. Wall said:
I'm not sure I understand the question, plus you mention Net::SSH
when Net::SFTP uses Net::SSH::perl instead of Net::SSH. But, yes, you
can put a hash inside the anonymous array following 'ssh_args', e.g.;

my %params = ( port => 22 );
my %args = (ssh_args => [%params]);

But that's a rather long-winded way of doing it, and in my opinion
it's a bit obfuscated. More plainly: yecch! :)
The docs for Net::SFTP also refer you to Net::SSH::perl....
It seems you read the docs for Net::SFTP and Net::SSH::perl, because
the code below defines the ssh port just as the docs recommend. But
ssh_args is not really associated with a hash, but a scalar: a
reference to an anonymous array. You might benefit from reading
perlreftut and perlref. In particular, see "Make Rule 2" in
perlreftut.
my %args = (user => "$user", password => "$pass", ssh_args =>
[port => 22]);

$user and $pass don't need to have quotes around them, though. See
'perldoc -q quoting' for why this is generally a bad idea.

The docs call for a reference to a list of named arguments; [] is the
most convenient way to write this. If you also wanted to turn on
compression, here's one way to write it:

my $sftp = Net::SFTP->new( $host,
user => $user,
password => $pass,
ssh_args => [port => 22, compression => 1]
);

Caveat: I've never used Net::SFTP, the above code is untested and
based purely on a quick reading of the docs.

Oops, I actually did mean Net::SSH::perl. I read the man pages for
both SFTP and SSH-Perl modules, but was a bit confused with the syntax
usage when I saw the ssh_args => [ ] line in the example file. I just
tested this out and it worked, I won't need to worry about making sure
/etc/services had ssh port defined when I port my script to another
system. Thanks for your help :). I was just recently placed on a
project to provide some scripts and just started to learn perl while I
was creating my code (I have not been coding for a long time) so I'm
still learning. I'll refer to the perlreftut and perlref for more
information, thanks again for all the tips!

Icari.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top