最近一直很鬱悶,要維護10所大學伺服器,假如伺服器的磁碟滿了,或者其它原因,能不能早點發現呢?我就想到了,弄個腳本,監控伺服器,有異常時,發封郵件給我,就OK了。我原來一直想使用系統自帶的sendmail來實現我的想法,由於對sendmail不熟,一直弄不好。只到今天,嘿嘿,在看一個網站時,看到使用php發郵件的方法。記得裝軟體時,沒有裝php,但是有裝perl啊,就這麼辦,使用perl來發郵件。過程如下:
1、登錄系統
2、查詢是否有安裝perl
[root@CentOS ~]# rpm -qa | grep perl
perl-5.8.5-36.RHEL4
3、OK,來檢查一下網路
[root@CentOS ~]# ping act0r.bokee.com
PING act0r.bokee.com (58.49.57.34) 56(84) bytes of data.
64 bytes from 58.49.57.34: icmp_seq=0 ttl=54 time=128 ms
4、安裝發郵件所需要的模塊
[root@CentOS ~]# perl -MCPAN -e shell
基本上是一路回車,只有到最後面時,會讓你選擇你所在的區域,及國家。
cpan>install Authen::SASL
cpan>q
5、vi /etc/init.d/sendmail.sh
#!/usr/bin/perl
use Net::SMTP;
my $mailhost = "smtp.163.com"; # SMTP伺服器
my $mailfrom = 'notfour@163.com'; # 你的EMAIL地址
my @mailto = ('fayland@gmail.com', 'not_four@hotmail.com'); # 收信人地址
my $subject = "此為標題";
my $text = "此為正文\n第二行位於此。";
$smtp = Net::SMTP->new($mailhost, Hello => 'localhost', Timeout => 120, Debug => 1); #為0時,不輸出調試信息
# anth login, type your user name and password here
$smtp->auth('user','pass'); ###用戶名和密碼
foreach my $mailto (@mailto) {
# Send the From and Recipient for the mail servers that require it
$smtp->mail($mailfrom);
$smtp->to($mailto);
# Start the mail
$smtp->data();
# Send the header
$smtp->datasend("To: $mailto\n");
$smtp->datasend("From: $mailfrom\n");
$smtp->datasend("Subject: $subject\n");
$smtp->datasend("\n");
# Send the message
$smtp->datasend("$text\n\n");
# Send the termination string
$smtp->dataend();
}
$smtp->quit;
6、給sendmail執行許可權
[root@CentOS ~]# chmod 755 /etc/init.d/sendmail.sh
7、測試
[root@CentOS ~]# /etc/init.d/sendmail.sh
8、嘿嘿,看看信發送了沒有。
[火星人
]
在Linux下使用Perl發信郵件已經有1366次圍觀
http://coctec.com/docs/linux/show-post-202332.html