Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

connecting via SSH

by nathanvit (Beadle)
on Mar 14, 2006 at 15:48 UTC ( [id://536607]=perlquestion: print w/replies, xml ) Need Help??

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

Hello and sorry for my english...
I'm trying to write a script from WinXP to connect to a linuxbox via SSH.
I'm using Net::SSH::Perl
My problem is that it responds:
Can't map service name 'ssh' to port number at script.pl line 11

Any suggestions please?

PS: Obviusly ssh is running in linuxbox on the default port...
use Net::SSH::Perl; use strict; my $ssh = Net::SSH::Perl->new($host_ssh); print "Sto per connettermi al server $host_ssh via ssh\n"; $ssh->login($user_ssh, $passwd_ssh);

Replies are listed 'Best First'.
Re: connecting via SSH
by thor (Priest) on Mar 14, 2006 at 15:53 UTC
    In reading the docs for Net::SSH::Perl, it looks like it can take an optional port argument in the new method. Try:
    use Net::SSH::Perl; use strict; my $ssh = Net::SSH::Perl->new($host_ssh, port => 22); print "Sto per connettermi al server $host_ssh via ssh\n"; $ssh->login($user_ssh, $passwd_ssh);
    and see if that does the trick. It'd be weird if that did it though; you'd think that that would be the default.

    thor

    The only easy day was yesterday

      Normally, the default is accessed by getservbyname, but Windows doesn't have SSH in the "database" accessed by getservbyname. There are three solutions:

      • Specify the port explicitely, as suggested in the parent post.

      • Upgrade to version Net::SSH::Perl version 1.27 or higher. Instead of relying entirely on getservbyname, version 1.27 and higher use 22 (hardcoded) as the port number if getservbyname fails.

        Older version (<= 1.26):

        my @serv = getservbyname(my $serv = $rport, 'tcp'); $rport = $serv[2]; croak "Can't map service name '$serv' to port number" unless defined $rport;

        Newer version (>= 1.27):

        my @serv = getservbyname(my $serv = $rport, 'tcp'); $rport = $serv[2] || 22;
      • Add SSH to the services "database". In windows, it's %SystemRoot%\system32\drivers\etc\services, a text file identical to unix's /etc/services. The entry to add is simply:

        ssh 22/tcp # Secure Shell Login
      I'm really stuck.
      I've declared port as you suggested but when i start my script it responds:
      Your Vendor has not defined Fcntl macro F_SETFL, used at c:/Perl/site/lib/Net/SSH/Perl.pm line 218
      Which "Vendor" does it mean?? I can't understand the meaning...

        That error means the OS doesn't support it that flag for fnctl, and that Perl doesn't emulate it. I don't know if that helps, but newer version of Net::SSH::Perl don't use F_SETFL.

        Older version (<= 1.26):

        fcntl($sock, F_SETFL, O_NONBLOCK) or die "Can't set socket non-blocking: $!";

        Newer version (>= 1.27):

        defined($sock->blocking(0)) or die "Can't set socket non-blocking: $!";

        Check if you have the module Fcntl. I think it is installed with Perl by default. Btw, are you using Active State or Cygwin's Perl?

        Igor 'izut' Sutton
        your code, your rules.

        fcntl is not very portable. That's why you get on an OS that support a particular fcntl flag and an fcntl that doesn't emulate it.

        ("Vendor" refers to the OS.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://536607]
Approved 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-25 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found