我是Perl和套接字编程的新手,这是我尝试实现的,所以如果脚本无法连接到IP 192.168.0.100和端口5000,它会触发基于子邮件子例程的电子邮件。我假设非零退出代码意味着失败,但我正在阅读一些文档,并在Perl中提到如果socket没有建立它返回undef值,所以我尝试匹配($?== undef)以及($?! = 0)如下所述,但它不起作用。我不知道如何匹配退出状态以便它会触发电子邮件,任何帮助都表示赞赏http://www.perlmonks.org/bare/?node_id=398525
#!/usr/bin/perl
use IO::Socket::INET;
$| = 1;
my ($socket,$client_socket);
$socket = new IO::Socket::INET (
PeerHost => '192.168.0.100',
PeerPort => '5000',
Proto => 'tcp',
) or die "ERROR in Socket Creation : $!\n";
eval { my $size = $socket->send("Hello"); };
if ((my $size > 0) || $@) {
&send_email;
}
sub subemail
{
my $subject = "ERROR in Socket Creation";
my $body = "\n Check if the logstash server is up \n\n";
my $smtp_server = $ENV{"ET_SMTP_SERVER"};
my ($from, $to) = ("test\@example.com", "testing\@example.com");
unless ( defined( $smtp_server ) )
{
$smtp_server = 'mailhost.example.com';
}
my $smtp = Net::SMTP->new( $smtp_server );
$smtp->mail($from);
$smtp->to($to);
$smtp->data();
$smtp->datasend( 'To: ' . $to . "\n" );
$smtp->datasend( 'From: ' . $from . "\n" );
$smtp->datasend( 'Subject: ' . $subject . "\n" );
$smtp->datasend( "\n" );
$smtp->datasend( $body );
$smtp->datasend( "\n" );
$smtp->dataend();
$smtp->quit;
}
答案 0 :(得分:0)
它所指的回报可能来自new
本身。所以你要检查:
subemail() if not defined $socket;