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

Discussion in 'Perl Misc' started by Icari, Aug 18, 2004.

  1. Icari

    Icari Guest

    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.
     
    Icari, Aug 18, 2004
    #1
    1. Advertising

  2. Icari <> wrote in message
    <news:>:

    > 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.

    --
    David Wall
     
    David K. Wall, Aug 19, 2004
    #2
    1. Advertising

  3. Icari

    Icari Guest

    "David K. Wall" <> wrote in message news:<Xns954A6923E8A64dkwwashere@216.168.3.30>...
    >
    > 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.
     
    Icari, Aug 19, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jamis Buck

    [ANN] Net::SFTP 0.5.0

    Jamis Buck, Dec 2, 2004, in forum: Ruby
    Replies:
    0
    Views:
    131
    Jamis Buck
    Dec 2, 2004
  2. Jamis Buck

    [ANN] Net::SFTP 0.9.0

    Jamis Buck, Jan 11, 2005, in forum: Ruby
    Replies:
    0
    Views:
    98
    Jamis Buck
    Jan 11, 2005
  3. Jamis Buck
    Replies:
    0
    Views:
    102
    Jamis Buck
    Jun 18, 2005
  4. MaggotChild

    Net::SFTP Fails, sftp OK

    MaggotChild, Oct 28, 2009, in forum: Perl Misc
    Replies:
    2
    Views:
    1,003
    C.DeRykus
    Oct 29, 2009
  5. Kevin13
    Replies:
    3
    Views:
    949
    Kevin13
    Dec 1, 2009
Loading...

Share This Page