5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

bashでパスワード自動生成

Last updated at Posted at 2019-11-13

パスワードを一括生成してコピペしたいことがあったのでメモ

サンプル

数字8桁で先頭が0でない文字列を10個生成

コマンド

$ cat /dev/urandom | tr -dc '0123456789' | fold -w 8 | grep -v '^0' | head -n 10

実行結果

74101604
15854326
24370649
78490829
36276057
70422352
94393920
82691490
34135561
48386263

数字/英小文字/英大文字/記号12桁で必ず記号を含む文字列を10個生成

コマンド

$ cat /dev/urandom | tr -dc '0123456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ,.+\-!' | fold -w 12 | grep -E '[,\.+\-\!]' | head -n 10

実行結果

zD,JXJdHQ,gC
15zwLb7HVh,W
3CG,64,,yQbR
XX7E.ejM9.R,
ebysijGSpfZ,
,XHtcyi4G.UN
Y38MX3c.bhnp
w4,dRUjrpby!
JJUrH3eW5H,r
.WWDp84.w5Ds

shellscript化

$ cat generate_password.sh
# !/bin/bash

# 引数チェック
if [ $# -ne 1 ]; then
  echo "Usage:sh generate_password.sh password_num"
  echo "Example:sh generate_password.sh 10"
  exit 1
fi

# 生成したい数
generate_num=$1

# 数字8桁で先頭が0でない文字列を引数の数だけ生成
cat /dev/urandom | tr -dc '0123456789' | fold -w 8 | grep -v '^0' | head -n ${generate_num}
$ sh generate_password.sh 10
74948144
55446718
50202090
54395135
40939495
78852681
71060205
39155894
19875823
24833084
5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?