|go ahead... be a heretic|
Perl Script to Test Telnet Connectivityby redapplesonly (Sexton)
|on Nov 08, 2022 at 21:52 UTC ( #11148050=perlquestion: print w/replies, xml )||Need Help??|
redapplesonly has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks, first-time poster, and Perl newbie here. (I'm a Java & C coder.) Thank you for reading my question.
So I am struggling to write a Perl script that uses telnet to see if a remote host is responsive. This script needn't be complex; basically, this is a "Are you alive?" test done on the remote machine. (I would normally use ICMP/ping, but my network traffic must pass through my company's firewall.) A bit of a spoiler; this question leans a lot on how to interpret the Net::Telnet module documentation. (https://metacpan.org/pod/Net::Telnet)
First, here's the behavior I'm hoping to emulate. If I manually telnet to a remote host from the command line, it looks like this:
That "SSH-2.0-OpenSSH_7.4" message tells me that the telnet session is successful, and the remote server opened a valid TCP/IP connection for me. I will have to manually issue a ^C character to break out of the session.
Okay, here's an example of an trying to contact a server which is unavailable:
Here, I get nothing back, because the remote host is down. The telnet session will ultimately fail.
So now, I contimplate how to automate these actions in Perl. Ultimately, I'd like to have a subroutine that could take a remote host by IP address (expressed as a string) and then try my telnet test:
So right away, you can prob tell that I really don't know what I'm doing. I've made my best attempt. Right now, the code fails with the error message:
Line 22 is:
I've been reading and rereading the Net::Telnet module documentation over and over, and I'm getting no-where. Does anyone have any practical advice? I'll take whatever I can get. Thank you.
Back to Seekers of Perl Wisdom