[英]BASH script to add username, password and multiple groups
ASSUMPTIONS:假设:
I have a file called users.txt我有一个名为users.txt的文件
username:password:groups
user1:password:group1,group2,group3
user2:password:group3
user3:password:group1,group3
user4:password:group2,group3
CODE代码
#!/bin/bash
FILENAME="users.txt"
while IFS=':' read USERNAME PASSWORD GROUPS
do
echo "USERNAME" $USERNAME "PASSWORD" $PASSWORD "GROUPS" $GROUPS
done < "$FILENAME"
I want to be able to add a bunch of users to be able to do a samba share.我希望能够添加一堆用户来进行 samba 共享。 I have a full list of users to input.
我有一个完整的用户列表要输入。 I am having difficulty mostly with the groups.
我在大多数情况下都遇到了小组问题。
Two biggest questions are, how to I get the groups as one string and how do I encrypt the password.两个最大的问题是,如何将组作为一个字符串获取以及如何加密密码。
I have been through all tutorials on this site and none of them are working.我已经阅读了本网站上的所有教程,但都没有奏效。
Simple to understand code would really help.简单易懂的代码真的很有帮助。 Thanks.
谢谢。
UPDATED FINISHED WORKING EXAMPLE更新完成的工作示例
#!/bin/bash
# set filename
FILENAME="users.txt"
# loop through file
while IFS=':' read USERNAME PASSWORD GROUPNAMES
do
# add user and assign groups
echo " "
echo "ADDING USER: " $USERNAME
useradd $USERNAME -G $GROUPNAMES
# add password
echo -e "$PASSWORD\n$PASSWORD\n" | passwd $USERNAME
# add user to samba
echo -e "$PASSWORD\n$PASSWORD\n" | smbpasswd -a $USERNAME
done < "$FILENAME"
Don't use GROUPS as a variable name : it's a read-only variable already used by the system.不要使用 GROUPS 作为变量名:它是系统已经使用的只读变量。 Just change the name of this variable.
只需更改此变量的名称即可。
I'm not sure about the password encryption part but I can give a suggestion about the groups.我不确定密码加密部分,但我可以提供有关组的建议。 Inside of your loop, you could do this:
在你的循环内,你可以这样做:
read -ra groups_array -d, <<<"$groups"
Now you have an array containing each group as an element.现在您有一个包含每个组作为元素的数组。 You can pass them as separate arguments to a command using the expansion
"${groups_array[@]}"
or access them individually using "${groups_array[0]}"
, "${groups_array[1]}"
, etc.您可以使用扩展
"${groups_array[@]}"
将它们作为单独的参数传递给命令,或者使用"${groups_array[0]}"
、 "${groups_array[1]}"
等单独访问它们。
Note that I am using lowercase variable names - this is intentional.请注意,我使用的是小写变量名 - 这是故意的。 Uppercase names should be reserved for use by the shell, as otherwise you run the risk of overwriting important ones.
大写名称应保留供 shell 使用,否则您将面临覆盖重要名称的风险。
Excellent script above: I used the code co create this working version.上面的优秀脚本:我使用代码共同创建了这个工作版本。
#!/bin/bash #!/bin/bash
function setupUser()函数 setupUser()
{ {
echo "remove user: " $1 smbpasswd -d $1 userdel $1回声“删除用户:” $1 smbpasswd -d $1 userdel $1
echo "adding user: "$1 useradd $1 -G nogroup echo -e "$2\\n$2\\n" | echo "添加用户:"$1 useradd $1 -G nogroup echo -e "$2\\n$2\\n" | passwd $1 echo -e "$2\\n$2\\n" |
passwd $1 echo -e "$2\\n$2\\n" | smbpasswd -a $1 }
smbpasswd -a $1 }
#--------------------------------------------------------------------------- #------------------------------------------------- --------------------------
setupUser tom secret setupUser dick secret setupUser harry secret setupUser tom secret setupUser dick secret setupUser harry secret
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.