in reply to Using Net::SSH2 with Net::Telnet::Cisco

That should work:
use Net::OpenSSH; my $ssh = Net::OpenSSH->new($host); my ($fh, $pid) = $ssh->open2socket(); my $conn = Net::Telnet->new(Fhopen => $fh);
Though note that Net::Telnet may expect to find a server talking the TELNET protocol at the other side of the connection but the SSH connection will just pass all the data stream to the remote shell unmodified.

Replies are listed 'Best First'.
Re^2: Using Net::SSH2 with Net::Telnet::Cisco
by ryber (Acolyte) on Jan 20, 2011 at 18:10 UTC

    Salva-

    thanks for the suggestion. My one concern is that the CPAN page for Net::OpenSSH states: 'For password authentication, IO::Pty has to be installed'. The problem I am experiencing with Net::Appliance::Session seems to come down to the fact that IO::Pty is (apparently) not thread safe. Have you tried this code in a threaded app?

    Thanks,
    Ryan

      Have you tried this code in a threaded app?

      No, I usually prefer to use processes (via fork) rather than threads, and in that environment IO::Pty has never given me any problem.

      In any case, as IO::Pty is only used on the login phase, you could just use a lock to serialize it so that not more than one thread could be using it at the same time.

      Also, if you really find that IO::Pty is not thread safe, just report it to its maintainers... and if it affects Net::OpenSSH operation I would also like to know!

Re^2: Using Net::SSH2 with Net::Telnet::Cisco
by pchel (Initiate) on Apr 05, 2011 at 01:09 UTC
    Hello. I've tried to use your code
    use Net::OpenSSH; use Net::Telnet; my $ssh = Net::OpenSSH->new('pa:abc@10.0.0.1'); my ($fh, $pid) = $ssh->open2socket(); my $conn = Net::Telnet->new(Fhopen => $fh); my @lines = $conn->cmd("sh ver"); print join "\n";
    But I got message below
    Pseudo-terminal will not be allocated because stdin is not a termina +l. command timed-out at t1.pl line 6
    Could you please give me a more detailed example?
      It seems that Net::Telnet prefers talking through a tty.

      That works for me:

      #!/usr/bin/perl use strict; use warnings; use Net::OpenSSH; use Net::Telnet; $Net::OpenSSH::debug = -1; my $ssh = Net::OpenSSH->new('localhost'); my ($fh, $pid) = $ssh->open2pty(); my $conn = Net::Telnet->new(Fhopen => $fh); my @lines = $conn->cmd("find /tmp"); print @lines; my @lines1 = $conn->cmd("ls"); print "\n\nls:\n", @lines1;
        Another late reply:

        I had to make a few adjustments to your code to get it to work properly on a Cisco device:

        use strict; use warnings; use Net::OpenSSH; use Net::Telnet; $Net::OpenSSH::debug = -1; my $promptEnd = '/\w+[\$\%\#\>]\s{0,1}$/o'; my $timeout = 60; my $ssh = Net::OpenSSH->new('your.ciscodevice.here', user => 'johndoe', password => 'secretpass', kill_ssh_on_timeout => 1, timeout => $timeout); # stderr_to_stdout was required to get Net::Telnets waitfor() to work my ($fh, $pid) = $ssh->open2pty({stderr_to_stdout => 1}); my %params = ( fhopen => $fh, prompt => $promptEnd, timeout => $timeout, errmode => 'return', telnetmode => 0, cmd_remove_mode => 1, output_record_separator => "\r", ); my $conn = Net::Telnet->new( %params ); my @lines = $conn->cmd("show tech"); print @lines;


        PS: Error checks removed, insert them at will.

        cat /dev/world | perl -e "(/(^.*? \?) 42\!/) && (print $1))"
        errors->(c)