邮件帐号管理的shell script


关键词

邮件帐号管理的shell script

摘要

这个shell script是用来操作我的邮服帐号的,现在提供了查看,删除,修改,更新等基本功能。还没有最终完成,比如在部分出错处理时,采用简单忽略的处理方式,数据库的备份和恢复也没加进去呢,删除帐号时也没有提供是否要删除邮件内容的的选择,现在是不删,测试嘛,省的删了又建,麻烦,嘻嘻!有时间再来改吧!

真要酬谢我的兄弟p4s2wd啊,前些日子,他发了个在线音乐的网址给我,让我听某个电台,我的firefox无法正常显示,我生气就换成win2000了,我的shell稿子,就这么一生气忘了备份了,呵呵呵,装完系统,补丁更新完毕,重启,啊,win2000起不来了,我晕倒!算了,还是换回来吧!于是又用debian sarge做桌面!这时候才想起来,我的那个脚本没有备份!还好我运气虽然差,还没差到家,呵呵,我记得给我兄弟p4s2wd发过一次,请他斧正的,于是让他又发回给我!他没删噢!哈哈

这个shell script是用来操作我的邮服帐号的,现在提供了查看,删除,修改,更新等基本功能。还没有最终完成,比如在部分出错处理时,采用简单忽略的处理方式,数据库的备份和恢复也没加进去呢,删除帐号时也没有提供是否要删除邮件内容的的选择,现在是不删,测试嘛,省的删了又建,麻烦,嘻嘻!有时间再来改吧!

真要酬谢我的兄弟p4s2wd啊,前些日子,他发了个在线音乐的网址给我,让我听某个电台,我的firefox无法正常显示,我生气就换成win2000了,我的shell稿子,就这么一生气忘了备份了,呵呵呵,装完系统,补丁更新完毕,重启,啊,win2000起不来了,我晕倒!算了,还是换回来吧!于是又用debian sarge做桌面!这时候才想起来,我的那个脚本没有备份!还好我运气虽然差,还没差到家,呵呵,我记得给我兄弟p4s2wd发过一次,请他斧正的,于是让他又发回给我!他没删噢!哈哈

#!/bin/sh
#menu
#Main Menu Script
#set -x
#ignore CTRL-C and QUIT interrupts
trap "" 2 3 15
MYDATE=`date +%d/%m/%Y`
THIS_HOST=`hostname -s`

USER=`whoami`

#User level file
USER_LEVELS=priv.user

#hold file
HOLD1=hold1.$$

#select1_result file
SELECT1_RESULT=select1_result.$$

#seleclt_op file
SELECT1_OP=select1_op.$$
#Colour function
colour()
{
#format is background;foregroundm
case $1 in
black_green)
echo ""
;;
black_yellow)
echo ""
;;
black_white)
echo ""
;;
black_cyan)
echo ""
;;
red_yellow)
echo ""
;;
esac
}

#Just read a single key please
#get_char()
#{
#get_char
#Save current stty settings
#SAVEDSTTY=`stty -g`
#stty cbreak
#dd if=/dev/tty bs=1 count=1 2> /dev/null
#stty -cbreak
#stty $SAVEDSTTY
#}

#Turn the cursor on or off
cursor()
{
#cursor
#turn cursor on/off

_OPT=$1
case $_OPT in
on) echo "[?25h"
;;
off) echo "[?25l"
;;
*) return 1
;;
esac
}

#check what privilege level the user has
restrict()
{
colour red_yellow
echo -e -n " 07Sorry you are not authorised to use this function"
colour black_green
}

user_level()
{
#user_level
#read in the priv.user file
while read LINE
do
case $LINE in
#ignore comments
#*)
;;
*)
echo $LINE >> $HOLD1
;;
esac
done < $USER_LEVELS

FOUND=false
while read MENU_USER PRIV
do
if [ "$MENU_USER" = "$USER" ]
then
FOUND=true
case $PRIV in
yes|YES)
return 0
;;
no|NO)
return 1
;;
esac
else
#no match found read next record
continue
fi
done <$HOLD1
if [ "$FOUND" = "false" ]
then
echo "Soryy $User you have not been authorised to use this menu"
exit 1
fi
}

#called when user selects quit
my_exit()
{
#my_exit
#called when user selects quit!
colour black_white
cursor on
rm *.$$
exit 0
}

tput init
#display their user levels on the screen
if user_level; then
ACCESS="Access Mode is High"
else
ACCESS="Access Mode is Normal"
fi


my_select1()
{
clear
colour black_green
cat <
----------------------------------------------------------------------------
EOF
colour black_cyan
echo "use postfix;select username,name from mailbox;">$SELECT1_OP
$MYSQL -uroot<$SELECT1_OP >>$SELECT1_RESULT
rm $SELECT1_OP
#SERIAL=0
#MYPAUSE=0
#while read MAILNAME NAME
#do
# SERIAL=`expr $SERIAL + 1`
# MYPAUSE=`expr $MYPAUSE + 1`
# if [ $MYPAUSE -eq 5 ];then
# echo "Press any key continue..."
# MYPAUSE=0
# read DUMMY
# fi
# echo -e -n "$MAILNAME $NAME "
#done<$SELECT1_RESULT
more -5 -d $SELECT1_RESULT
colour black_cyan
echo -e -n " total:$SERIAL"
colour black_green
cat <
-----------------------------------------------------------------------------
EOF
colour black_cyan
rm $SELECT1_RESULT
}

my_select1_result()
{
while :
do
colour black_green
cat <
$ACCESS
-----------------------------------------------------------------------------
User:$USER Host:$THIS_HOST Date:$MYDATE
-----------------------------------------------------------------------------
1.Detail mailbox
2.Display mailbox again
3.Return
H.Help
Q.Exit
-----------------------------------------------------------------------------
EOF
colour black_cyan
echo -e -n " Your Choice [1,2,3,H,Q >"
read MY_SELECT
case $MY_SELECT in
1)
echo -e -n "Please input you are going to detail mailbox:"
read MAILNAME
echo "use postfix; select username as 'mailbox',password as 'password',name as 'true name', maildir as 'maildir',quota as 'quota',domain as 'domain',created as 'created time',modified as 'modified time',active as 'active' from mailbox where username LIKE '$MAILNAME%' G;">$SELECT1_OP
clear
$MYSQL -uroot<$SELECT1_OP
rm $SELECT1_OP
;;
2)
my_select1
;;
3)
break
;;
q|Q)
my_exit
;;
*)
;;
esac
done
}


my_delete1()
{
while :
do
colour black_green
cat <
$ACCESS
-----------------------------------------------------------------------------
User:$USER Host:$THIS_HOST Date:$MYDATE
-----------------------------------------------------------------------------
1.Delete mailbox
2.Display mailbox again
3.Return
H.Help
Q.Exit
-----------------------------------------------------------------------------
EOF
colour black_cyan
echo -e -n " Your Choice [1,2,3,H,Q >"
read MY_SELECT
case $MY_SELECT in
1)
echo -e -n "Please input you are going to delete mailbox:"
read MAILNAME
echo "use postfix;select * from mailbox where username='$MAILNAME';">$SELECT1_OP
MYSELECT=`$MYSQL -uroot<$SELECT1_OP`
if [ -z $MYSELECT ]; then
echo "input failed:"
break
else
{
echo "use postfix;delete from mailbox where username='$MAILNAME';">$SELECT1_OP
echo "delete $MAILNAME[Y/N]"
read MYDELETE
if [ "$MYDELETE" == "Y" -o $MYDELETE == "y" ];then
$MYSQL -uroot<$SELECT1_OP
echo "delete successful"
break
else
echo "delete failed"
fi
}
fi
;;
2)
my_select1
;;
3)
break
;;
H|h)
echo "this is a help file"
;;
Q|q)
my_exit
;;
esac
done
}

my_update1()
{
while :
do
colour black_green
cat <
$ACCESS
------------------------------------------------------------------------------
User:$USER Host:$THIS_HOST Date:$MYDATE
------------------------------------------------------------------------------
1.Update mailbox
2.Display mailbox again
R.Return
H.Help
Q.Exit
------------------------------------------------------------------------------
EOF
colour black_cyan
echo -e -n " Your Choice[1,2,R,H,Q]:"
read MY_SELECT
case $MY_SELECT in
1)
while :
do
echo -e -n "Please input you are going to modify mailbox:"
read MAILNAME
[ -z "$MAILNAME" ] || break
done
echo "use postfix;select username from mailbox where username='$MAILNAME'">$SELECT1_OP
MYSELECT=`$MYSQL -uroot<$SELECT1_OP`
if [ -z "$MYSELECT" ];then
echo "update failed"
else
while :
do
echo -e -n "Please input password:"
read MYPASSWD
echo -e -n "Please input password again:"
read REMYPASSWD
if [ $MYPASSWD = $REMYPASSWD ];then
break
else
echo "password no match"
fi
done

echo -e -n "Please input user:"
read MYNAME
echo -e -n "active[y/n]"
read MYACTIVE
case $MYACTIVE in
Y|y)
MYACTIVE="1"
;;
N|n)
MYACTIVE="0"
;;
esac
MYMAILDIR="$MAILNAME/"
echo "modify[y/n]"
read MYSELECT
case $MYSELECT in
y|Y)
MYDATE=`date "+%Y-%m-%d %H:%M:%S"`
echo "use postfix;update mailbox set password='$MYPASSWD',name='$MYNAME',quota=0,modified='$MYDATE',active='$MYACTIVE' where username='$MAILNAME'">$SELECT1_OP
$MYSQL -uroot <$SELECT1_OP
echo "update successful"
;;

n|N)
echo "update failed"
;;
esac
fi

;;
2)
my_select1
;;
R|r)
break
;;
Q|q)
my_exit
;;
*)
;;
esac
done
}

my_new1()
{
while :
do
echo -e -n "Please input new mailbox:"
read MAILNAME
[ -z "$MAILNAME" ] || break
done
echo "use postfix;select username from mailbox where username='$MAILNAME';">$SELECT1_OP
MYSELECT=`$MYSQL -uroot<$SELECT1_OP`
if [ -n "$MYSELECT" ];then
echo "create mailbox failed"
else
while :
do
echo -e -n "Please input password:"
read MYPASSWD
echo -e -n "Please input password again:"
read REMYPASSWD
if [ $MYPASSWD = $REMYPASSWD ];then
break
else
echo "password no match"
fi
done
echo -e -n "Please input user:"
read MYNAME
echo -e -n "active[y/n]"
read MYACTIVE
case $MYACTIVE in
Y|y)
MYACTIVE="1"
;;
N|n)
MYACTIVE="0"
;;
esac
MYMAILDIR="$MAILNAME/"
echo "modify[y/n]"
read MYSELECT
case $MYSELECT in
y|Y)
MYDATE=`date "+%Y-%m-%d %H:%M:%S"`
MYDOMAIN=`echo $MAILNAME|awk -F@ '{print $2}'`
echo "use postfix;insert into mailbox (username,password,name,maildir,quota,domain,created,active) valuse('$MAILNAME','$MYPASSWD','$MYNAME','$MYMAILDIR',0,'$MYDOMAIN','$MYDATE','$MYACTIVE');">$SELECT1_OP
$MYSQL -uroot <$SELECT1_OP
echo "create successful"
;;
n|N)
echo "create failse"
;;
esac
fi
}


tput init
colour black_green
while :
do
tput clear
colour black_green
cat <
$ACCESS
-----------------------------------------------------------------------------
User:$USER Host:$THIS_HOST Date:$MYDATE
-----------------------------------------------------------------------------
1.Display mailbox
2.Delete mailbox
3.Modify mailbox
4.New mailbox
H.Help
Q.Exit
-----------------------------------------------------------------------------
EOF

colour black_cyan
echo -e -n " Your Choice [1,2,3,H,Q >"
MYSQL="/opt/mysql5/bin/mysql"
[ -x $MYSQL ] || exit 0
read CHOICE
case $CHOICE in
1)
my_select1
my_select1_result
;;
2)
my_select1
my_delete1
;;
3)
my_select1
my_update1
;;
4)
my_new1
;;
H|h)
tput clear
cat <
This is a help file
EOF
;;
Q|q)
my_exit
;;
*)
echo -e " 07unknown user response"
;;
esac
echo -e -n " 07Hit the return key to continue"
read DUMMY
done

 

要饭二维码

洪哥写文章很苦逼,如果本文对您略有帮助,可以扫描下方二维码支持洪哥!金额随意,先行谢过!大家的支持是我前进的动力!

文章的版权

本文属于“洪哥笔记”原创文章,转载请注明来源地址:邮件帐号管理的shell script:http://www.splaybow.com/post/130501240120087522.html

如果您在服务器运维、网络管理、网站或系统开发过程有需要提供收费服务,请加QQ:115085382!十年运维经验,帮您省钱、让您放心!
亲,如果有需要,先存起来,方便以后再看啊!加入收藏夹的话,按Ctrl+D

« 源代码安装postfix邮件系统 Postfix 日志脚本 »

相关文章:

腾讯企业邮箱POP,SMTP分别是什么  (2014/9/4 8:23:19)

邮件服务器架设  (2014/3/9 13:27:43)

winwebmail邮局附件大小  (2014/3/8 10:13:12)

Exchange服务器快速恢复的步骤  (2013/10/24 14:04:36)

使域名DNS解析支持DKIM  (2013/5/27 16:45:56)

邮局使用SPF  (2013/5/24 12:27:46)

用telnet来理解SMTP协议  (2013/5/24 12:23:38)

qmail+vpopmail+squirrelmail 安装经验谈  (2013/9/6 10:59:41)

LINUX中的QMail邮件安全问题  (2013/9/5 10:59:12)

用qmail/vpopmail/courier-imap/qmail-scanner/igenus打造邮件系统  (2013/8/31 10:59:13)