Chinaunix首页 | 论坛 | 博客
  • 博客访问: 67636
  • 博文数量: 14
  • 博客积分: 1599
  • 博客等级: 上尉
  • 技术积分: 160
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-13 09:39
文章分类
文章存档

2011年(6)

2010年(4)

2008年(4)

分类:

2010-03-18 17:11:34

一个任务

需要将两个网页上所列出的所有用户在几个不同的Solaris机器上(INT,QA,STG,PROD)建立用户,由于有些用户在某些机器上可能已经存在账户了,因此需要将那些没有账户的用户进行建立,而已经有账户的则不建立。另外需要单独发邮件通知给每个人不同的账号。

粗看了下,估计有70多个用户,5个机器。粗算下来得2,3百个账号,体力活的不干,想想。


1.首先把所有的用户全部放到一个文本文件中,因为有重复,所以先预处理保证唯一性。
看起来就像这样。

Barack Obama
George WBush
William JClinton
...

 

2, cat /etc/password | cut -f1,4 -d: on Int,qa,stg and prod env and paste to a execl sheets named "INT","QA","STG" and "PROD",这样就 有了一张每个不同机器上已有用户的worksheet.

 

3, 将需要建立账号的用户放在另外一张工作表中(user),然后在这张表中使用execel的vlookup函数,将那些不在某个主机上的用户列出来。

=IF(ISNA(VLOOKUP(C2,QA!$A$1:$A$51,,FALSE)),LOWER(C2),"")

于是得到这样一张工作表

Second   Name FirstName Account
QA INT STG FND
Barack Obama BObama
bobama
bobama
George WBush GWBush
gwbush gwbush

William JClinton WClinton


wjclinton wjclinton








































 

现在,那些在某一主机上需要建立的账号会自动被创建。

下来,需要根据这个列表来进行自动账号建立和邮件的发送。哦,还有密码生成。 

excel没有自动密码生成工具,于是找了一个现成的,只不过是把6位长度改成8位

=CONCATENATE(IF(INT(RAND()*2)=0,CHAR(INT(RAND()*9+48)),IF(INT(RAND()*2)=0,
CHAR(INT(RAND()*25+65)),CHAR(INT(RAND()*25+97)))),IF(INT(RAND()*2)=0,
CHAR(INT(RAND()*9+48)),IF(INT(RAND()*2)=0,CHAR(INT(RAND()*25+65)),
CHAR(INT(RAND()*25+97)))),IF(INT(RAND()*2)=0,CHAR(INT(RAND()*9+48)),
IF(INT(RAND()*2)=0,CHAR(INT(RAND()*25+65)),CHAR(INT(RAND()*25+97)))),
IF(INT(RAND()*2)=0,CHAR(INT(RAND()*9+48)),IF(INT(RAND()*2)=0,CHAR(INT(RAND()*25+ 65)),
CHAR(INT(RAND()*25+97)))),IF(INT(RAND()*2)=0,CHAR(INT(RAND()*9+48)),
IF(INT(RAND()*2)=0,CHAR(INT(RAND()*25+65)),CHAR(INT(RAND()*25+97)))),
IF(INT(RAND()*2)=0,CHAR(INT(RAND()*9+48)),
IF(INT(RAND()*2)=0,CHAR(INT(RAND()*25+65)),CHAR(INT(RAND()*25+97)))), IF(INT(RAND()*2)=0,CHAR(INT(RAND()*9+48)),
IF(INT(RAND()*2)=0,CHAR(INT(RAND()*25+65)),CHAR(INT(RAND()*25+97)))),IF(INT(RAND ()*2)=0,CHAR(INT(RAND()*9+48)),
IF(INT(RAND()*2)=0,CHAR(INT(RAND()*25+65)),CHAR(INT(RAND()*25+97)))))

 

还不错

 

pX1M3246
m3302itJ
p12E6y06
6KYF70kE
rfATL22q
2r08e707
323B2lun
T2CsO848
37Y0niab

 

5.  哦,关键时候发现solaris的useradd没有-p这个选项,无法在命令行中以批得方式输入密码,ugh。

google 之

got this

 

PW=$1
PWSTRING=`perl -e "print crypt('"${PW}"', (('a'..'z', 'A'..'Z',
'0'..'9', '.', '/')[int(rand(64))].('a'..'z', 'A'..'Z', '0'..'9', '.',
'/')[int(rand(64))]));"`
echo "newuser:${PWSTRING}:UID:GID:New User:/home/newuser:/bin/sh" >> /
etc/passwd
pwconv

 

 

6. 根据自己的要求做些修改,得到一个原型脚本

 

#cat createuser.pl

 

#!/usr/bin/bash

# Script to create user and send a notificat email

# createuser.pl {Username} {Password} {EmailAddress}

 

ENV=INT

USERNAME=$1
PW=$2

EMAIL=$3


LASTUID=`tail -1 /etc/passwd | cut -f3 -d:`
((NEWUID=$LASTUID+1))
GROUPID=1

GROUP=other
REALNAME=
HOMEDIR=/export/home/$USERNAME
SHELL="/usr/bin/bash"

ENCPWD=`perl -e "print crypt('"${PW}"', (('a'..'z', 'A'..'Z', '0'..'9', '.', '/')[int(rand(64))].('a'..'z', 'A'..'Z', '0'..'9', '.', '/')[int(rand(64))]));"`

 

mkdir $HOMEDIR

echo "${USERNAME}:${ENCPWD}:${NEWUID}:${GROUPID}:${REALNAME}:${HOMEDIR}:${SHELL}" >> /etc/passwd
chown $USERNAME $HOMEDIR
chgrp $GROUP $HOMEDIR
chmod 755 $HOMEDIR
pwconv

 

#send email (using sendemail perl script )

./sendemail -f myemail@mydomain.com -t $EMAIL -u "Account Created" -m " Your account created\n username:$USERNAME \n password:$PWD \n \n Please change your password when first login\n Thanks \n\n -:)"

 

a user list file users.txt with two colum, username and password like this

testa pwda

testb pwdb

...

 

and then

 

cat users.txt | while read a ; do user=($a); ./createuser.pl ${user[0]} ${user[1]} ${user[2]} ;done

 

 

test


-bash-3.00# finger testa
Login name: testa
Directory: /export/home/testa           Shell: /usr/bin/bash
Last login Tue Mar  2 17:49 on pts/2 from localhost
No unread mail
No Plan.
-bash-3.00# id testa
uid=7779(testa) gid=1(other)
-bash-3.00# ssh testa@localhost
Password:
Last login: Tue Mar  2 17:49:49 2010 from localhost
Sun Microsystems Inc.   SunOS 5.10      Generic January 2005
-bash-3.00$

 

Tested ok on my Virtual Box





----------------------------------------------
改进

突然发现自己够蠢的,把简单的事情给搞复杂了,用什么excel,unix下的处理文本的工具还不够强吗,把所有要建的用户放到一个文件中,然后在建立前去passwd里查下不就行了吗,唉,够蠢的。

#!/usr/bin/bash



# Script to create user and send a notificat email
# createuser.pl {Username} {Password} {EmailAddress}
# created by xaliyan



create()
{
USERNAME=$1
PW=$2
EMAIL=$3



LASTUID=`tail -1 /etc/passwd | cut -f3 -d:`
((NEWUID=$LASTUID+1))
GROUPID=778

GROUP=staff #need to changed in differnet env
REALNAME=
HOMEDIR=/export/home/$USERNAME
SHELL="/usr/bin/bash"
EXIST=`grep "^${USERNAME}:" /etc/passwd`



if [ $EXIST ]; then
echo "[Failed ] $USERNAME"
continue
fi



echo "[Created] $USERNAME"
ENCPWD=`perl -e "print crypt('"${PW}"', (('a'..'z', 'A'..'Z', '0'..'9', '.', '/')\
[int(rand(64))].('a'..'z', 'A'..'Z', '0'..'9', '.', '/')[int(rand(64))]));"`

mkdir $HOMEDIR
echo "${USERNAME}:${ENCPWD}:${NEWUID}:${GROUPID}:${REALNAME}:${HOMEDIR}:${SHELL}" >> /etc/passwd
chown $USERNAME $HOMEDIR
chgrp $GROUP $HOMEDIR
chmod 755 $HOMEDIR
pwconv

# send email (using sendemail perl script )

./sendemail -q -f who@where.com -t $EMAIL\
-u "Account Created in $ENV "\
-m " Your account in `echo $ENV | tr [:lower:] [:upper:]` environment has been created!\n\n\
username:$USERNAME \n\
password:$PW \n \n\
If you want to change the password, please login $AUTHMASTER and using 'passwd' command.\
And wait about 5 minutes for sync. \n\n\
This mail generated and send by script automaticly. Any problems in login, please let me know. Thanks \n\n "
}

export ENV=`hostname | cut -f3 -d-`
export AUTHMASTER=fnd-authmaster-${ENV}.dev.xxxxxxx.com
echo Creating users from $1 in $ENV environment...
echo ---------------------------------------------
cat $1 | while read LINE ; do REC=($LINE); create ${REC[0]} ${REC[1]} ${REC[2]} ;done



 save the userlist in txt file with 3 colum, username,password,email like this

aenglish      ******       abc@domain.com
amoraes      ******      bcd@domain.com
asteiner       ******      def@domain.com

...

 

run

-bash-3.00# ./createuser.sh users.txt | tee log.stg
Creating users from users.txt in stg environment...
---------------------------------------------
[Failed ] aenglish
[Created] amoraes
[Created] asteiner
[Created] athinh
[Failed ] achernikov
[Failed ] bzagarella
[Created] bporter
...


遗留问题,其实可以干脆的抛弃excel了,有个solaris下的pwd gen

#!/bin/ksh
# Random password generator.
# Pass in the length of the password to generate.
#
# by Michael Roth, 2006
#set -x

RANDOM=$$

if [ "$#" -ne "1" ]; then
echo "Usage: ${0##*/} PASSWORDLENTGH"
echo "\te.g. ${0##*/} 8"
exit 1
fi

if [ ! -z `echo "$1" | tr -d "[:digit:]"` ]; then
echo "Usage: ${0##*/} PASSWORDLENTGH"
echo "\te.g. ${0##*/} 8"
echo "\nError: PASSWORDLENGTH must be a number!"
exit 1
fi

if [ ! "$1" -gt "0" ]; then
echo "Usage: ${0##*/} PASSWORDLENTGH"
echo "\te.g. ${0##*/} 8"
echo "\nError: PASSWORDLENGTH must be greater then 0!"
exit 1
fi

#
# Main
#

# Modify STRING as source
STRING='q w e r t y u i o p a s d f g h j k l z x c v b n m Q W E R T Y U I O P A S D F G H J K L Z X C V B N M 1 2 3 4 5 6 7 8 9 0 _ - ! ? = '

# password length
LENGTH="$1"

typeset -i index
index=1

IFS_SAV="$IFS"
IFS=" "

# Put $STRING in an array
for i in `echo $STRING`
do
array[$index]=$i
((index=index+1))
done

string_len=${#array[*]}

IFS="$IFS_SAV"

typeset -i pwlen
pwlen=0

PASS=""

while [ "$pwlen" -lt "$LENGTH" ]
do
index=$(($RANDOM % $string_len))
PASS="$PASS${array[$index]}"
((pwlen=pwlen+1))
done

echo "Your password: $PASS"
exit 0





##############################################################################
### This script is submitted to BigAdmin by a user of the BigAdmin community.
### Sun Microsystems, Inc. is not responsible for the
### contents or the code enclosed.
###
###
### Copyright Sun Microsystems, Inc. ALL RIGHTS RESERVED
### Use of this software is authorized pursuant to the
### terms of the license found at
###
##############################################################################


阅读(1317) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~