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

Passing telnet commands through SSH

by SiliconeClone (Initiate)
on Apr 02, 2014 at 19:01 UTC ( [id://1080823]=perlquestion: print w/replies, xml ) Need Help??

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

Ok I have scoured the web for an answer and perhaps I just haven't liked what I found lol. But my situation is as follows. I have different environments that kind of works like this. login-environment - can telnet into node prod-environment - can not ( can't even ping due to permissions on that one.) So I was attempting tp SSH into "login" and from there telnet into the desired node to pass my commands"
sub turnOnOff { my ($self,$args) = @_; my $cmd = shift; my $telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die'); my $ssh = Net::SSH::Perl->new("$hostname", debug=>0); $ssh->login("$username","$password"); $ssh->cmd($telnet->open($self->{'ipAddress'})); $ssh->cmd($telnet->waitfor('/[\$#%:><][\s\b]+$/')); $ssh->cmd($telnet->print($loginUser)); $ssh->cmd($telnet->waitfor('/[\$#%:><][\s\b]+$/')); $ssh->cmd($telnet->print($userPW)); $ssh->cmd($telnet->waitfor('/[\$#%:><][\s\b]+$/')); $ssh->cmd($telnet->print("$self->{'strOnOff'} 1")); $ssh->cmd($telnet->waitfor('/[\$#%:><][\s\b]+$/')); $ssh->cmd($telnet->print("y")); $ssh->cmd($telnet->waitfor('/[\$#%:><][\s\b]+$/')); $ssh->cmd($telnet->print("logoff")); return 1; }
Is this even possible?

Replies are listed 'Best First'.
Re: Passing telnet commands through SSH
by salva (Canon) on Apr 03, 2014 at 08:06 UTC
    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(...); ...
      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?
Re: Passing telnet commands through SSH
by snoopy (Curate) on Apr 02, 2014 at 22:06 UTC
    Hi There,

    One approach that might come in handy here is SSH tunneling. This is a technique for directing a remote SSH connection back to your local server.

    To recap, you have ssh access to an intermediate host that has telnet access to the destination machine. Telnet normally runs on port 23.

    Hopefully your local host is a 'Nix machine, which means that you can use a recipe such as this to set up port forwarding: Setting up an SSH tunnel

    E.g. in another window: % ssh -g -L 10023:telnet-host:23 ssh-host

    Telnet will then appear to be running locally on port 10023:

    % telnet localhost 10023 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. .... login:
    You can then adapt you script to connect to telnet on your localhost on port 10023.
    #!/usr/bin/perl use warnings; use strict; use Net::Telnet; my ($username, $password) = @ARGV; die "usage $0 username password" unless $username && $password; my $telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die', Port => 10 +023); $telnet->open('localhost'); $telnet->waitfor('/[\$#%:><][\s\b]+$/'); print "found login prompt..."; $telnet->print($username); $telnet->waitfor('/[\$#%:><][\s\b]+$/'); print "found password prompt..."; $telnet->print($password); print "logged in yay!\n";
    Hope this helps.

      Going to try this now. Thanks

      And yes your recap is dead on

      Ok this worked! However, I need it to work programatically. So i attempted to integrate the tunnel into the program.

      It opens fine, I can be on the sshHost machine and see the tunnel up and running. However the program itself locks at that point. Sadness, right?

      I can not figure out why it is locking. I have a sub that opens the tunnel, and then after that it should run the telnet sub (which works if I open the tunnel manually). At that point it just hangs. Any more suggestions?

      It is hanging at the following

      channel 1: open confirm rwindow 0 rmax 32768

      This only happens when I open the tunnel. If I send other commands like "who", etc.. it does not hang at all.

        I'm totally just guessing here - no real experience in this arena - but you might try running tunnel connection as a child process (or maybe the parent spawns a child to run telnet, and then the parent opens the tunnel? I'm clueless on that).

        Anyway it sounds like the program hangs because it's waiting for i/o on one connection, and so cannot do anything on the other. In my limited experience using SSH tunnels (interactively only, never via scripts), the tunnel ssh connection is always running separately (e.g. in another shell) from whatever process is supposed to actually do stuff using the tunnel.

      I finally got it working; for the most part lol

      It is still hanging after creating the tunnel. I think this is because the SSH after the tunnel is open flips to the host machine.

      ie I am in "Dev" environment, so I log into that via ssh. Send my tunnel command to "login" environment, at this point it hangs, but it did open the pid. I think it has something to do with the flipping, same reason I couldn't directly send the telnet through the SSH connection originally

      To get around this I have forked the SSH sub, so that my telnet stuff will run regardless. Since it wouldn't wait for the sub routine what I ended up doing was adding a small sleep timer to give the tunnel time to establish

      Thanks for your help!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1080823]
Approved by GotToBTru
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-20 04:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found