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

hi perlmonks
This is my first post,i installed Net::SSH2.
but i couldn't get the output
here is my code
#!/usr/bin/perl use Net::SSH2; my $ssh2 = Net::SSH2->new(); $ssh2->connect('$HostName') or die "Unable to connect $HostName $@ \n" +; $ssh2->auth_password('$user_id','$password') or die "Unable to login $ +@ \n"; my $chan = $ssh2->channel(); $chan->exec('ls -ltr') or die "Unable to execute the command $@ \n";
How can i get output,how can i debug this code and give me comments on this code.
Have Nice Day

Replies are listed 'Best First'.
Re: help on Net::SSH2
by madbombX (Hermit) on Aug 21, 2006 at 17:06 UTC
    The first thing that I would get into the habit of doing using the lines:

    use strict; use warnings;

    Next, the following code is what works for me under Net::SSH2:

    use strict; use warnings; use Net::SSH2; my $ssh2 = Net::SSH2->new(); $ssh2->connect('example.com') or die ("SSH2 Connect Error: $!"); if ($ssh2->auth_keyboard('password')) { my $chan = $ssh2->channel() or die ("Channel Create Error: $@"); $chan->exec('ls') or die ("Channel Exec: $@"); } else { die ("Password Error"); }

    With the above code, you'll be able to find out if its connecting. If the code 'die's, then its not actually connecting.

    You should note that when you do an 'exec' that it uses a perl exec (http://search.cpan.org/~nwclark/perl-5.8.7/pod/perlfunc.pod#exec) which executes a system command and never returns. You may want to open up a shell and execute the command and then return the output since the will act more like system and less like exec.

    Eric

Re: help on Net::SSH2
by zentara (Cardinal) on Aug 21, 2006 at 17:33 UTC
    Also, if you want to return the output of the channel, the docs say it acts like a tied filehandle, so......
    #!/usr/bin/perl use warnings; use strict; use Net::SSH2; my $ssh2 = Net::SSH2->new(); $ssh2->connect('localhost') or die "Unable to connect Host $@ \n"; $ssh2->auth_password('z','ztester') or die "Unable to login $@ \n"; #works #my $sftp = $ssh2->sftp(); #my $fh = $sftp->open('/etc/passwd') or die; #print $_ while <$fh>; # A channel object is created by the Net::SSH2 "channel" method. # As well as being an object, it is also a tied filehandle. my $chan = $ssh2->channel(); $chan->exec('ls -la'); while (<$chan>){ print }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Thanks
      yes it is printing the values.
      how can ,if i want to change the directory and list the files.
      more over,i have to do lot of commands in single channel
        Read "perldoc Net::SSH2::Channel". You can combine the stderr and stdout with 'merge', so you can see the remote error messages. If you want to run mutiple commands, you can use "shell" or just create as many channels as you need, like $chan, $chan1, $chan2, etc. and run exec on them in an sequential order. Another option may be to use the SFTP object, if you just want dir listings.

        I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: help on Net::SSH2
by zentara (Cardinal) on Aug 22, 2006 at 15:54 UTC
    Hi, again. I was fiddling with the various Net::SSH2 features, and found 2 good example sources. The first is the Net-SSH2.t test file, and the second is the mailling list (with browsable archives) at SSH2 users

    Here is an example of how to use the shell(), from the archives.

    #!/usr/bin/perl use warnings; use strict; use Net::SSH2; my $ssh2 = Net::SSH2->new(); print "connect fail\n" unless ($ssh2->connect('testbox')); print "password fail\n" unless ($ssh2->auth_keyboard('z','*******')); my $chan=$ssh2->channel(); $chan->shell(); print $chan "uname -a\n"; print "LINE : $_" while <$chan>; print $chan "who\n"; print "LINE : $_" while <$chan>; $chan->close; <--end-- and the output: --start--> LINE : Linux testbox 2.6.5-7.111.19-smp #1 SMP Fri Dec 10 15:10:58 UT +C 2004 i686 i686 i386 GNU/Linux LINE : root pts/0 Feb 14 14:33 (robspc) <--end--

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum