liusz
请教如何获取文件中的特定字符串
请帮忙看看,我对perl不熟悉。
脚本的功能是:读特定目录/usr/local/bin/tmpmail/下的邮件文件,读出其中的收件人地址,然后发出去,发邮件时写条系统日志,日志包含收件人地址,发送成功与否。
文件格式如下:
Subject: test Web Security Report Mon Apr 7 20:35:31 2008
From: liuszhotmail.com
To: liuszhotmail.com
Date: Mon Apr 7 20:35:31 2008
Mime-Version:1.0
X-mailer:Linux mailagent
Content-Type:multipart/mixed;boundary="=====test_mailagent====="
--=====test_mailagent=====
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Sent from test system test on date: Mon Apr 7 20:35:31 2008
--=====test_mailagent=====
Content-Type:application/octect-stream;name="filetest.zip"
Content-Transfer-Encoding:base64
Content-Disposition:attachment;filename="filetest.zip"
希望得到文件中的mailto 地址,
我的脚本如下:
#!/usr/bin/perl -w
use File::Basename;
$myname = basename($0);
@pids=split(//n/, qx/ps -C $myname -o pid=/);
if ($#pids > 0)
{
# Exit if the last instance of this script still has not finished
exit;
}
@files = </usr/local/bin/tmpmail/*>;
foreach $file (@files)
{
if($file =~ m/(//.+)+//_lock_/)
{
next;
}
get_mailto_address($file);
send_mail($file);
}
sub get_mailto_address
{
open my $fh,"< ",$file or die;
$fh = shift;
my $line = <$fh>;
print $line, "/n";
read $fh, my $buffer, 1024;
print $buffer, "/n";
close( $fh );
}