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.
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
| [reply] |
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.
| [reply] |
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.
| [reply] |
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. | [reply] [d/l] |
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.
| [reply] [d/l] [select] |
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.
| [reply] |
can u share the script that uses both ss and telnet...
even am expecting to do
| [reply] |