smbuseradmin用于管理smb用户, smbdiradmin用于管理共享目录, smbpass.ex是一个expect脚本,用于设置smb用户密码时调用。 脚本大量运用sed, awk, eval。
smbuseradmin
#!/bin/bash # file name: smbuseradmin # Author: Joshua.Guo # Date: 2010/07/09 # A tool used to manage smb user info, including add user, del user, query user and set user password check_sys_user () { cat /etc/passwd | gawk -F: '{print $1}' | grep "$1" | wc -l } check_smb_user () { pdbedit -L | gawk -F: '{print $1}' | grep "$1" | wc -l } add_smb_user() { echo "add smb user" local result; result=`check_sys_user $1` echo "result = $result" if (( result == 0 )) then #add needed system user groupadd -f smbgroup useradd $1 -g smbgroup -s /sbin/nologin echo "The smb user $1 is added to group $smbgroup" echo $2 | passwd $1 --stdin fi #add smb user /sbin/smbpass.ex $1 $2 } del_smb_user() { echo "delete smb user" local usergroupid local smbgroupid #delete smb user smbpasswd -x $1 #delete related system user usergroupid=`eval "cat /etc/passwd | sed -n '/^$1/p'" | gawk -F: '{print $4}'` echo "user group id: $usergroupid" smbgroupid=`cat /etc/group | grep smbgroup | gawk -F: '{print $3}'` echo "smb group id: $smbgroupid" if (( usergroupid == smbgroupid )) then userdel -fr $1 fi #remove from /etc/samba/smb.conf execstr="sed '/valid users/s//<$1/>[ /t]*//g' /etc/samba/smb.conf > /etc/samba/smb.conf.tmp" eval $execstr if [[ -f /etc/samba/smb.conf.tmp ]] then #rm -rf /etc/samba/smb.conf /bin/mv /etc/samba/smb.conf.tmp /etc/samba/smb.conf fi } set_smb_user_passwd() { if (( $# != 2)) then echo "set_smb_user_passwd() expect parameter user, passwd" fi echo "set smb user password" /sbin/smbpass.ex $1 $2 } query_smb_user_list() { pdbedit -L | gawk -F: '{print $1}' | sort } smb_user_admin_usage () { echo -e "" echo -e "Usage smbuseradmin [-adq] [-p passwd] [-u user] [-h]" echo -e "" echo -e "For example:" echo -e "/t1. Add a smb user USER_A with password 123456" echo -e "/t/tsmbuseradmin -a -u USER_A -p 123456" echo -e "" echo -e "/t2. Set smb user USER_A's password to 123456" echo -e "/t/tsmbuseradmin -u USER_A -p 123456" echo -e "" echo -e "/t3. Delete a smb user USER_A" echo -e "/t/tsmbuseradmin -d -u USER_A" echo -e "" echo -e "/t4. Query all smb user" echo -e "/t/tsmbuseradmin -q" echo -e "" exit 0 } while getopts adqu:p:h options do case $options in a) adduserflag=1 ;; d) deluserflag=1 ;; q) queryuserflag=1 ;; u) smbusername=$OPTARG ;; p) smbuserpasswd=$OPTARG ;; h) smb_user_admin_usage ;; /?) echo "Usage smbuseradmin [-adq] [-p passwd] [-u user] [-h]" exit 1 ;; esac done #add smb user if (( adduserflag == 1 )) then if [ NULL = ${smbusername:-NULL} ] then echo "Add smb user missing user name" exit 1 fi if [ NULL = ${smbuserpasswd:-NULL} ] then echo "Add smb user missing password" exit 2 fi add_smb_user ${smbusername} ${smbuserpasswd} exit 0 fi #delete smb user if (( deluserflag == 1 )) then if [ NULL = ${smbusername:-NULL} ] then echo "Delete smb user missing user name" exit 1 fi del_smb_user ${smbusername} exit 0 fi #query smb user if (( queryuserflag == 1 )) then query_smb_user_list exit 0 fi if (( $# == 0 )) then echo "Usage smbuseradmin [-adq] [-p passwd] [-u user] [-h]" exit 1 fi #change smb user passwd if [ NULL = ${smbusername:-NULL} ] then echo "set smb user password missing user name" exit 1 fi if [ NULL = ${smbuserpasswd:-NULL} ] then echo "set smb user password missing password" exit 2 fi set_smb_user_passwd ${smbusername} ${smbuserpasswd}
smbdiradmin
#!/bin/bash # file name: smbdiradmin # Author: Joshua.Guo # Date: 2010/07/12 # A tool used to manage smb share directory, including add a share directory, delete a share directory, disable share a directory, add a smb user to a share directory, delete a smb user from a share directory # Note: this script may need to restart smb service check_share_dir () { eval "cat /etc/samba/smb.conf | sed -n '//[$1/]/p' | wc -l" } check_smb_user () { eval "pdbedit -L | sed -n '//<$1/>/p' | wc -l" } check_share_dir_user_list () { eval "sed -n '//[$1/]/,//[/p' /etc/samba/smb.conf" | grep "valid users" | gawk -F= '{print $2}' | eval "sed -n '//<$2/>/p'" | wc -l } add_share_dir () { mkdir -p /adm/smb/$1 chmod a+w /adm/smb/$1 local smb_shared_flag smb_shared_flag=`check_share_dir $1` if (( smb_shared_flag == 0 )) then echo -e "[${dir_name}]" >> /etc/samba/smb.conf echo -e "/tpath = /adm/smb/${dir_name}" >> /etc/samba/smb.conf echo -e "/tvalid users =" >> /etc/samba/smb.conf echo -e "/tbrowseable = no" >> /etc/samba/smb.conf echo -e "/twritable = yes" >> /etc/samba/smb.conf fi } add_user_to_share_dir () { local result result=`check_share_dir $1` if (( result == 0 )) then echo "Cann't find share directory $1" exit 2 fi result=`check_smb_user $2` if (( result == 0 )) then echo "smb user $2 inexistent" exit 2 fi result=`check_share_dir_user_list $1 $2` if (( result == 1 )) then echo "$2 can access $1 already" else eval "sed '//[$1/]/,//[/s//(valid users =/)//1 $2/g' /etc/samba/smb.conf" > /etc/samba/smb.conf.tmp if [[ -f /etc/samba/smb.conf.tmp ]] then #rm -rf /etc/samba/smb.conf /bin/mv /etc/samba/smb.conf.tmp /etc/samba/smb.conf else echo "Failed adding $2 to access $1" exit 2 fi fi } disable_share_dir () { gawk 'BEGIN{RS=ORS="["}!/'"$1"']/' /etc/samba/smb.conf | sed '/^/[$/d' > /etc/samba/smb.conf.tmp if [[ -f /etc/samba/smb.conf.tmp ]] then #rm -rf /etc/samba/smb.conf /bin/mv /etc/samba/smb.conf.tmp /etc/samba/smb.conf else echo "Failed disable share directory $1" exit 2 fi } disable_user_from_share_dir () { eval "sed '//[$1/]/,//[/s/$2//g' /etc/samba/smb.conf" > /etc/samba/smb.conf.tmp if [[ -f /etc/samba/smb.conf.tmp ]] then #rm -rf /etc/samba/smb.conf /bin/mv /etc/samba/smb.conf.tmp /etc/samba/smb.conf else echo "Failed disable user $2 from accessing share directory $1" exit 2 fi } smb_dir_admin_usage () { echo -e "" echo -e "Usage smbdiradmin [-adr] [-n dir] [-u user] [-h]" echo -e "" echo -e "For example:" echo -e "/t1. Add directory DIR_A to samba service" echo -e "/t/tsmbdiradmin -a -n DIR_A" echo -e "" echo -e "/t2. Remove directory DIR_A from samba service" echo -e "/t/tsmbdiradmin -r -n DIR_A" echo -e "" echo -e "/t3. Remove directory DIR_A from samba service and delete directory DIR_A" echo -e "/t/tsmbdiradmin -d -n DIR_A" echo -e "" echo -e "/t4. Allow smb user USER_A to access DIR_A" echo -e "/t/tsmbdiradmin -a -n DIR_A -u USER_A" echo -e "" echo -e "/t5. Prohibit smb user USER_A to access DIR_A" echo -e "/t/tsmbdiradmin -r -n DIR_A -u USER_A" echo -e "" exit 0 } while getopts adn:ru:h options do case $options in a) add_share_dir_flag=1 ;; d) del_share_dir_flag=1 ;; n) dir_name=$OPTARG ;; r) disable_share_flag=1 ;; u) user_name=$OPTARG ;; h) smb_dir_admin_usage ;; /?) echo "Usage smbdiradmin [-adr] [-n dir] [-u user] [-h]" exit 1 ;; esac done #add share if (( add_share_dir_flag == 1 )) then if [ NULL = ${dir_name:+NULL} ] then if [ NULL = ${user_name:-NULL} ] then #add a shared directory add_share_dir ${dir_name} else #add a uaser to a share dir's access list add_user_to_share_dir ${dir_name} ${user_name} fi exit 0 fi echo "Missing directory name" fi #disable share if (( disable_share_flag == 1 )) then if [ NULL = ${dir_name:+NULL} ] then if [ NULL = ${user_name:-NULL} ] then #add a shared directory disable_share_dir ${dir_name} else #add a uaser to a share dir's access list disable_user_from_share_dir ${dir_name} ${user_name} fi exit 0 fi echo "Missing directory name" fi #remove share directory if (( del_share_dir_flag == 1 )) then if [ NULL = ${dir_name:+NULL} ] then disable_share_dir ${dir_name} rm -rf /adm/smb/${dir_name} exit 0 fi echo "Missing directory name" fi echo "Usage smbdiradmin [-adr] [-n dir] [-u user] [-h]" exit 1
smbpass.ex
#!/usr/bin/expect set username [lindex $argv 0] set password [lindex $argv 1] spawn smbpasswd -a $username expect "*password:" send "$password/r" expect "*password:" send "$password/r" expect eof