3unit11

eg:虚拟账户之间互相收发邮件

[root@mail-westos ~]# systemctl start mariadb

[root@mail-westos ~]# systemctl start httpd

[root@mail-westos ~]# systemctl start dovecot

[root@mail-westos ~]# cd /home/vmail/

您在 /var/spool/mail/root 中有新邮件

[root@mail-westos vmail]# ls

163.com  redhat.com

[root@mail-westos vmail]# mail westos@linux.com

Subject: 2333

sdfafqeaw

.

EOT

[root@mail-westos vmail]# mailq

Mail queue is empty

[root@mail-westos vmail]# ls

163.com  linux.com  redhat.com

[root@mail-westos vmail]# cd linux.com/

[root@mail-westos linux.com]# ls

westos

[root@mail-westos linux.com]# cd /etc/dovecot/conf.d/

[root@mail-westos conf.d]# ls

10-auth.conf       20-imap.conf                 auth-dict.conf.ext

10-director.conf   20-lmtp.conf                 auth-ldap.conf.ext

10-logging.conf    20-pop3.conf                 auth-master.conf.ext

10-mail.conf       90-acl.conf                  auth-passwdfile.conf.ext

10-master.conf     90-plugin.conf               auth-sql.conf.ext

10-ssl.conf        90-quota.conf                auth-static.conf.ext

15-lda.conf        auth-checkpassword.conf.ext  auth-system.conf.ext

15-mailboxes.conf  auth-deny.conf.ext           auth-vpopmail.conf.ext

[root@mail-westos conf.d]# vim 10-auth.conf  

15 # There are a few special variables you can use, eg.:

 16 #

 17 #   %u - username

 18 #   %n - user part in user@domain, same as %u if there's no domain

 19 #   %d - domain part in user@domain, empty if there's no domain

 20 #   %h - home directory

 

30 mail_location = maildir:/home/vmail/%d/%n   ##邮件目录

[root@mail-westos conf.d]# vim auth-sql.conf.ext

passdb {

  6   driver = sql

  7

  8   # Path for SQL configuration file, see example-config/dovecot-sql.conf.ext

  9   args = /etc/dovecot/dovecot-sql.conf.ext

 10 }

 

[root@mail-westos conf.d]# cp /usr/share/doc/dovecot-2.2.10/example-config/dovecot-sql.conf.ext /etc/dovecot/dovecot-sql.conf.ext

[root@mail-westos conf.d]# vim /etc/dovecot/dovecot-sql.conf.ext

 32 driver = mysql  ##使用mysql数据库

 71 connect = host=localhost dbname=email user=postfix password=postfix ##连接本地email数据库  用户名密码为postfix

 78 default_pass_scheme = PLAIN ##明文认证

 107 password_query = \ ##如果密码正确

108   SELECT username, domain, password \

109   FROM emailuser WHERE username = '%u' AND domain = '%d' ##从emailuser表中,指定的用户名和域,选择。。。

125    user_query = SELECT maildir, 666 AS uid, 888 AS gid FROM emailuser WHERE     username = '%u' ##进入maildir目录

 

 

[root@mail-westos conf.d]# vim 10-mail.conf

[root@mail-westos conf.d]# > /var/log/maillog

[root@mail-westos conf.d]# systemctl restart dovecot.service

[root@mail-westos conf.d]# cat /var/log/maillog

Apr 23 21:51:40 mail-westos dovecot: master: Warning: Killed with signal 15 (by pid=1 uid=0 code=kill)

Apr 23 21:51:40 mail-westos dovecot: anvil: Warning: Killed with signal 15 (by pid=1 uid=0 code=kill)

Apr 23 21:51:40 mail-westos dovecot: log: Warning: Killed with signal 15 (by pid=1 uid=0 code=kill)

Apr 23 21:51:40 mail-westos dovecot: master: Dovecot v2.2.10 starting up for imap, pop3, lmtp (core dumps disabled)

Apr 23 21:52:24 mail-westos dovecot: auth: Fatal: Unknown database driver 'mysql'

Apr 23 21:52:24 mail-westos dovecot: master: Error: service(auth): command startup failed, throttling for 2 secs   ##没有识别mysql

Apr 23 21:52:24 mail-westos dovecot: pop3-login: Disconnected: Auth process broken (disconnected before auth was ready, waited 0 secs): user=<>, rip=172.25.254.41, lip=172.25.254.241, secured, session=<S4DO299N/wCsGf4p>

[root@mail-westos conf.d]# yum search dovecot ##查找和dovecot有关的软件

已加载插件:langpacks

============================= N/S matched: dovecot =============================

dovecot-mysql.x86_64 : MySQL back end for dovecot

dovecot-pgsql.x86_64 : Postgres SQL back end for dovecot

dovecot-pigeonhole.x86_64 : Sieve and managesieve plug-in for dovecot

dovecot.i686 : Secure imap and pop3 server

dovecot.x86_64 : Secure imap and pop3 server

 

  名称和简介匹配 only,使用“search all”试试。

[root@mail-westos conf.d]# yum install dovecot-mysql.x86_64 -y

 

[root@mail-westos conf.d]# systemctl restart dovecot.service

[root@mail-westos conf.d]# id vmail

uid=666(vmail) gid=888(vmail) 组=888(vmail)

[root@mail-westos conf.d]# vim /etc/dovecot/dovecot-sql.conf.ext

 

[1]+  已停止               vim /etc/dovecot/dovecot-sql.conf.ext

[root@mail-westos conf.d]# fg

vim /etc/dovecot/dovecot-sql.conf.ext

[root@mail-westos conf.d]# systemctl restart dovecot.service

 

 

[kiosk@foundation41 Desktop]$ telnet 172.25.254.241 110  ##没有识别数据库

Trying 172.25.254.241...

Connected to 172.25.254.241.

Escape character is '^]'.

-ERR Disconnected: Auth process broken

Connection closed by foreign host.

[kiosk@foundation41 Desktop]$ telnet 172.25.254.241 110  ##连接服务器的110端口

Trying 172.25.254.241...

Connected to 172.25.254.241.

Escape character is '^]'.

+OK [XCLIENT] Dovecot ready.

user lee@redhat.com

+OK

pass lee

+OK Logged in.

list

+OK 0 messages:

.

quit

+OK Logging out.

Connection closed by foreign host.

[kiosk@foundation41 Desktop]$ thunderbird

 

pop3 110 smtp 25

邮件搭建

[root@mail-westos vmail]# postfix+mysql+dovecot

[root@mail-westos vmail]# extmail  ##下载

 

Red Hat System Administration III

###################################单元 十一Bash Scripts

 

##########################Bash脚本基础

 

BASH = GNU Bourne-Again Shell,BASH 是 GNU 组织开发和推广的一个项目。

 

Bash脚本类似批处理,简单来讲就是把许多的指令集合在一起,并提供循环、条件、判断等重要功能,语法简单实用,用以编写程序,大大简化管理员的操作,并可以完成图形工具所无法实现的功能。

 

如何创建新shell脚本?

1. 创建包含bash命令的文本文件。文件的第一行应为:

#!/bin/bash

2. 使文件可执行(使用chmod +x scripts)

3. 将文件放置在用户的$PATH的目录中

~/bin – 用于用户的私有程序

/usr/local/bin – 本地开发、系统上的其他人使用的脚本

/usr/local/sbin - 本地开发、由root使用的脚本

直接运行脚本和使用source命令运行脚本是不同的!

 

查看shell

[root@server database]# cat /etc/shells

 

脚本调试模式:

#!/bin/bash -x

# bash -x scripts

eg:

[root@server mnt]# vim test.sh

#!/bin/bash

echo hello world!

[root@server mnt]# bash -x test.sh

+ echo hello 'world!'

hello world

 

引用和转义

引用和转义在shell解析字符串时用于去除字符串中特殊字符或保留词语的特殊含义。这会导致按字面处理字符串,而不是展开变量或将其部分内容视作具有特殊含义。

引用有三种类型:

弱引用:

将字符串放置在双引号中,保留字符串中所有字符的文字值,$、` 、\和!字符除外。换言之,变量扩展和命令扩展在双引号内仍起作用。

echo “can I have a $FRUIT”

echo “The current time is $(date +%r).”

 

强引用:

将字符串放置在单引号中,保留字符串中所有字符的文字值,同时禁用所有扩展:

echo “Make $$$ Fast”

rm 'untitled folder'

 

转义:

非引用的\是转义字符。它保留了下一个字符的文字值。(例如,\$PATH是确切的字符串$PATH,而不是PATH变量的内容。)

echo Make \$\$\$ Fast\!

ls untitled\ folder

 

[root@server0 ~]# echo # not a comment #

 

[root@server0 ~]# echo \# not a comment #

# not a comment

[root@server0 ~]# echo \# not a comment \#

# not a comment #

[root@server0 ~]# echo '# not a comment #'

# not a comment #

[root@server0 ~]# echo '$HOME'

$HOME

[root@server0 ~]# echo '`pwd`'

`pwd`

[root@server0 ~]# echo '"Hello,world"'

"Hello,world"

 

[root@server0 ~]# echo "$HOME"

/root

[root@server0 ~]# echo "`pwd`"

/root

[root@server0 ~]# echo ""Hello, world""

Hello, world

[root@server0 ~]# echo "\$HOME"

$HOME

[root@server0 ~]# echo "\`pwd\`"

`pwd`

[root@server0 ~]# echo "\"Hello, world\""

"Hello, world"

 

Shell变量

shell变量用于为稍后在脚本中使用的名称指定值,并且仅限于shell命令行或从中声明变量的脚本。

若要定义或指定值:

FRUIT=apple

若要参考或使用变量:

$FRUIT

${FRUIT}

[root@server0 ~]# FIRST=John

[root@server0 ~]# LAST=Doe

[root@server0 ~]# echo $FIRST $LAST

John Doe

[root@server0 ~]# echo $FIRST_$LAST

Doe

[root@server0 ~]# echo ${FIRST}_$LAST

John_Doe

 

命令替换

命令替换在子shell中执行指定命令并用命令输出替换脚本中的命令替换。

 

语法:

$(shell command)

示例:

touch datafile.$(id -un)

TODAY=$(date +%Y-%m-%d)

 

[root@server0 ~]# TAROUTPUT=$(tar cvf /tmp/backup.tar $(find /etc -type f -mtime 1))

tar: Removing leading `/' from member names

[root@server0 ~]# echo $TAROUTPUT

/etc/hosts.allow /etc/hosts.deny /etc/sysconfig/iptables /etc/xinetd.d/tftp /etc/rht

/etc/firewalld/zones/public.xml.old /etc/firewalld/firewalld.conf.old /etc/xinetd.conf

 

算术运算符

算术运算符指的是可以在程序中实现加、减、乘、除等数学运算的运算符。

operator   meaning

<VARIABLE>++ 增量后

<VARIABLE>-- 减量后

- 减法

+ 加法

** 幂运算

* 乘法

/ 除法

% 余数

+= 加等

-= 减等

 

Shell计算命令:

$[]表示数学运算。

# echo $[1+2]

# a=1; echo $[$[$a+1]*2]

[root@server mnt]# [ "1" -lt "2" ] && echo yes|| echo no

yes

[root@server mnt]# [ "1" -gt "2" ] && echo yes|| echo no

no

[root@server mnt]# [ "1" -le "2" ] && echo yes|| echo no

yes

[root@server mnt]# [ "1" -ge "2" ] && echo yes|| echo no

no

[root@server mnt]# [ "1" -ne "2" ] && echo yes|| echo no

yes

[root@server mnt]# [ "1" -eq "2" ] && echo yes|| echo no

no

[root@server mnt]# a=5

[root@server mnt]# [ "$a" -gt "0" -a  "$a" -lt "10" ] && echo yes|| echo no

yes

 

expr表示数学运算。

# echo `expr 1 + 2`

let指示数学运算。

# let A=1+2

# echo $A

(())表示数学运算。bash内建功能,效率高。

#!/bin/bash

for ((i=1;i<10;i++))

do

((j+=i))

done

echo $j

 

循环

for循环用于值列表中的相同命令的重复。

[root@server0 ~]# for HOST in host{1..3};do echo $HOST;done

host1

host2

host3

[root@server0 ~]# for NUM in $(seq 2 2 8);do echo $NUM;done

2

4

6

8

 

循环与计算结合:

#!/bin/bash

for ((i=1;i<=100;i++))

do

((j+=i))

#j=`expr $j + $i`

#let j+=i

#j=$[j+=i]

done

echo $j

 

也可以写成一行:

# for((i=0; i<=100; i++));do j=`expr $j + $i` ;done;echo $j

 

数据库备份示例:

#!/bin/bash

for DB in $(mysql -e "show databases;" -E -N | grep -v '^*' | grep -v 'schema$')

do

echo "Backing up $DB"

mysqldump $DB > /dbbackup/$DB.dump

done

echo ""

for DBDUMP in /dbbackup/*

do

SIZE=$(stat --printf "%s\n" $DBDUMP)

echo "$DBDUMP

$SIZE"

done

 

eg:输出ip脚本

[root@mail-westos mnt]# vim show_ip.sh

1 #!/bin/bash

  2 ip=$(ifconfig eth0 |grep inet |grep inet6 -v |awk -F " " '{print $2}')

  3 hostname=`hostname`

  4 echo "${hostname}'s ipaddress is : $ip"

[root@mail-westos mnt]# sh show_ip.sh ##执行脚本方式2

eg:

[root@mail-qq mnt]# vim host_check.sh

1 #!/bin/bash

  2 for NA in {1..40}

  3 do

  4 ping -c1 -w1 172.25.254.$NA &> /dev/null && echo 172.25.254.$NA is up ||echo 172.25.254.$NA is down

[root@mail-qq mnt]# chmod +x host_check.sh  ##执行脚本方式1

[root@mail-qq mnt]# ./host_check.sh

 

eg:自动创建用户

[root@mail-qq mnt]# vim userfile

 1 user1

 2 user2

 3 user3

[root@mail-qq mnt]# vim passfile

1 123

2 234

3 345123

[root@mail-qq mnt]# vim create_user.sh

 1 #!/bin/bash

 2 MAX=`wc -l $1|cut -d " " -f 1`

 3 for NUM in $(seq 1 $MAX)

 4 do

 5         NAME=`sed -n ${NUM}p $1`

 6         PASSWORD=`sed -n ${NUM}p $2`

 7         useradd $NAME

 8         echo $PASSWORD |passwd --stdin $NAME

 9 done

[root@mail-qq mnt]# sh create_user.sh userfile passfile

 

eg:输出10以内偶数

[root@server mnt]#  vim test.sh

#!/bin/bash

for NUM in `seq 2 2 10`

do

echo $NUM

done

[root@server mnt]# sh test.sh

2

4

6

8

10

eg:10s倒计时 ,每次不换行刷新      \r     carriage return ##man echo

#!/bin/bash

for ((i=10;i>0;i--))

do

echo -n "After ${i}s is end "

echo -ne "\r    \r"

sleep 1

done

 

eg:1分10秒倒计时 #######################

#!/bin/bash

MIN=1

for ((i=3;i>0;i--))

do

        while

        [ "$i" -eq "0" -a "$MIN" -gt "0" ]

        do

        echo -n "After ${MIN}:${i} is end"

        echo -ne "\r    \r"

        i=59

        ((MIN--))

        done

echo -n "After ${MIN}:${i} is end"

echo -ne "\r    \r"

sleep 1

done

 

eg:

[root@server mnt]#  vim test.sh

#!/bin/bash

for NAME in /etc/hello /etc/passwd /etc/group

do

ls -l $NAME &> /dev/null && echo $NAME is exist || echo $NAME is not exist

done

[root@server mnt]# sh test.sh

/etc/hello is not exist

/etc/passwd is exist

/etc/group is exist

 

posted @ 2017-04-25 21:04  yangying  阅读(195)  评论(0编辑  收藏  举报