您的位置:首页 > 运维架构 > Shell

shell script自动搭建简单的mail服务器(postfix+dovecot)

2013-03-04 17:22 429 查看
#!/bin/bash

#

#Shell name:config_mail.sh

#

#Program:

# config mail server by this shell script

#

#Author:perofu

#

#Mail: perofu.com@gmail.com

#

#History:

# 2012/10/14

#

#NOTE:

#

#Please check some right strings in your /etc/postfix/main.cf and

#/etc/dovecot/dovecot.conf to change it in this shell script,

#

#Not in below part

######################################################

# /etc/postfix/main.cf

######################################################

#

# #myhostname = host.domain.tld

# #mydomain = domain.tld

# #myorigin = $myhostname

# #myorigin = $mydomain

# #inet_interfaces = all

# inet_interfaces = localhost

# mydestination = $myhostname, localhost.$mydomain, localhost

# #mynetworks = 168.100.189.0/28, 127.0.0.0/8

# #relay_domains = $mydestination

#

######################################################

# /etc/dovecot/dovecot.conf

######################################################

#

# #protocols = imap pop3 lmtp

#

######################################################

#If you have any opinions please contact me

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:.

export PATH

SHELL_NAME=$(basename $0)

PC=/etc/postfix/main.cf #postfix configuer

DC=/etc/dovecot/dovecot.conf #dovecot configuer

if [ $UID -ne 0 ]

then

echo "Run as root"

exit 1

fi

if [ "$#" -ne "0" ]

then

if [ "$#" -ne "3" ]

then

echo "Usage:"

echo "A ./${SHELL_NAME}"

echo "B ./${SHELL_NAME} hostname domain_name subnet"

echo

exit 77

else

hn=$1

dm=$2

sn=$3

fi

else

read -p "Please input your host name like [mail.example.com] " hn

read -p "Please input your domain name like [example.com] " dm

read -p "Please input your subnet like [10.0.0.0/24] " sn

fi

#install postfix and dovecot

yum install postfix* dovecot* -y &> /dev/null

cp $PC $PC.bak #backup

cp $DC $DC.bak

#check some right strings in your /etc/postfix/main.cf and write in there

grep "#myhostname = host.domain.tld" $PC |sed -i "s/#myhostname = host.domain.tld/myhostname = ${hn}/" $PC

grep "#mydomain = domain.tld" $PC |sed -i "s/#mydomain = domain.tld/mydomain = ${dm}/" $PC

grep "#myorigin = \$myhostname" $PC |sed -i "s/#myorigin = \$myhostname/myorigin = \$myhostname/" $PC

grep "#myorigin = \$mydomain" $PC |sed -i "s/#myorigin = \$mydomain/myorigin = \$mydomain/" $PC

grep "#inet_interfaces = all" $PC |sed -i "s/#inet_interfaces = all/inet_interfaces = all/" $PC

grep "inet_interfaces = localhost" $PC |sed -i "s/inet_interfaces = localhost/#inet_interfaces = localhost/" $PC

grep "#mynetworks = 168.100.189.0/28, 127.0.0.0/8" $PC |sed -i "s%#mynetworks = 168.100.189.0\/28, 127.0.0.0\/8%#mynetworks = ${sn}, 127.0.0.0\/8%" $PC

grep "#relay_domains = \$mydestination" $PC |sed -i "s/#relay_domains = \$mydestination/relay_domains = \$mydestination/" $PC

grep "^mydestination = $myhostname, localhost.$mydomain, localhost" /etc/postfix/main.cf |sed -i "s/^mydestination = \$myhostname, localhost.\$mydomain, localhost/mydestination = \$myhostname, \$mydomain/" /etc/postfix/main.cf

#check some right strings in your /etc/dovecot/dovecot.conf and write in there

grep "#protocols = imap pop3 lmtp" $DC |sed -i "s/#protocols = imap pop3 lmtp/protocols = imap pop3 lmtp/" $DC

#check sendmail server and stop it

if [ -e /usr/sbin/sendmail.sendmail ]

then

/etc/init.d/sendmail stop &> /dev/null

chkconfig sendmail off

yes 2 | alternatives --config mta &> /dev/null

/etc/init.d/postfix restart &> /dev/null

chkconfig postfix on

/etc/init.d/dovecot restart &> /dev/null

chkconfig dovecot on

else

/etc/init.d/postfix restart &> /dev/null

chkconfig postfix on

#MTA

yes 1 | alternatives --config mta &> /dev/null

/etc/init.d/dovecot restart &> /dev/null

chkconfig dovecot on

fi

echo "Enjoin mail-server"

grep "^mydestination = $myhostname, localhost.$mydomain, localhost" /etc/postfix/main.cf |sed -i "s/^mydestination = \$myhostname, localhost.\$mydomain, localhost/mydestination = \$myhostname, \$mydomain/" /etc/postfix/main.cf

本文出自 “无咎” 博客,请务必保留此出处http://perofu.blog.51cto.com/6061242/1144586
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: