Shell scripts create users in batches and generate random passwords

Source: Internet
Author: User


Requirement: create 10 SYSTEM account oldboy01-oldboy10 in batches and set to generate password (different password ).


Implementation script:

#!/bin/bash#Question3for i in $(seq -w 10)do        useradd -s /bin/bash oldboy$i        echo "password$i" | md5sum | tee -a passwd.txt | passwd --stdin  oldboy$idone


Script Execution result:

[[Email protected] Q4] # sh q4.shchanging password for user oldboy01.passwd: All authentication tokens updated successfully. changing password for user oldboy02.passwd: All authentication tokens updated successfully. changing password for user oldboy03.passwd: All authentication tokens updated successfully. changing password for user oldboy04.passwd: All authentication tokens updated successfully. changing password for user oldboy05.passwd: All authentication tokens updated successfully. changing password for user oldboy06.passwd: All authentication tokens updated successfully. changing password for user oldboy07.passwd: All authentication tokens updated successfully. changing password for user oldboy08.passwd: All authentication tokens updated successfully. changing password for user oldboy09.passwd: All authentication tokens updated successfully. changing password for user oldboy10.passwd: All authentication tokens updated successfully. view the passwd file: [[email protected] Q4] # tail-N 10/etc/passwdoldboy01: X: 501: 501:/home/oldboy01:/bin/basholdboy02: X: 502: 502:/home/oldboy02:/bin/basholdboy03: X: 503:/home/oldboy03:/bin/basholdboy04: X: 503: 504 :: /home/oldboy04:/bin/basholdboy05: X: 505: 505:/home/oldboy05:/bin/basholdboy06: X: 506:/home/oldboy06: /bin/basholdboy07: X: 507: 507:/home/oldboy07:/bin/basholdboy08: X: 508: 508:/home/oldboy08:/bin/basholdboy09: x: 509: 509:/home/oldboy09:/bin/basholdboy10: X: 510: 510:/home/oldboy10:/bin/bash

The account is successfully created. Then let's take a look at the password file:

[[Email protected] Q4] # lspasswd.txt q4.sh [[email protected] Q4] # Cat passwd.txt # Secret-secret-encryption-secret- signature-encoding-f46c891cad3974fc64b7133911404c2a-ebde6449998d7c84ccb23725ecac782b-signature-encoding-


Requirement 2: create 10 SYSTEM account oldboy01-oldboy10 in batches and set the password randomly (the password is 8 characters ).


Slight modification based on the above script:

#!/bin/bash#Question4for i in $(seq -w 10)do        useradd -s /bin/bash oldboy$i        echo "password$i" | md5sum |cut -c-8 | tee -a passwd.txt | passwd --stdin  oldboy$idone


Execution result:

[[Email protected] Q4] # sh q4.shchanging password for user oldboy01.passwd: All authentication tokens updated successfully. changing password for user oldboy02.passwd: All authentication tokens updated successfully. changing password for user oldboy03.passwd: All authentication tokens updated successfully. changing password for user oldboy04.passwd: All authentication tokens updated successfully. changing password for user oldboy05.passwd: All authentication tokens updated successfully. changing password for user oldboy06.passwd: All authentication tokens updated successfully. changing password for user oldboy07.passwd: All authentication tokens updated successfully. changing password for user oldboy08.passwd: All authentication tokens updated successfully. changing password for user oldboy09.passwd: All authentication tokens updated successfully. changing password for user oldboy10.passwd: All authentication tokens updated successfully. # Check the password file #-, therefore, it is an append instead of an overwrite [[email protected] Q4] # Cat passwd.txt example-Example-ebde6449998d7c84ccb23725ecac782b-Example -2017-16a383252c3a424b3d977592ce8cf8ee-446174d0862f8f830ef4c54bdcf73d82-166335a86348834ffd4d79cd5a73867b-later


The random process is missing:

Batch delete users just created:

#!/bin/bash#del_user.shfor i in `seq -w 10`do    userdel -r oldboy$idone

Script For randomly generating passwords:

#!/bin/bash#Question4for i in $(seq -w 10)do        useradd -s /bin/bash oldboy$i        echo "$RANDOM" | md5sum |cut -c-8 | tee  passwd.txt | passwd --stdin  oldboy$idone

 


This article is from "Shi zhenning's technology blog" blog, please be sure to keep this source http://magic3.blog.51cto.com/1146917/1431581

Related Article

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.