linux邮件系统(报警) POSTFIX AND DOVECOT

postfix 邮件服务器和shell邮件报警
一个系统里只能有一个邮件服务器

首先卸载系统自带的sendmail,或者关闭服务
安装postfix 邮件
yum -y install postfix*

注意:postfix 只能提供25号端口的进程和程序
     接收邮件的110端口是由dovecot来提供的进程和程序

安装dovecot
yum -y install dovecot*


postfix 配置和服务

cd  /etc/postfix/
vi  main.cf
修改
myhostname  = mail.g.cn (你的主机名)
mydomain = g.cn (你的域名)
myorigin = $myhostname   (你的发送邮件后缀)
myorigin = $mydomain     (你的发送邮件后缀)
inet_interfaces = all (25端口在所有端口监听)
mydestination = $myhostname,$mydomain         (收件人的邮件后缀)
mynetworks = 192.168.1.0/24 , 127.0.0.0/8  (服务器所处网段)
relay_domains = $mydestination (允许中继转发)

重启服务
service  postfix restart
查看25号端口
netstat  -tunpl | grep 25
发现进程叫做master

改变默认邮件服务器
[root@localhost ~]# alternatives --config mta

There is 1 program that provides 'mta'.

  Selection    Command
-----------------------------------------------
*+ 1           /usr/sbin/sendmail.postfix
Enter to keep the current selection[+], or type selection number: 1

这样25号端口的发信就设置好了


配置和启动dovecot
vi  /etc/dovecot/dovecot.conf
修改protocols  把#号去掉 保存
service dovecot restart
chkconfig dovecot on

现在在linux系统中创建系统用户
useradd  user1
passwd  user1
useradd  user2
passwd  user2
当这2个用户创建好了 根据上面的邮件设置 我们的系统会自动给这2个用户创建一个系统邮箱 叫 user1@g.cn ,user2@g.cn

通过outlook 添加pop3的邮件用户 分别为user1,user2 收发服务器全都写上linux的服务器主机ip 这样就可以测试了

在linux中通过postfix发邮件
第一种方式:
 首先安装telnet-server ,telnet
yum -y install telnet-server*
yum -y install telnet
启动服务(也可以通过/etc/xinetd.d/telnet 这个配置文件把disable改成 no来启动)
chkconfig telnet on
service xinetd restart(这个是telnet服务端的服务进程)


[root@localhost init.d]# telnet 127.0.0.1 25 [登录25号端口写邮件]
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
220 mail.g.cn ESMTP Postfix
mail from:root@g.cn  [邮件的发件人 回车]
250 2.1.0 Ok
rcpt to:user1@g.cn   [邮件的收件人回车]
250 2.1.5 Ok
data               [输入内容指令 回车]
354 End data with <CR><LF>.<CR><LF> 
root -> user1   [邮件内容 回车]
.             [以点来结束邮件内容]
250 2.0.0 Ok: queued as AD3523B0E
quit                [退出登录]
221 2.0.0 Bye
Connection closed by foreign host.
[root@localhost init.d]#

这样信件就发送成功了
使用
mail命令查看邮件状态
q退出

编写测试脚本
#!/bin/bash
#disk.sh

#发送主题为shell.... 内容为test的邮件给user1
echo " test" |mail user1@g.cn  -s  "shell...."

给脚本执行权限
chmod a+x disk.sh

编写报警脚本
touch apache.sh
touch mysql.sh
touch disk.sh

chmod a+x apache.sh mysql.sh disk.sh

apache.sh
#!/bin/bash
#apache.sh
nc  -w2  localhost 80
if [ $? -ne 0 ];then
  echo "apache's 80 port is down,please restart apache process"|mail user1@g.cn  -s  "apache is shutdown"
  service httpd restart
fi

mysql.sh
#!/bin/bash
#mysql.sh
nc  -w2  localhost 3306
if [ $? -ne 0 ];then
  echo "mysql 3306 port is down,please restart mysql process"|mail user1@g.cn  -s  "mysql is shutdown"
  service mysqld restart
fi

查看磁盘状态命令 df
disk.sh
#!/bin/bash
#disk.sh
#取出df命令的结果第三行第四列的值
num = df |awk 'NR==3{print int($4)}'  
if [ $num -gt 22 ]
then
   echo "disk space is ${num}% , now > 22%" |mail user1@g.cn -s "disk space alerts"
fi

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值