LoginSignup
0
0

More than 5 years have passed since last update.

useraddでアカウント一括登録するPerlスクリプト

Posted at

概要

centOSでアカウントを一度に登録したいときが必ず一度はありますよね。
useraddを使おうと思いますが、パスワードの指定は-pオプションでできるのですが、暗号化された文字列出ないと登録できないようです。
そこで、今回perlのcrypt関数で登録できるスクリプトを作成しました。

実行方法

perl uadd.pl

ファイル構成

account.txt
アカウント設定ファイル。アカウントと平文のパスワードをタブ区切りで記述します。
uadd.pl
プログラム本体
account.txt(sample)
taro    pa123Sadu
Jackson o4anDex
uadd.pl
#! /usr/bin/perl

# 下記形式のアカウント設定ファイル
# アカウント<TAB>パスワード
$PASSFILE = "account.txt";

# useraddコマンド
$USERADD_CMD = "useradd";

# ユーザーシェル
$USER_SHELL = '/sbin/nologin';

# salt
$SALT = 'a5';

open(DATAFILE, "< $PASSFILE") or die("error :$!");

while (my $line = <DATAFILE>) {
    chomp($line);
    @data = split(/\t/, $line);

    # 暗号化されたパスワード生成
    $pass = crypt(@data[1], $SALT);

    # 実行するコマンド
    $cmd = sprintf("sudo %s -s %s -p %s %s %s", $USERADD_CMD, $USER_SHELL, $pass, @data[0]);

    print "$cmd\n";

    # 実行
    system($cmd);
}
0
0
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
0
0