Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Passing telnet commands through SSH

by salva (Monsignor)
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(...); ...


Comment on Re: Passing telnet commands through SSH
Download Code
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@1.1.1.2'); my ($socket, $pid) = $ssh->open_tunnel('1.2.0.1', 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 test.pl 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?
    Username:
    Password:

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: note [id://1080909]
    help
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (8)
    As of 2014-08-20 05:41 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (105 votes), past polls