Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1286067
  • 博文数量: 168
  • 博客积分: 3483
  • 博客等级: 中校
  • 技术积分: 1696
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-06 13:17
文章分类

全部博文(168)

文章存档

2015年(6)

2014年(9)

2013年(47)

2012年(11)

2011年(13)

2010年(18)

2009年(11)

2008年(42)

2007年(11)

分类:

2008-06-28 22:07:17

#在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) |
给主人留下些什么吧!~~