#!/usr/bin/python
# coding=utf8
import os
import sys
import shutil
import ConfigParser
from ConfigParser import NoSectionError
from ConfigParser import NoOptionError
path_smb_root = '/opt'
filename_global_config = '/etc/samba/smb.conf'
def getConf(str_option, str_section = "global"):
if str_section == None :
str_section = 'global'
if str_section.strip() == '' :
str_section = 'global'
cf = ConfigParser.ConfigParser()
cf.read(filename_global_config)
try:
if str_section.strip() == "" :
for i in cf.sections():
for j in cf.options(i):
if j == str_option.strip() :
return cf.get(i, j)
else:
return cf.get(str_section.strip(), str_option.strip())
except NoSectionError,err:
# print err
pass
except NoOptionError,err:
# print err
pass
return None
def updateConf(str_option_name, str_option_value, str_sectionname = "global"):
if str_sectionname == None :
str_sectionname = 'global'
if str_sectionname.strip() == '' :
str_sectionname = 'global'
cf = ConfigParser.ConfigParser()
cf.read(filename_global_config)
if not cf.has_section(str_sectionname.strip()):
cf.add_section(str_sectionname.strip())
cf.set(str_sectionname, str_option_name, str_option_value)
cf.write(open(filename_global_config, "w"))
return str_option_value
def deleteConf(str_section_name, str_option_name = ''):
cf = ConfigParser.ConfigParser()
cf.read(filename_global_config)
if str_option_name.strip() == '':
if cf.has_section(str_section_name.strip()):
cf.remove_section(str_section_name)
else:
cf.remove_option(str_section_name, str_option_name)
cf.write(open(filename_global_config, "w"))
def write_smb_conf(share_name, path, username, comment = 'default'):
share_name = share_name.strip()
if comment.strip() == 'default':
updateConf('comment', 'share folder for %s' % username, share_name)
else:
updateConf('comment', comment, share_name)
updateConf('path', path, share_name)
updateConf('write list', username, share_name)
def create_smb_user(share_name, username, password):
#检查共享目录和用户是否存在
if getConf('path', share_name) != None:
print 'the share name exist.'
sys.exit()
cmd = '/usr/bin/pdbedit -L'
a = os.popen(cmd).readlines()
for i in a :
if i.split(':')[0] == username.strip():
print 'the username exist.'
sys.exit()
if os.path.exists(os.path.join(path_smb_root, share_name)):
print 'the path %s exist.' % os.path.join(path_smb_root, share_name)
sys.exit()
#创建smb帐户
cmd = '/usr/sbin/useradd -M -s /sbin/nologin %s' % username
os.system(cmd)
#设置密码
cmd = 'echo %s > /tmp/dynamic_smb_password && echo %s >> /tmp/dynamic_smb_password && cat /tmp/dynamic_smb_password | /usr/bin/smbpasswd -a -s %s && rm /tmp/dynamic_smb_password'
os.system(cmd % (password, password, username))
#创建文件夹
os.makedirs(os.path.join(path_smb_root, share_name))
cmd = 'chmod -R 777 %s' % os.path.join(path_smb_root, share_name)
os.system(cmd)
#修改smb配置
write_smb_conf(share_name, os.path.join(path_smb_root, share_name), username)
#重启smb服务
cmd = '/etc/init.d/smb reload'
os.system(cmd)
def del_smb_share(share_name):
#删除配置
deleteConf(share_name)
#重启smb服务
cmd = '/etc/init.d/smb reload'
os.system(cmd)
#删除文件夹
if os.path.exists(os.path.join(path_smb_root, share_name)):
shutil.rmtree(os.path.join(path_smb_root, share_name))
def help(help_sort):
if help_sort == 1:
print '[cmd] add|del share_name [username] [password]'
if help_sort == 2:
print '[cmd] add share_name username password'
if help_sort == 3:
print '[cmd] del share_name'
if __name__ == '__main__':
if len(sys.argv) >= 2:
if sys.argv[1] == 'add':
if len(sys.argv) == 5:
create_smb_user(sys.argv[2], sys.argv[3], sys.argv[4])
else:
help(2)
if sys.argv[1] == 'del':
if len(sys.argv) == 3:
del_smb_share(sys.argv[2])
else:
help(3)
else:
help(1)
#------------------------------------------------------------------------------------------------------------------------------
python 程序结束
保存成auto_config_samba.py
配置相关:
/etc/samba/smb.conf 文件只保留以下内容
[global]
passdb backend = tdbsam
server string = Samba Server Version %v
运行命令:
需要root权限
python auto_config_samba.py add|del share_name [username] [password]
参数说明:
add 添加共享
del 删除共享
share_name 共享名
username 用户名
password 用户密码
说明:
共享文件夹将在 /opt 下创建
添加共享时会自动检查 共享名 用户名 /opt/share_name 时候存在,若存在则退出
删除共享时只删除 /etc/samba/smb.conf 中相应的设置和 /opt/share_name 文件夹,不删除用户名