Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Forced to modify perl telnet script to use both telnet and ssh

by Mr. Muskrat (Canon)
on Sep 01, 2012 at 15:23 UTC ( #991164=note: print w/replies, xml ) Need Help??

in reply to Forced to modify perl telnet script to use both telnet and ssh

I'll give you a basic example that expands a bit on what Illuminatus said about try one and then the other.

# in pseudocode no less! get $deviceIP ping $deviceIP or report it as not responding and exit ssh $deviceIP if ssh error then if ssh error is 'connection refused' then telnet $deviceIP or report error and exit do work with telnet connection else report error and exit end if end if do work with ssh connection

If you already know which devices use telnet and which use ssh then things get even simpler. Just try the correct protocol!

Update: Oops. I forgot the most important part.

You will not get any good responses until you can give us some clear information regarding your problem and code that demonstrates said problem.

  • Comment on Re: Forced to modify perl telnet script to use both telnet and ssh
  • Download Code

Replies are listed 'Best First'.
Re^2: Forced to modify perl telnet script to use both telnet and ssh
by afoken (Abbot) on Sep 01, 2012 at 17:51 UTC
    ping $deviceIP or report it as not responding and exit

    Just a little, slightly off-topic bean counting. Some devices prefer not to respond to pings, yet they readily serve telnet, ssh, http or other services. Yes, this is stupid behaviour, nevertheless it happens, mostly because some clueless people dictated stupid "firewall" rules.

    I would omit the ping test, because it is generally useless. If you get a ping reply, you still don't know what services are supported, you only know that the device was reachable shortly after you sent the ping package. If you don't get a ping response, you know nothing. The device may be shut down, offline, firewalled, or misconfigured. In both cases, you still have to try ssh and telnet. Omitting ping reduces the amount of code needed and wastes slightly less network resources.

    Another different thing: Because ssh is encrypted and telnet is not, I would prefer to connect via ssh first, and only if that fails fall back to telnet.

    I would only try telnet first if I had to work with a known set of machines where telnet is much more likely enabled than ssh. But then again, if I know the machines, and know that they always respond to pings when they are up and running, a ping test may be faster then failing to establish an ssh connection.


    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Using ping first has become a habit for me since all of the devices we use at work respond to pings. We have a longer ssh timeout so when devices really are down, we find out sooner if we ping rather than connecting directly. (The longer timeout is due to some devices running Windows which takes longer to setup the initial ssh connection.)

      I did recommend using ssh first.

      Sorry I wasn't clear. We currently do have a working Perl/telnet script. Due to new security requirements we are moving to SSH. So, not all devices support SSH but will sometime in the future. Therefore I have to have a single script to handle both Telnet and SSH. (Lucky me). As stated, my dinosaur of a boss (he refuses to use new technology such as ZFS on Solaris) wants to "simply" modify the current script to handle both connections using the existing commands.

        Please go through your script adding statements like this:

        if ($device->{supportsSSH}) {SSH code} else {Telnet code}

        After a while you may notice that blocks of code keep getting repeated. Please consider refactoring them into sub {}'s as you work through your script.

Re^2: Forced to modify perl telnet script to use both telnet and ssh
by essej1 (Novice) on Sep 05, 2012 at 17:32 UTC
    The base issues are that we are moving to ssh from telnet, but not all at once and my boss refuses to make major changes. So, I've been tasked to implement expect like things without using expect. What I'm after is something like:
    send command read response send another command
    The devices being examined give different prompts or responses, depending on firmware or vendor. Therefore there are different follow-up commands. The following does work, but note the comments about
    #!/opt/csw/bin/perl use strict; use warnings; use Net::SSH2; my $host = "<host name or IP>" my $user = "<user ID>"; my $pass = "<user password>"; my $ssh2 = Net::SSH2->new(); my $ok = 1; print "==> $host\n"; $ssh2->connect($host) or $ok = 0; if (!$ok) { print "Probable telnet\n"; exit(1); } if (!$ssh2->auth_password ($user,$pass)) { print "==> 4 pass fail\n"; exit(1); } my $chan2 = $ssh2->channel(); $chan2 -> shell(); # form some reason the order of the commands matters. # change any one and the prints are blank print $chan2 "dir\n"; sleep (1); # for some reason the sleep is needed to get output print "LINE : $_" while <$chan2>; print $chan2 "sh ver\n\n"; print "LINE : $_" while <$chan2>; print $chan2 "sh clock\n\n"; print "LINE : $_" while <$chan2>; print $chan2 "exit\n"; print "==> END\n"
    Paradise: Florida, full tank of gas, no appointments.
      Hi, I have written a module which integrates both SSH & Telnet; it's an overlay which uses both Net::Telnet and Net:SSH2; it's called Control::CLI. Maybe that might help you. I made it because I needed it to run on Windows systems, where I was not able to get expect to work.
        can u share the script that uses both ss and telnet... even am expecting to do

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991164]
[Discipulus]: I hope at least a minimal implementation, in the middle of july i'll have the first introductional course..
Discipulus nemo profeta in patria..

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2017-06-27 09:18 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (601 votes). Check out past polls.