smile的专栏

细雨鱼儿出,微风燕子斜

创建samba共享文件夹脚本

创建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


阅读更多
上一篇Linux服务器性能日志收集和分析脚本
下一篇Linux下 Apache+SVN 让用户自行修改SVN密码
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭