用脚本批量创建用户user_00 ... user_20, 并且给这些用户设置一个随机密码,并记录到一个日志文件中。
答案:
{{{密码回复可见}}}
答案:
{{{密码回复可见}}}
0
#!/usr/bin/env python
# coding=utf-8
import os
for i in range(0,21):
with open('/home/wangdi/ceshi/log.txt','a') as f:
if i < 10:
user_name = 'user_0'+'%d' % i
os.system('useradd %s' %user_name )
os.system('echo "%s" | passwd --stdin %s' %(user_name,user_name))
f.write('add new user %s,password %s \n' % (user_name,user_name))
else:
os.system('useradd user_%d' %i )
os.system('echo "%d" | passwd --stdin user_%d' %(i,i))
f.write('add new user user_%d,password user_%d \n' % (i,i))
# coding=utf-8
import os
for i in range(0,21):
with open('/home/wangdi/ceshi/log.txt','a') as f:
if i < 10:
user_name = 'user_0'+'%d' % i
os.system('useradd %s' %user_name )
os.system('echo "%s" | passwd --stdin %s' %(user_name,user_name))
f.write('add new user %s,password %s \n' % (user_name,user_name))
else:
os.system('useradd user_%d' %i )
os.system('echo "%d" | passwd --stdin user_%d' %(i,i))
f.write('add new user user_%d,password user_%d \n' % (i,i))
0
for i in `seq -w 00 20`
do
echo $i
useradd -M user_"$i"
bb=`mkpasswd`
echo "user_"$i":$bb" >> /scripts/shell/2.log
done
do
echo $i
useradd -M user_"$i"
bb=`mkpasswd`
echo "user_"$i":$bb" >> /scripts/shell/2.log
done
0
#!/bin/bash
>/tmp/pw.txt
for i in `seq 1 20`
do
useradd user$i
n="user$i$RANDOM"
echo $n | passwd --stdin user$i
echo "user$i passwd is $n" >> /tmp/pw.txt
done
>/tmp/pw.txt
for i in `seq 1 20`
do
useradd user$i
n="user$i$RANDOM"
echo $n | passwd --stdin user$i
echo "user$i passwd is $n" >> /tmp/pw.txt
done
0
#! /bin/bash # #批处理创建用户user_00...user_20,并给这些用户设置一个随机密码,并记录到文件中 # # for i in `seq 1 20` do useradd -M "user_$i" passwd=`mkpasswd` echo user_$i":"$passwd >> /opt/rh/2.txt done
0
本帖最后由 xueyongbo 于 2015-12-15 10:21 编辑
测试可以实现,输入用户数量,或通过录入用户名的文件,生成用户和随机密码。。
可以直接调用脚本选择创建用户的方式。或 使用 sh adduser.sh 20 ,或使用 sh adduser.sh /tmp/userlist.txt 的形式运行脚本。。
这个随机密码的函数还可以搞点别点算法。。
http://www.cnblogs.com/chengmo/archive/2010/10/23/1858879.html
- #!/bin/bash
- # By xueyongbo
- # 2015/12/9 15:40
- rand_random_passwd(){
- while :
- do
- rand=$RANDOM
- if [ ${#rand} -eq 5 ];then
- echo "$username$rand" | passwd --stdin $username
- echo -e " $username\t$username$rand" >> /tmp/user_passwd.txt
- break
- else
- continue
- fi
- done
- }
- number_of_user(){
- if [ -z $number ];then
- read -p "input the number of user :" num
- else
- num=$number
- fi
- for i in `seq -w 00 $num`
- do
- useradd -m user_$i
- username=user_$i
- rand_random_passwd
- done
- }
- from_userlist(){
- if [ -z $namefile ];then
- read -p "input the namelist file path :" name
- else
- name=$namefile
- fi
- for i in $(cat $name)
- do
- useradd -m $i
- username=$i
- rand_random_passwd
- done
- }
- if [ -z $1 ];then
- echo "select the number of menu :"
- select user_add_method in number_of_user from_userlist Help quit
- do
- case $user_add_method in
- quit)
- echo "your entered quit."
- break
- ;;
- number_of_user)
- number=$1
- number_of_user
- ;;
- from_userlist)
- namefile=$1
- from_userlist
- ;;
- Help)
- echo -e "you can direct run the script and you need select the number of menu : sh adduser.sh\nor input a user of number parameter ,like this : sh adduser.sh 20\nor from file thr username list : sh adduser.sh /tmp/userlist.txt\n"
- ;;
- *)
- echo "error!! invaild selection \"$REPLY\",you need select the number of menu,chose again or select \"help\"."
- ;;
- esac
- done
- elif [ -f $1 ];then
- namefile=$1
- from_userlist
- elif [ $(echo $1 | grep -E "[0-9]+") ];then
- number=$1
- number_of_user
- else
- echo "the invaild parameter $1 ."
- fi
可以直接调用脚本选择创建用户的方式。或 使用 sh adduser.sh 20 ,或使用 sh adduser.sh /tmp/userlist.txt 的形式运行脚本。。
这个随机密码的函数还可以搞点别点算法。。
http://www.cnblogs.com/chengmo/archive/2010/10/23/1858879.html
0
本帖最后由 考鸡蛋 于 2015-12-29 17:48 编辑
- [root@bogon ~]# less useradd.sh
- #!/bin/bash
- #批量创建用户脚本
- #2015-12-29
- #检测或安装expect
- rpm -qa | grep expect || yum install -y expect
- #生成用户名文件
- for i in `seq 01 20`
- do
- name=user_$i
- echo -e "$name \c" ' ' >> ./name.txt
- done
- #打印记录文件头
- echo 'username: password' >> ./user_passwd
- #创建用户设置密码
- for i in `cat ./name.txt`
- do
- #记录用户名
- echo -e "$i: \c" >> ./user_passwd
- #创建用户
- useradd $i
- #生成用户密码并出入记录文件
- pawd=`mkpasswd`
- echo $pawd >> ./user_passwd
- #更改用户密码
- echo $pawd | passwd --stdin $i
- done
- #删除临时文件
- rm -fr ./name.txt
- 测试:
- [root@bogon ~]#sh -x useradd.sh
- [root@bogon ~]#less user_passwd
- username: password
- user_1: 53ftlcBT[
- user_2: }nsMwnW48
- user_3: r7!dgT5qU
- user_4: 81<KquNrz
- user_5: iqgj83/EX
- user_6: 4m:UXkh5o
- user_7: qa)6lX8oI
- user_8: $9uddb3IQ
- user_9: 1TmhstR9)
- user_10: cb8XtxL6,
- user_11: /RFd4qx8y
- user_12: 6kdu+YB7l
- user_13: w5eL4zLa!
- user_14: GF28vihd!
- user_15: I96rvM[rk
- user_16: oHnk8gM1^
- user_17: Ce-Zq3jh0
- user_18: uTza%pH14
- user_19: Umoet1|S4
- user_20: vb3tyBU1@
-
- [root@bogon ~]# su user_1
- [user_1@bogon root]$ su user_2
- 密码:
- [user_2@bogon root]$ su user_3
- 密码:
- [user_3@bogon root]$ ls
- ls: 无法打开目录.: 权限不够
0
本帖最后由 balich 于 2016-1-5 15:35 编辑
- #!/bin/bash
- #
- # 2015-12-24
- . /etc/init.d/functions
- Userbox=/home/userbox.txt
- Failbox=/home/fail_userbox.txt
- [ -f "$Userbox" ] || touch $Userbox
- [ -f "$Failbox" ] || touch $Failbox
- for n in `seq -w 0 20`
- do
- password=`mkpasswd -l 8`
- useradd user_$n >& /dev/null && user_status=$?
- echo "$password" | passwd --stdin user_$n >& /dev/null && pass_status=$?
- if [ $user_status -eq 0 -a $pass_status -eq 0 ]; then
- action "adduser user_$n " /bin/true
- echo -e "user:\t user_$n password:$password" >> $Userbox
- else
- action "adduser user_$n " /bin/false
- echo -e "user:\t user_$n password:$password" >> $Failbox
- fi
- done
0
在最后一个echo行下,加个echo "$bb"|passwd --stdin user_$i
Armani 发表于 2015-8-27 11:10
for i in `seq -w 00 20`
do
echo $i
在最后一个echo行下,加个echo "$bb"|passwd --stdin user_$i
0
- for i in `seq -w 1 20`
- do
- pw=`echo $RANDOM|sha256sum|base64|head -c 12`
- useradd user_$i >/dev/null
- echo $pw |passwd --stdin user_$i > /dev/null
- echo "user_$i $pw" >> ./newuser.txt
- done
0
本帖最后由 prospect 于 2016-1-6 22:31 编辑
#!/bin/sh
#author@zhanwang.xie
#date_20160106
for i in `seq -w 1 20`
do
useradd user$i
passwd="user$RANDOM"
echo $passwd |passwd --stdin user$i
echo "user$i is OK"
sleep 1
echo "user is user$i;passwd is $passwd" >>user.txt
done
#!/bin/sh
#author@zhanwang.xie
#date_20160106
for i in `seq -w 1 20`
do
useradd user$i
passwd="user$RANDOM"
echo $passwd |passwd --stdin user$i
echo "user$i is OK"
sleep 1
echo "user is user$i;passwd is $passwd" >>user.txt
done
0
#!/bin/bash
for i in `seq -w 1 20`
do
p=`mkpasswd -l 10`
useradd user_$i
echo "$p"| passwd --stdin user_$i >> passwd.log 2>/dev/null
done
for i in `seq -w 1 20`
do
p=`mkpasswd -l 10`
useradd user_$i
echo "$p"| passwd --stdin user_$i >> passwd.log 2>/dev/null
done
0
#!/bin/bash
rpm -qa | grep expect || yum install -y expect
for i in `seq -w 00 20`
do
useradd user_$i
passwd=`mkpasswd`
echo $passwd |passwd --stdin user_$i
echo "user_$i $passwd" >> /var/user.log
done
rpm -qa | grep expect || yum install -y expect
for i in `seq -w 00 20`
do
useradd user_$i
passwd=`mkpasswd`
echo $passwd |passwd --stdin user_$i
echo "user_$i $passwd" >> /var/user.log
done
0
- #!/bin/bash
- user='user_'
- echo "" > userfile
- for u in $(seq -w 00 20)
- do
- useradd $user$u
- pw=`mkpasswd -s 0 -l 6`
- echo $user$u:$pw >> userfile
- done
- chpasswd < userfile
0
本帖最后由 rolay8 于 2016-1-18 16:37 编辑
- #!/bin/bash
- for i in $(seq -w 00 20); do
- rn=$(head -n5 /dev/urandom |sed 's/[^0-9a-Z]//g'|strings |sed ':label;N;s/\n//g'|head -n1)
- useradd user_$i >/dev/null && echo -n $rn|passwd --stdin user_$i >/dev/null
- echo "user_$i : $rn">>user.txt
- done
0
- #!/bin/bash
- for i in `seq 00 19`
- do
- username="user_"$i
- password=`mkpasswd`
- useradd $username
- echo $password|passwd --stdin $username
- echo $username" "$password >>./pwd.txt
- done
0
for i in {0..10}
do
if ! id Auser_$i &> /dev/null
then
useradd Auser_$i
passWord="$RANDOM"
# echo "$passWord"
echo "$passWord" | passwd --stdin Auser_$i &>> /var/log/create_log
else
echo "Auser_$i is cunzai"
fi
done
do
if ! id Auser_$i &> /dev/null
then
useradd Auser_$i
passWord="$RANDOM"
# echo "$passWord"
echo "$passWord" | passwd --stdin Auser_$i &>> /var/log/create_log
else
echo "Auser_$i is cunzai"
fi
done
0
#!/bin/bash #批量创建用户 for i in `cat /root/shell/user.txt` do useradd -s /bin/bash $i echo `mkpasswd -l 10 -s 0 -d 5 -C 3` |passwd --stdin $i echo $i done
0
#!/bin/bash
##
##
>/tmp/passwd_sh
for i in `seq -w 0 20`
do
useradd -M -g 503 user$i
passwd=`mkpasswd -l 7 -s 0`
echo $passwd >>/tmp/passwd_sh
echo $passwd|passwd --stdin user$i
done
##
##
>/tmp/passwd_sh
for i in `seq -w 0 20`
do
useradd -M -g 503 user$i
passwd=`mkpasswd -l 7 -s 0`
echo $passwd >>/tmp/passwd_sh
echo $passwd|passwd --stdin user$i
done
0
#!/bin/bash
>/tmp/user.log
for i in `seq 1 20`
do
useradd user$i
n="user$i$RANDOM"
echo $n | passwd --stdin user$i
echo "user$i passwd is $n" >> /tmp/user.txt
done
exit0
====删除脚本
#!/bin/bash
for i in `seq 1 20`
do
if [ -z `grep user$i /etc/passwd` ]
then
echo "Not fount user$i"
else
userdel -r user$i
echo "user$i is remove"
fi
done
exit 0
>/tmp/user.log
for i in `seq 1 20`
do
useradd user$i
n="user$i$RANDOM"
echo $n | passwd --stdin user$i
echo "user$i passwd is $n" >> /tmp/user.txt
done
exit0
====删除脚本
#!/bin/bash
for i in `seq 1 20`
do
if [ -z `grep user$i /etc/passwd` ]
then
echo "Not fount user$i"
else
userdel -r user$i
echo "user$i is remove"
fi
done
exit 0
编辑回复