CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Modify a Perl script to validate a SMTP connection

 



ed_runs
New User

Sep 2, 2015, 11:58 AM

Post #1 of 7 (1752 views)
Modify a Perl script to validate a SMTP connection Can't Post

Hello all,

I have a script that I need to modify. It is a script that is provided by my load balancing appliance but is woefully inadequate. The script in its current form simply checks that there is some sort of connection on 25. I want it to connect, read the banner, and if the banner contains "ESMTP MAIL Service ready" then sign off. I am able to create the code to connect and read the banner but don't have the experience to integrate this into the existing script. Any help or suggestions is appreciated.

Here is the original script:


Code
#!/usr/bin/perl -w 
################################################################
##
## Copyright 1998-2005 NetScaler, Inc. All rights reserved.
## This software and documentation contain valuable trade
## secrets and proprietary property belonging to NetScaler, Inc.
## None of this software and documentation may be copied,
## duplicated or disclosed without the express
## written permission of NetScaler, Inc.
##
################################################################
## This is a netscaler supplied script. Please dont modify this as it will be overwritten during
## reboot. If you want to modify, please make a copy of this script and modify.

## This script is used to do smtp monitoring using KAS feature.

use strict;
use Net::SMTP;
use Net::SMTP6;
use Netscaler::KAS;

sub is_ipv4_address
{
my $address = $_[0];
if ($address =~ m/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/) {
return 1;
}
return 0;
}

## This function is a handler for performing smtp probe in KAS mode
sub smtp_probe
{

## There must be at least 4 arguments to this function.
## 1. First argument is the IP that has to be probed.
## 2. Second argument is the port to connect to.
## 3. Timeout, it is present in index 3

if(scalar(@_) < 2)
{
return (1,"Invalid number of arguments");
}

## Try to connect to the server
my $smtp;
if (is_ipv4_address($_[0])) {
$smtp=Net::SMTP->new($_[0].":".$_[1],Timeout=>$_[3])
or return (1,"Unable to connect to server - $!");
## Probe succeeded
$smtp->quit;
return 0;
}
else { #IPV6 adress
$smtp=Net::SMTP6->new($_[0], PeerPort => $_[1], Timeout=>$_[3])
or return (1,"Unable to connect to server - $!");
## Probe succeeded
$smtp->quit;
return 0;
}
}

## Register smtp probe handler, to the KAS module.
probe(\&smtp_probe);


This is what I would like to integrate into this:


Code
$banner = $smtp->banner(); 
if (index($banner, 'ESMTP MAIL Service ready') eq -1) {return (1,"Unable to connect to server - $!");}


Thanks in advance for your assistance.

~Ed


FishMonger
Veteran / Moderator

Sep 2, 2015, 12:32 PM

Post #2 of 7 (1749 views)
Re: [ed_runs] Modify a Perl script to validate a SMTP connection [In reply to] Can't Post

You would need to add your code just before the $smtp->quit; statement.


ed_runs
New User

Sep 2, 2015, 12:39 PM

Post #3 of 7 (1747 views)
Re: [FishMonger] Modify a Perl script to validate a SMTP connection [In reply to] Can't Post

That is exactly what I did but it did not work. When I added it in like that and uploaded it to the Netscaler and assigned it to replace the original, it did not work. The servers show as unavailable. Unfortunately, I don't get any feedback/response on the Netscaler.

Thanks


FishMonger
Veteran / Moderator

Sep 2, 2015, 12:59 PM

Post #4 of 7 (1741 views)
Re: [ed_runs] Modify a Perl script to validate a SMTP connection [In reply to] Can't Post

Did you put it in both places?

Saying "it did not work" is meaningless without a proper explanation on how it failed.

Which line/statement was it executing when you received the error message?

Why are you using "Unable to connect to server - $!" as the error message in your added statement? That is a misleading message because it doesn't have anything to do with your index statement.

Did you add any debugging statements such as outputting the value of $banner to see if it holds what you think?

eq is a string comparison operator, not a numeric comparison operator. To check a numeric value use == instead of eq.


ed_runs
New User

Sep 2, 2015, 1:07 PM

Post #5 of 7 (1739 views)
Re: [FishMonger] Modify a Perl script to validate a SMTP connection [In reply to] Can't Post

I did put it in both places.

I do not see how it failed. The Netscaler runs this in the background. All I see is that it reports the server as unavailable once I apply this script in place of the original. If there is a way to manually run the script from the Netscaler I do not know how. I tried to replicate as much as I could on my windows box but it is not even close to being a apples to apples compassion.
The reason I chose the "unable to connect to server -$!" as the error message was because that is the text Citrix used in the original script. I agree it is misleading but I was trying to keeps things as inline with what already existed.

I will modify the script to use == instead of eq.

Thanks for all your feedback. I am listening and learning.

~Ed


FishMonger
Veteran / Moderator

Sep 2, 2015, 1:21 PM

Post #6 of 7 (1736 views)
Re: [ed_runs] Modify a Perl script to validate a SMTP connection [In reply to] Can't Post

$banner needs to be declared otherwise it will generate a compilation error. Use the my keyword to declare the var.


Code
my $banner = $smtp->banner();


Change:

Code
if (index($banner, 'ESMTP MAIL Service ready') eq -1) {return (1,"Unable to connect to server - $!");}


To:

Code
if (index($banner, 'ESMTP MAIL Service ready') == -1) { 
return (1, "'ESMTP MAIL Service ready' not found in <$banner>");
}



ed_runs
New User

Sep 2, 2015, 2:14 PM

Post #7 of 7 (1733 views)
Re: [FishMonger] Modify a Perl script to validate a SMTP connection [In reply to] Can't Post

Thank You

It is working now.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives