Shell脚本来创建多个AWS IAM用户

时间:2019-05-02 10:26:40

标签: shell

#!/bin/bash

users="test1@aaa.com test2@aaa.com test3@aaa.com test4@aaa.com"

for user in $users
do
  aws iam create-user --user-name "$user"
  aws iam attach-user-policy --user-name="$user" --policy-arn=arn:aws:iam::435501576190:policy/S3-Access-sftp-release-binaries
  aws iam create-login-profile --user-name="$user" --password='4l)C=Tb(Tl$I' --password-reset-required
done

我想自动创建用户,并且我有一个带有用户名的文本文件。如何实现自动化?

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

#!/bin/bash

gen_pw() {
  head -c 9 /dev/urandom | uuencode -m - | head -2 | tail -1 |  tr '1IlO0' '$/%&#'
}

while read user  # I assume a users text file containing one user per line here
do
  password=$(gen_pw)
  aws iam create-user --user-name "$user"
  aws iam attach-user-policy --user-name="$user" --policy-arn=arn:aws:iam::435501576190:policy/S3-Access-sftp-release-binaries
  aws iam create-login-profile --user-name="$user" --password="$password" --password-reset-required
  echo "created $user with password $password"
done < my_users_file.txt

您需要为此安装uuencode。您也可以改用base64.../urandom | base64 - | tr ...(然后不需要headtail)。