标签 bash 下的文章

批量添加用户 - iredmail - bash脚本

需求:自动生成用户名和密码,使用 iredmail 的脚本导入。
尝试:

#!/bin/bash
#For Loop to Read Three-expression

rm -rf /tmp/users.txt
rm -rf /tmp/users.sql

for ((i=1; i<=3; i++))
do
# echo "$i"
USER=$(strings /dev/urandom |tr -dc a-z0-9 | head -c8)
PWD=$(strings /dev/urandom |tr -dc a-z0-9 | head -c8)
DOMAIN="lan.anqun.org"

# echo $USER $PWD
# echo $USER '($PWD)'
echo $USER@$DOMAIN $PWD >> /tmp/users.txt
bash create_mail_user_SQL.sh $USER@$DOMAIN $PWD >> /tmp/users.sql
done

参考:

使用 curl 批量向 iredmail 提交新邮箱用户和密码

需求:iredmail 免费版没有 api,除了官方带的工具外,有没其它方法可以批量创建账号呢?

尝试:可以使用 curl 来提交表单,用脚本循环提交。

##
# Configurations.
##

read -p "请输入iredmain管理台的登录网址URL,如 https://mail.anqun.org/iredadmin/login: " login_url
read -p "请输入iredmail的创建用户网址URL,如 https://mail.anqun.org/iredadmin/create/user: " create_url
read -p "请输入邮局的管理员用户名,如 postmaster@lan.anqun.org: " username
read -p "请输入邮局的管理员用密码: " password

read -p "请输入要添加用户的域名,如 lan.anqun.org: " domainname
read -p "请输入要添加用户的个数,如 100: " number

rm iredmail-users.txt
rm tmpcs.txt
rm cookie

# Path to temporary file which will store your cookie data.
cookie_path=cookie

action_url="$create_url/$domainname"

##
# Logic. Most likely you shouldn't change here anything.
##

curl -i -b $cookie_path -c $cookie_path -d "username=$username&password=$password" "$login_url"

curl -i -b $cookie_path -c $cookie_path -o tmpcs.txt --request GET "$action_url"

csrf_token=$(grep -oP '(?<=<input type="hidden" name="csrf_token" value=")[^"]*' tmpcs.txt)


for ((i=1; i<=$number; i++))
    do
        mailuser=$(strings /dev/urandom |tr -dc a-z0-9 | head -c8)
        mailpasswd=$(strings /dev/urandom |tr -dc a-z0-9 | head -c8)
        echo $mailuser@$domainname  $mailpasswd >> iredmail-users.txt

        data="csrf_token=$csrf_token&domainName=$domainname&username=$mailuser&newpw=$mailpasswd&confirmpw=$mailpasswd&cn=&preferredLanguage=en_US&mailQuota=1024&submit_add_user=Add"
        curl -i --cookie cookie --data "${data}" "$action_url"
done

参考:https://www.drupal.org/node/2045751

IRedMail - 批量创建用户 - urandom

需求:随机创建100个邮箱用户和密码。

尝试:可使用 /dev/urandom 生成随机字符的用户名和密码(八位),然后通过 create_mail_user_SQL.sh 生成SQL语句,最后导入到数据库。

#!/bin/bash
#For Loop to Read Three-expression

rm -rf /tmp/iredmail-users.txt
rm -rf /tmp/iredmail-users.sql

for ((i=1; i<=100; i++))
do
USER=$(strings /dev/urandom |tr -dc a-z0-9 | head -c8)
PWD=$(strings /dev/urandom |tr -dc a-z0-9 | head -c8)
DOMAIN="lan.anqun.org"

echo $USER@$DOMAIN $PWD >> /tmp/iredmail-users.txt
bash create_mail_user_SQL.sh $USER@$DOMAIN $PWD >> /tmp/iredmail-users.sql
done

参考:
https://docs.iredmail.org/sql.create.mail.user.html
https://www.runoob.com/linux/linux-shell-process-control.html