Question

I am new at perl scripting, I have to add a telnet socket to the perl script below.

If sending mail from a host fails it opens a ticket. The server info is gathered from a database. But i want to add a telnet socket here. İf sending the mail fails, I want to try telnet to telnet to the server. İf it connects, do not open the ticket, and if it fails, open it.

is this possible?

Here's the script:

foreach my $id(keys %server)
{
    $sth->execute($id) ;
    my $status = ($sth->fetchrow_array)[0] ;
    $sth->finish() ;
    if (!defined($status)) # Insert it!
    {
        $sth2->execute($id) or die "$runTime - $DBI::err\n" ; 
        $status = 1 ; # first time up
    }

    my $currtime = $time{'yyyy-mm-dd hh:mm:ss'} ;
    print "$server{$id}->{server}, $server{$id}->{port}, $server{$id}->{sender}, $server{$id}->{recipient}\n";

    my $currstat = mySendMail($server{$id}->{server}, $server{$id}->{port}, $server{$id}->{sender}, $server{$id}->{recipient}) ;

    if(defined($currstat)) #Open Ticket 
    {
        $currstat =~ s/[\r\n]+$// ; 
        $currstat =~ s/[\r\n]+/;/g ;
        $currstat = substr($currstat, 0 , 1000) ;
        #cawto "AHDNEW $server{$id}->{asset} mail_check down $currstat", "n=jerry", "c=red" ;
        cawto "AHDNEW $server{$id}->{asset} mail_check down $currstat", "n=serverticket", "c=red" ;
        cawto "AHDNEW $server{$id}->{asset} mail_check down $currstat", "n=sdyydb1", "c=red" ;
        $sth3->execute(2, $currtime, $currstat, $id) or die "$runTime - $DBI::err\n" ; 
        print STDERR "$runTime : $server{$id}->{asset} $currstat- \n" ;
        if ($server{$id}->{asset} =~ /^msite/)
        {
            #cawto "msitefe service recycle $server{$id}->{asset}", "c=red" ;
            print `start C:tool \\service.exe\\\\$server{$id}->{asset} -u msite\\script`;
            sleep 60;
            print `start C:tool \\service.exe\\\\$server{$id}->{asset} -u msite\\script`;           
        }

    }
    else
    {
        print STDERR "$runTime : $server{$id}->{asset} success- \n" ;
        if ($status > 1) #Ticket close
        {

            cawto "AHDNEW $server{$id}->{asset} mail_check up", "n=serverticket", "c=red" ;
            cawto "AHDNEW $server{$id}->{asset} mail_check up", "n=sdyydb1", "c=red" ;
        }
        $sth3->execute(1, $currtime, '', $id) or die "$runTime - $DBI::err\n" ;   

    }



}
Was it helpful?

Solution

Net::Telnet is your friend. This should get you started:

use Net::Telnet;

my $domain = 'mail.example.com'; 
$smtp = Net::Telnet->new(
    Host=>$domain,
    Port=>'25',
    Timeout => 30,
    Errmode => 'return',
);

if ($smtp) {
#   $line = $smtp->getline;
    $smtp->close();
    print "It's there\n";
}
else { print "Something went wrong\n" }

For more info, check out Net::Telnet

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top