批量创建100个系统用户,要求:帐号和密码(8位以上含数字和字母)均不能相同。

解答:

#!/bin/bash

for((i=1;i<101;i++))

do

printf -v num "%.4d" $i

userno=$num

##用户名username 是类似“user0001”“user0002”“user0003”这样的格式

username="user$userno"

##用户密码userpasswd 是类似“user-0001”“user-0002”“user-0003”这样的格式

userpasswd="user-$userno"

grep "^$username" /etc/passwd

##grep 查找后没有此用户 则会返回非0值

if [ $? -eq 0 ]; then

echo "$username exists!"

else

useradd -m "$tmpuser" -p "$userpasswd"

[ $? -eq 0 ] && echo "User '$username' has been added to system!" || echo "Failed to add a user!"

fi

done

Linux下批量创建用户、密码及用户主目录

#!/bin/bash

echo

num=0

username=user

tmpuser=""

userpasswd=""

useradd -m user -p "user123" -s "/bin/bash" # create user and user

group

for ((i=1; i<=25; i++)) #1 2 3...25

do

printf -v num "%.3d" $i

userno=$num

tmpuser="$username$userno"

userpasswd="$username-$userno"

useradd -m "$tmpuser" -g user -p "$userpasswd" -s "/bin/bash"

echo "********************************************"

done

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++

#if [ $bFlag -eq 1 ]

#then

# echo "successfully batch to create users!"

#else

# echo "!!!failed batch to create users!!!"

#fi

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++

Linux & aix 批量创建用户脚本

adduser.sh linux脚本

#!/bin/bash

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/ucb:/usr/local/bin

if [ $(id -u) -eq 0 ]; then

if [ -f "$1" ]; then

cat "$1" | while read username

do

grep "^$username" /etc/passwd

if [ $? -eq 0 ]; then

echo "$username exists!"

else

pass=$(perl -e 'print crypt($ARGV[0], "password")'

$username)

useradd -m -p $pass $username

[ $? -eq 0 ] && echo "User '$username' has been added

to system!" || echo "Failed to add a user!"

fi

done

else

echo "error username config file"

exit 1

fi

else

echo "Only root can add a user to the system"

exit 2

fi

userlist用户名列表文件

test1

test2

......

再调用脚本 ./adduser.sh userlist 即可批量创建用户名与密码相同的用户。

adduser.sh aix 脚本

#!/bin/ksh

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/ucb:/usr/local/bin

if [ $(id -u) -eq 0 ]; then

if [ -f "$1" ]; then

cat "$1" | while read username

do

grep "^$username" /etc/passwd

if [ $? -eq 0 ]; then

echo "$username exists!"

else

useradd -m $username;

[ $? -eq 0 ] && echo "User '$username' has been added

to system!" || echo "Failed to add a user!"

echo echo $username:$username \| chpasswd | ksh;

pwdadm -c $username;

fi

done

else

echo "error username config file"

exit 1

fi

else

echo "Only root can add a user to the system"

exit 2

fi

批量创建100个系统用户,要求:帐号和密码(8位以上含数字和字母)均不能相同。

解答:

#!/bin/bash

for((i=1;i<101;i++))

do

printf -v num "%.4d" $i

userno=$num

##用户名username 是类似“user0001”“user0002”“user0003”这样的格式

username="user$userno"

##用户密码userpasswd 是类似“user-0001”“user-0002”“user-0003”这样的格式

userpasswd="user-$userno"

grep "^$username" /etc/passwd

##grep 查找后没有此用户 则会返回非0值

if [ $? -eq 0 ]; then

echo "$username exists!"

else

useradd -m "$tmpuser" -p "$userpasswd"

[ $? -eq 0 ] && echo "User '$username' has been added to system!" || echo "Failed to add a user!"

fi

done

Linux下批量创建用户、密码及用户主目录

#!/bin/bash

echo

num=0

username=user

tmpuser=""

userpasswd=""

useradd -m user -p "user123" -s "/bin/bash" # create user and user

group

for ((i=1; i<=25; i++)) #1 2 3...25

do

printf -v num "%.3d" $i

userno=$num

tmpuser="$username$userno"

userpasswd="$username-$userno"

useradd -m "$tmpuser" -g user -p "$userpasswd" -s "/bin/bash"

echo "********************************************"

done

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++

#if [ $bFlag -eq 1 ]

#then

# echo "successfully batch to create users!"

#else

# echo "!!!failed batch to create users!!!"

#fi

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++

Linux & aix 批量创建用户脚本

adduser.sh linux脚本

#!/bin/bash

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/ucb:/usr/local/bin

if [ $(id -u) -eq 0 ]; then

if [ -f "$1" ]; then

cat "$1" | while read username

do

grep "^$username" /etc/passwd

if [ $? -eq 0 ]; then

echo "$username exists!"

else

pass=$(perl -e 'print crypt($ARGV[0], "password")'

$username)

useradd -m -p $pass $username

[ $? -eq 0 ] && echo "User '$username' has been added

to system!" || echo "Failed to add a user!"

fi

done

else

echo "error username config file"

exit 1

fi

else

echo "Only root can add a user to the system"

exit 2

fi

userlist用户名列表文件

test1

test2

......

再调用脚本 ./adduser.sh userlist 即可批量创建用户名与密码相同的用户。

adduser.sh aix 脚本

#!/bin/ksh

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/ucb:/usr/local/bin

if [ $(id -u) -eq 0 ]; then

if [ -f "$1" ]; then

cat "$1" | while read username

do

grep "^$username" /etc/passwd

if [ $? -eq 0 ]; then

echo "$username exists!"

else

useradd -m $username;

[ $? -eq 0 ] && echo "User '$username' has been added

to system!" || echo "Failed to add a user!"

echo echo $username:$username \| chpasswd | ksh;

pwdadm -c $username;

fi

done

else

echo "error username config file"

exit 1

fi

else

echo "Only root can add a user to the system"

exit 2

fi

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐