关闭

创建samba共享文件夹脚本

427人阅读 评论(0) 收藏 举报

创建samba共享文件夹的脚本,也可以删除创建的samba共享文件夹。创建时输入参数为用户名和密码;删除时为用户名。


#!/bin/sh

DATA_FOLDER_PATH=/var/cache/share_folder
SMB_CONFIG=/etc/samba/smb.conf

# 检查参数个数
if [ $# -ne 2 ] && [ $# -ne 3 ] ; then
	echo "ERROR:parameters number is $# !"
	echo "usage: $0 [add/del] username password"
	exit 1
fi

add_system_user()
{
	useradd $1
}

del_system_user()
{
	userdel $1
	rm -rf /home/$1
}

add_smb_user()
{
	smbpasswd -a $1 -s <<EOF
$2
$2
EOF
}

del_smb_user()
{
	smbpasswd -x $1
}

add_smb_config()
{
	mkdir -p $2
	chown $1:$1 $2

	echo "[$1]" 								>> $SMB_CONFIG
	echo "comment = private homework"			>> $SMB_CONFIG
	echo "path = $2" 							>> $SMB_CONFIG
	echo "browseable = no"						>> $SMB_CONFIG
	echo "public = no"							>> $SMB_CONFIG
	echo "printable = no"						>> $SMB_CONFIG
	echo "writable = yes"						>> $SMB_CONFIG
	echo "write list = $1"						>> $SMB_CONFIG
}

del_smb_config()
{
	if [ -d "$DATA_FOLDER_PATH/$user_name" ];then
		rm -rf $DATA_FOLDER_PATH/$user_name
	fi

	start=`sed -n "/\[$1\]/=" $SMB_CONFIG`
	end=$(($start+7))
	if [ -z "$start" ] || [ -z "$end" ];then
		echo "Not found line."
		exit 0
	fi

	sed -i "${start},${end}d" $SMB_CONFIG
}

option=$1
user_name=$2
password=$3

add_samba_user()
{
	if [ ! -d $DATA_FOLDER_PATH ]; then
		mkdir -p $DATA_FOLDER_PATH
	fi
	
	if [ -d "$DATA_FOLDER_PATH/$user_name" ];then
		echo "Student data folder $DATA_FOLDER_PATH/$user_name exist"
		exit 0
	fi
	
	add_system_user $user_name
	add_smb_user $user_name $password
	add_smb_config	$user_name $DATA_FOLDER_PATH/$user_name
	service smb restart
}

del_samba_user()
{
	del_system_user $user_name
	del_smb_user $user_name
	del_smb_config	$user_name $DATA_FOLDER_PATH/$user_name
	service smb restart
}

case "$option" in
	"add")
	add_samba_user $1
	;;
	"del")
	del_samba_user $1
	;;
	*) echo "parameters error!"
	;;
esac

exit 0


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:168838次
    • 积分:2340
    • 等级:
    • 排名:第15938名
    • 原创:64篇
    • 转载:9篇
    • 译文:0篇
    • 评论:39条
    文章分类
    最新评论