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

I'm trying to convert a script that was using Net::Telnet to now use Net::SSH2 but there are some differences in the commands that I'd like some advice on.

Here is an example that I have from the script. The first thing that doesnt work is "print" but there may be more.
$t->connect($jump_host); $t->auth_keyboard($jump_user, $jump_pass); $t->print("telnet $host"); } else { (undef, $host) = split /\s+/, $opte; $type = 'opte'; $t->open($host); } $t->waitfor('/:/'); $t->print($z{$type}{'user'}); $t->waitfor('/:/'); $t->cmd($z{$type}{'pass'}); $t->print('enable'); $t->waitfor('/:/'); $t->cmd($z{$type}{'sudo'}); $t->cmd('terminal length 0'); $t->cmd('terminal width 512'); $t->prompt('/[\$#] *$/');
In this case $t is the Net::SSH2 connection. Can I reuse my code in a reasonable way or do I need to significantly redo this?

Replies are listed 'Best First'.
Re: Changing a script from Net::Telnet to Net::SSH2
by perlfan (Parson) on Mar 03, 2016 at 21:08 UTC
    telnet and ssh operate and different levels. telnet is a client for interacting with various daemons and it typically very low level; for example, it is useful for talking directly with things like an http or mail server. You are basically participating in the protocol exchange directly.

    ssh on the other hand, is a client to sshd; this provides a secure access to the userland shell of the system you're logging into (among other things); for example, you can telnet over an ssh tunnel (which it sounds like what you might need to do in order to retain your functionality).

    See A little demo for Net::SSH2 for a Net::SSH2 demonstration. For some perspective on the differences between telnet and ssh, check out http://unix.stackexchange.com/questions/80028/what-is-the-equivalent-of-telnet-localhost-25-with-ssh.

      Essentially what I have to do is SSH2 to the jumphost. Then from there I can use telnet to get to individual devices. Previously it was possible to get to jumphost via telnet and that is what this script was using. I'd like to figure out a way to do the SSH2 to jumphost bit then do the telnet to devices etc. The difference seems to be the way it handles 'printing' the commands I need to run. Most examples I found do not use the $ssh2-> type of setup which is what ours was using. For example "$t->print" says it is not a valid SSH2 macro.

        I completely misunderstood what you were trying to do. Based on your problem description, I thought you were connecting to something via telnet and now wanted to connect via SSH. But what you're now describing is needing to SSH into one system and then from there telnet into a second system. That's a completely different scenario.

        Now that I understand what it is you're trying to do, I agree with perlfan that creating an SSH tunnel is probably what you might want to consider. After you create the tunnel, your existing code using Net::Telnet just needs to connect to the local port of the SSH tunnel.

Re: Changing a script from Net::Telnet to Net::SSH2
by dasgar (Priest) on Mar 03, 2016 at 19:30 UTC

    Take a look at Control::CLI. It uses Net::Telnet and Net::SSH2 for telnet and ssh connections respectively. There's a chance that the syntax for Control::CLI maybe close enough to that of Net::Telnet that you could switch over to use Control::CLI in your code for ssh connection with minimal code change.

Re: Changing a script from Net::Telnet to Net::SSH2
by salva (Canon) on Mar 04, 2016 at 07:14 UTC
      I may try that if I can get it to install. I'm using a rather cranky Solaris 10 server and havent had much luck getting anything new to install on it.
        On Solaris you have to ensure that the OpenSSH client is installed. The one from SUNOracle is an old fork of the OpenSSH one that does not provide some features required by Net::OpenSSH.

        Then, you will have to tell Net::OpenSSH where to find it. For instance:

        my $ssh = Net::OpenSSH->new($host, ssh_cmd => '/opt/OpenSSH/bin/ssh');