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

Hi there,

i have another question concerning the SSH2 Module. The SSH-connection i have to accomplish is: -SSH Server 1- from there to -SSH Server 2- an from here via Telnet to the Devices.

I thougt I could easily connect via SSH to the first Server, start there something like "use shell" and give the commands like im sitting front of my Putty-session...

But it seems not as easy like this, and im a bit teary, beacuse im a trainee and need to finish the script on friday OO

Please help...

Regards from germany

Replies are listed 'Best First'.
Re: Net::SSH2 Question - again-
by Khen1950fx (Canon) on May 28, 2008 at 14:43 UTC
Re: Net::SSH2 Question - again-
by moritz (Cardinal) on May 28, 2008 at 11:00 UTC
    If you want use a shell through a ssh connection, why use perl at all? ssh does it just fine.

    Usually you use perl for this sort of thing if you want to automate something, in which case you don't need a shell.

Re: Net::SSH2 Question - again-
by bgi (Sexton) on May 28, 2008 at 11:06 UTC
    Sorry, i forgot the half...this is my script so far:

    use strict; use Net::SSH2; my $ssh1= Net::SSH2-> new(); my $buf; $ssh1-> connect('ipaddress') or die "conncet to SSH1 failed"; $ssh1-> auth_password('user', 'pass') or die "auth1 failed"; my $chan1= $ssh1-> channel(); $chan1-> blocking(1); $chan1-> exec('uname'); $chan1-> read($buf, 100); print $buf, "\n"; $chan1-> close;
    Works fine -so far-. Now i need to establish the connection from the first SSH-server to the second...

    The best will be to manage that with the commands i would give to my putty session...so the next would be "ssh- l user ipaddress" an the there is a password oO

    No idea how to manage the password through the channel!!!!
      avoid the issue with a public key
Re: Net::SSH2 Question - again-
by idsfa (Vicar) on May 28, 2008 at 15:27 UTC

    You can use ssh's port forwarding to do this:

    use strict; use Net::SSH2; my $ssh1 = Net::SSH2->new(); $ssh1->connect($host1) or die "connect to SSH1 failed"; $ssh1->auth_password('user', 'pass') or die "auth1 failed"; $ssh1->tcpip($host2, 22, '127.0.0.1', 9922); my $ssh2 = Net::SSH2->new(); $ssh2->connect('127.0.0.1', 9922) or die "connect to SSH2 failed"; $ssh2->auth_password('user2', 'pass2') or die "auth2 failed"; # ... as many hops as you need ...

    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. — Cyrus H. Gordon
      Its not working for me...after a few tests i can say, that it dont understand what to do with this line:

      $ssh2->connect('127.0.0.1', 9922) or die "connect to SSH2 failed";
        this line is not working!! no idea why...i tested the other lines with if(-command-){print "ok";} and every line prints its "ok" expect the given line with tcpip
Re: Net::SSH2 Question - again-
by bgi (Sexton) on May 28, 2008 at 12:34 UTC
    No chance, i have no access to the server...

    it must be possible to connect to the first SSH Server and then to the second one?!?!

    first with username & password

    and second only with password but with ssh-option "-l"
      You can't ssh to it with username and password?!?!?!
Re: Net::SSH2 Question - again-
by bgi (Sexton) on May 29, 2008 at 07:43 UTC
    Hello

    I have now port of the second server

Re: Net::SSH2 Question - again-
by bgi (Sexton) on May 31, 2008 at 12:51 UTC
    Yesterday after i decided to stop trying around i tried the tunnel-issue with the command line tool plink (from putty) aaaaand then, just for fun, i started the script again and IT WORKED!!!!!! so i can definitely say, that there is something wrond with those tcpip-method...

    all google-user: hf and good luck
Re: Net::SSH2 Question - again-
by bgi (Sexton) on May 29, 2008 at 08:38 UTC
    ok, forget my last post *cough*

    thank for your help, guys, i will try idsfa 's suggestion first.

    Aaaaand i have direct a qustion concerning this:
    The authentication to the second server looks like this: i type "ssh -l username hostname" to the fisrt server's shell and then a field appears: "Password:" I type my password and i am in...

    How can i transfer this authentication-procedure in my script??
    The problem is the "-l" option!