#在slackware, red hat, suse linux上测试通过。(假设samba配置目录在/etc/samba)
#!/bin/sh
# smbuseradd.sh v0.3
# yin-jianhong@163.com
if [ $# -ne 1 ]; then
echo "Usage: `basename $0` "
exit 1
fi
if [ "`id -u`" != 0 ]; then
echo "you must run this script as root"
exit 1
fi
username=$1
# 0. check whether the samba config and passwd db file are exist
smbconf_file=`find /etc -name 'smb.conf'`
if [ "x$smbconf_file" = "x" ]; then
echo "not find smb.conf file in dir /etc"
smbconf_file=`find /etc -name 'smb.conf*'`
if [ "x$smbconf_file" = "x" ]; then
exit 1
else
echo " get a copy from $smbconf_file"
echo " cp $smbconf_file ${smbconf_file/smb.conf*/smb.conf} ..."
cp $smbconf_file ${smbconf_file/smb.conf*/smb.conf}
fi
fi
smbpasswd_file=`find /etc -name smbpasswd`
if [ "x$smbpasswd_file" = x ]; then
echo "not find the smbpasswd db file"
exit 1
fi
# 1. and a system user with same name
if ! grep -q "^$username:" /etc/passwd; then
useradd -m $username
if [ $? -ne 0 ]; then
exit 1
fi
fi
# 2. and the smb user
if ! grep -q "^$username:" $smbpasswd_file; then
# and smb user, if the user not exist
grep "^$username:" /etc/passwd |
awk 'BEGIN { FS=":"
}
{ printf( "%s:%s:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:[UD ]:LCT-00000000:%s\n", $1, $3, $5) }
' >>$smbpasswd_file
if [ $? -ne 0 ]; then
echo "and smb user faild"
exit 1
fi
echo "and smb user [$username] successfully";
exist=0
else
echo "smb user [$username] have existed";
exist=1
fi
# 3. set samba passwd for the samba user
if [ $exist = 0 ]; then
echo "please set a smbpasswd for [$username]"
setpasswd="Y"
else
echo "do you want to set(or modify) smbpasswd for [$username]"
echo -n "{[Yy]*:yes, [other char]*:no} [y/n]? "
read setpasswd
fi
if echo $setpasswd | grep -q "^[Yy]"; then
smbpasswd $username
if [ $? -ne 0 ]; then
exit 1
fi
fi
阅读(3221) | 评论(0) | 转发(0) |