Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Passing telnet commands through SSH

by salva (Abbot)
on Apr 03, 2014 at 08:06 UTC ( #1080909=note: print w/replies, xml ) Need Help??

in reply to Passing telnet commands through SSH

You can use Net::OpenSSH to create tunnels through the SSH gateway:
my $ssh = Net::OpenSSH->new($hostname, user => $user, password => $pas +sword); my ($socket, $pid) = $ssh->open_tunnel($ip_address, 23); my $telnet = Net::Telnet->new(-fhopen => $socket, ...); $telnet->waitfor(...); ...

Replies are listed 'Best First'.
Re^2: Passing telnet commands through SSH
by wrongt (Initiate) on Jul 14, 2014 at 08:26 UTC
    Hi, i'm using Net::OpenSSH to create a tunnel via SSH gateway after that i want to connect to other remote host via telnet, here is my code:
    my $ssh = Net::OpenSSH->new('user@'); my ($socket, $pid) = $ssh->open_tunnel('', 23); my $telnet = Net::Telnet->new(-fhopen => $socket, -prompt => '/.*\$ $/', -telnetmode => 0, -cmd_remove_mode => 0, -output_record_separator => "\r"); $telnet->waitfor(-match => '/login\ name\:/', -errmode => "return") or die "login failed: " . $telnet->lastline; my @lines = $telnet->cmd("who"); print Dumper(@lines); $telnet->close;
    but code is always stop on this line: or die "login failed: " . $telnet->lastline;
    i get the following output:login failed:  at line 21.
    Could you please help me to successfully achieve connection to telnet host. Thank you.

      As the error says, your login fails.

      If you are really, really sure that your username and password are correct, then most likely the prompt is not login name: but something else. Find that and correct that.

        I suppose telnet session filed because on the system where script is running have version of OpenSSH 4.3, but Net::OpenSSH module need OpenSSH 5.4 and upwards for creating tunnels. In case that i didn't have possibility to upgrade OpenSSH version is any alternatives way to create an ssh tunnel to gateway after that use tunnel to establish a telnet connection to remote host?
          Is netcat or socat or any other similar utility available on the remote side? you can use then to forward stdio to some remote TCP server.

          If Perl (or any other scripting language) is available on the remote side, you could also write your own netcat. See golf challenge: one-liner netcat clone

    Log In?

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: note [id://1080909]
    and a soft breeze sighs...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (4)
    As of 2017-11-21 07:10 GMT
    Find Nodes?
      Voting Booth?
      In order to be able to say "I know Perl", you must have:

      Results (296 votes). Check out past polls.