Quantcast
Channel: Bashタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 2722

bashでパスワード自動生成

$
0
0

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

サンプル

数字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

Viewing all articles
Browse latest Browse all 2722

Trending Articles