Batch generate multiple accounts and set passwords

Source: Internet
Author: User
Tags md5 encryption


Issue 1: Batch Create 10 System account UCODE01-UCODE10 and set the password (the password cannot be the same)

#!/bin/bash
For N in $ (seq-w 10)
Do
Useradd ucode-$n
echo "$n" |passwd--stdin ucode-$n
Done

[Email protected] scripts]# sh adduser01.sh
Changing password for user ucode-01.
Passwd:all authentication tokens updated successfully.
Changing password for user ucode-02.
Passwd:all authentication tokens updated successfully.
Changing password for user ucode-03.
Passwd:all authentication tokens updated successfully.
Changing password for user ucode-04.
Passwd:all authentication tokens updated successfully.
Changing password for user ucode-05.
Passwd:all authentication tokens updated successfully.
Changing password for user ucode-06.
Passwd:all authentication tokens updated successfully.
Changing password for user ucode-07.
Passwd:all authentication tokens updated successfully.
Changing password for user ucode-08.
Passwd:all authentication tokens updated successfully.
Changing password for user ucode-09.
Passwd:all authentication tokens updated successfully.
Changing password for user ucode-10.
Passwd:all authentication tokens updated successfully.

[Email protected] scripts]# SU-UCODE01
Su:user UCODE01 does not exist
[Email protected] scripts]# su-ucode-01
[Email protected] ~]$ su-ucode-02
Password:
[Email protected] ~]$


Issue 2: Batch Create 10 System account UCODE01-UCODE10 and set password (password is random 8-bit string)

Random number variable $random and time $ (date +%t%n):

[Email protected] scripts]# echo $RANDOM
30942

[Email protected] scripts]# echo $RANDOM
21755
[[email protected] scripts]# echo $ (date +%n)
361278349
[[email protected] scripts]# echo $ (date +%t%n)
821466599
[[email protected] scripts]# echo $ (date +%t%n)

Combine two random numbers

[[email protected] scripts]# echo $ (date +%t%n) $RANDOM
73420786120340

Give the results to MD5 encryption to handle it.

[[email protected] scripts]# echo $ (date +%t%n) $RANDOM |md5sum
ECA0BA74CC88C3ACCD864250BE70AA0A-
[Email protected] scripts]#

Take a 8-bit string

[[email protected] scripts]# echo $ (date +%t%n) $RANDOM |md5sum |cut-c 2-9

9202fb1d
[[email protected] scripts]# echo $ (date +%t%n) $RANDOM |md5sum |cut-c 2-9
7e5247a9
[[email protected] scripts]# echo $ (date +%t%n) $RANDOM |md5sum |cut-c 2-9
67d9b364
[[email protected] scripts]# echo $ (date +%t%n) $RANDOM |md5sum |cut-c 2-9
808d5c06

To write a script:

Vim userdeluser02.sh
#!/bin/bash
. /etc/init.d/functions
>/mnt/user.txt
For N in $ (seq-w 10)
Do
Passwd= ' echo $ (date +%t%n) $RANDOM |md5sum |cut-c 2-9 '
Useradd ucode-$n >&/dev/null && user_status=$?
echo "$passwd" |passwd--stdin ucode-$n >&/dev/null && passwd_status=$?
If [$user _status-eq 0-a $passwd _status-eq 0];then
Action "AddUser ucode-$n"/bin/true
Echo-e "user:\tucode-$n pass:\t$passwd" >>/mnt/user.txt
Else
Action "AddUser ucode-$n"/bin/false
Echo-e "user:\tucode-$n pass:\t$passwd" >>/mnt/fail_user.txt
Fi
Done

[Email protected] scripts]# sh userdeluser02.sh
AddUser ucode-01 [OK]
AddUser ucode-02 [OK]
AddUser ucode-03 [OK]
AddUser ucode-04 [OK]
AddUser ucode-05 [OK]
AddUser ucode-06 [OK]
AddUser ucode-07 [OK]
AddUser ucode-08 [OK]
AddUser ucode-09 [OK]
AddUser ucode-10 [OK]
[Email protected] scripts]# Cat/mnt/user.txt
User:ucode-01 Pass:a3410e42
User:ucode-02 PASS:0E38B5B2
user:ucode-03 pass:9512c1a0
user:ucode-04 Pass:7fd7b5fe
User:ucode-05 pass:384f4c6c
user:ucode-06 Pass:611525b8
user:ucode-07 Pass:cfc69fdb
user:ucode-08 Pass:ddddbbe9
user:ucode-09 PASS:AEB10AC0
User:ucode-10 pass:7f64bf48
[Email protected] scripts]# su-ucode-01
[Email protected] ~]$ su-ucode-02
Password:



Expansion: 6 ways to generate random numbers in a Linux production system

Method 1: Pass the System environment variable ($RANDOM)

Example:

[Email protected] scripts]# echo $RANDOM
5016
[Email protected] scripts]#
[Email protected] scripts]# echo $RANDOM
14107
[Email protected] scripts]#


Method 2: Generate random numbers through OpenSSL

Example:

[email protected] scripts]# OpenSSL rand-base64 8
9boftwuekrm=
[email protected] scripts]# OpenSSL rand-base64 10
vyskdkydyvrsrw==
[Email protected] scripts]#

[email protected] scripts]# OpenSSL rand-base64 10|md5sum
FE581A8BD8787FA6E85A7AFAC5F1A66B-
[Email protected] scripts]#


Method 3: Get random numbers by Time (date)  

Example:

[Email protected] scripts]# date +%s%n
1437991244124034732
[Email protected] scripts]# date +%s%n
1437991246221217844
[Email protected] scripts]# date +%s%n%m
143799125181633289607
[Email protected] scripts]#


Method 4: Use the/DEV/RANDOM device to obtain

Description:/dev/random device that stores the real-time data of the current operating environment of the system. It can be seen as a unique data at some point in the system, so it can be used as a random-number meta-data. They can read the data in the form of file reading. /dev/urandom This device data is the same as in random. Just, it is a non-plug random number generator. Read operations do not cause blocking.

Example:

[Email protected] scripts]# Head/dev/urandom |cksum
3711806028 1786
[Email protected] scripts]# Head/dev/urandom |cksum
1097231128 1469
[Email protected] scripts]# Head/dev/urandom |cksum
1494175762 2776
[Email protected] scripts]# Head/dev/urandom |cksum
176794259 4099
[Email protected] scripts]#

[Email protected] scripts]# Head/dev/urandom |cksum|md5sum

885e794c0ff6b821c7a94b6f4623ac76-


Method 5: Use the UUID code to get the random number

Description: The UUID code is the Universal unique identifier (universally unique identifiter,uuid) It is a software building standard, also for the Free Software Foundation (Open Software Foundation, OSF) is part of the organization in the field of distributed computing environments (distributed Computing enveronment).

The purpose of the UUID is to allow all elements in a distributed system to have unique identification information, without the need for a central control to identify information, so that everyone can create a UUID that does not conflict with others. In such cases, there is no need to consider the name duplication problem when data is created, which will make the UUID code generated by any computer within the network to be unique across the Internet in the entire server network. Its original information will be added hardware, time, machine current operation information and so on.

The UUID format is: Contains 32 16 digits, with "-" connection number is divided into five segments, in the form of 8-4-4-4-12 32 characters. Example: 550e8400-e29b-41d4-a716-446655440000, so: the UUID of the theoretical total is 216 x 8 = 2128, approximately equal to 3.4 x 1038, that is, if 1 trillion uuid per second is generated, It will take 10 billion years to run out of all the UUID.

Example:

[Email protected] scripts]# Cat/proc/sys/kernel/random/uuid
4e5118d3-e262-48d7-9e19-148b1c7da81f
[Email protected] scripts]# Cat/proc/sys/kernel/random/uuid
C2fe4d92-55e7-45da-b4cf-c972483bed02
[Email protected] scripts]# Cat/proc/sys/kernel/random/uuid
c002946e-db84-44a9-a170-1f285b3ff117
[Email protected] scripts]#

You can also use MD5 to encrypt

[Email protected] scripts]# Cat/proc/sys/kernel/random/uuid |md5sum
4D0E0F6799AD6C15D27BD17F1483BF58-
[Email protected] scripts]#

Method 6: Get random numbers by expect

[Email protected] scripts]# yum-y install expect

Mkpasswd-l 8

[Email protected] scripts]# Mkpasswd-l 8
qgq4ee5#
[Email protected] scripts]# Mkpasswd-l 8
4lmmx+v2
[Email protected] scripts]# Mkpasswd-l 8
Sl2jj3i\
[Email protected] scripts]# Mkpasswd-l 8
HL9) o0rd
[Email protected] scripts]# Mkpasswd-l 8
[Email protected]
[Email protected] scripts]#

or add parameter-s-l, etc.

[Email protected] scripts]# Mkpasswd-s 0
Vtk9gb3em
[Email protected] scripts]# Mkpasswd-s 0
0nqvx8ivg
[Email protected] scripts]# Mkpasswd-s 0
C6kups5zf
[Email protected] scripts]# Mkpasswd-s 0
9rbbhqn9n
[Email protected] scripts]#


View mkpasswd parameter Settings Common parameter meanings of the MKPASSWD command:

usage:mkpasswd [args] [user]

Where arguments is:

-L # (length of password, default = 7)

Specifies the length of the password, which is 7 digits by default

-D # (min # of digits, default = 2)

Specifies the minimum number of digits in the password, which is 2-bit by default

-C # (min # of lowercase chars, default = 2)

Specifies the minimum number of lowercase letters in the password, the default is 2 bits

-C # (min # of uppercase chars, default = 2)

Specifies the minimum number of uppercase letters in the password, which is 2-bit by default

-S # (min # of special chars, default = 1)

Specifies the minimum number of special characters in a password, which is 1 bits by default

-V (verbose, show passwd interaction)

This parameter in the experiment when the error, specifically do not know.


Parameters:

-L # (password length definition, default is 9)

-D # (number of Bits, default is 16)

-C # (lowercase characters, default is 3)

-C # (uppercase characters, default is 2)

-S # (special character, default is 1)

-V (Verbose ... )

-P Prog (program set password, default is passwd)

code example:

#mkpasswd-l 20-d 5-c 5-c 5-s 5 root

Z}[email protected]&,c5{d3


Batch Build User scripts

#!/bin/bash

For N in $ (seq-w 3)

Do

Useradd ucode-$n

Passwd= '/usr/bin/mkpasswd-l 16-c 3-c 4-d 4-s 5 '

echo $passwd |passwd--stdin ucode-$n

echo "$PASSWD: ucode-$n" >>/opt/userlist.txt

Done



The number of random numbers above is different, how to format it uniformly? Answer: Use the md5sum command

[Email protected] scripts]# cat/proc/sys/kernel/random/uuid |md5sum |cut-c 1-9
873c50fe2
[Email protected] scripts]# head/dev/urandom |cksum |md5sum |cut-c 1-9
2a2b35b18
[[Email protected] scripts]# date +%s%n|md5sum |cut-c 1-9
6687443ee
[email protected] scripts]# OpenSSL rand-base64 10|md5sum |cut-c 1-9
6d2d2618c
[Email protected] scripts]# echo $RANDOM |md5sum |cut-c 1-9
e7109ef67
[Email protected] scripts]#

[Email protected] scripts]# mkpasswd-s 0|md5sum |cut-c 1-9
624d2bb67
[Email protected] scripts]#



This article from "Flat Light is true" blog, please be sure to keep this source http://ucode.blog.51cto.com/10837891/1760783

Batch generate multiple accounts and set passwords

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.