Chinaunix首页 | 论坛 | 博客
  • 博客访问: 103068974
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类:

2008-04-19 23:04:10

作者:lg    来自:

Linux AS4下测试例子:
#cat namelistfile
aa
bb
-bash-3.00[lg]# ./adduser.sh -f namelistfile
Reading usernamelist file(namelistfile) ...
namelistfile
Now Let's set some options or you can use default settings.
Would you like to add a new group to add these users to it?
Enter (yes)to create a new group otherwise you must verify the group.
Your Answer: yes
Please enter the group's name: aa
All these users will be added to group(aa)...
Adding group ...
The group(aa) is added!
The passwd is used by default 000000.
The user(aa) is added.
Changing password for user aa.
passwd: all authentication tokens updated successfully.
The user(aa)'s password is setted!
The passwd is used by default 000000.
The user(bb) is added.
Changing password for user bb.
passwd: all authentication tokens updated successfully.
The user(bb)'s password is setted!
==================================

脚本内容如下:
#!/bin/sh
#-----------------------------------
chkUID(){
    getUID(){
        id|sed -e 's/(.*$//' -e 's/^uid=//'
        }

    if [ "`getUID`" -ne 0 ]
    then
        echo -e "\tYou are not root!"
        exit 0
    fi
    }
chkUID
usagePRT(){
    echo ${USAGE:='USAGE:' `basename Linux AS4下测试例子:
#cat namelistfile
aa
bb
-bash-3.00[lg]# ./adduser.sh -f namelistfile
Reading usernamelist file(namelistfile) ...
namelistfile
Now Let's set some options or you can use default settings.
Would you like to add a new group to add these users to it?
Enter (yes)to create a new group otherwise you must verify the group.
Your Answer: yes
Please enter the group's name: aa
All these users will be added to group(aa)...
Adding group ...
The group(aa) is added!
The passwd is used by default 000000.
The user(aa) is added.
Changing password for user aa.
passwd: all authentication tokens updated successfully.
The user(aa)'s password is setted!
The passwd is used by default 000000.
The user(bb) is added.
Changing password for user bb.
passwd: all authentication tokens updated successfully.
The user(bb)'s password is setted!
==================================

脚本内容如下:
#!/bin/sh
#-----------------------------------
chkUID(){
    getUID(){
        id|sed -e 's/(.*$//' -e 's/^uid=//'
        }

    if [ "`getUID`" -ne 0 ]
    then
        echo -e "\tYou are not root!"
        exit 0
    fi
    }
chkUID
usagePRT(){
    echo ${USAGE:='USAGE:' `basename ` '-f namelistfile'}
    }
chkFILE(){
echo
    if [ ! "`awk 'NF!=2{print NF;exit;}' `" ] && [ "`awk 'NF!=2{print NF;exit;}' `" -ne 2 ] ; then
        echo -e "The file's format is not right!"
        exit 0
    fi
    }

userCHK(){
    for USER in `awk '{print ;}' `
    do
        if grep -wq $USER /etc/passwd ; then
            echo -e "The user($USER) has been added!"
            exit 1
        fi
        if echo $USER|grep -wq "^[0-9].*" ; then
            echo -e "The user($USER)'s name is wrong format!"
            exit 1
        fi
    done
    }

setOPT(){
    echo -e "Now Let's set some options or you can use default settings."
    setGRPNAME(){
        while :
        do
        echo -e "Would you like to add a new group to add these users to it?"
        echo -e "Enter (yes)to create a new group otherwise you must verify the group."
        printf "Your Answer: "
        read grpopt
        case $grpopt in
            yes)
            printf "Please enter the group's name: "
            read grpoptnew
            if cat /etc/group|sed 's/:.*//'|grep -wq $grpoptnew ; then
                echo "The group's name($grpoptnew) exist."
                exit
            else
                grpname=$grpoptnew
                echo -e "All these users will be added to group($grpname)..."
                echo -e "Adding group ..."
                if cp /etc/group /etc/group.$$ ; then
                    if groupadd $grpname ; then
                        echo -e "The group($grpname) is added!"
                        rm -f /etc/group.$$
                        break 1
                    else
                        echo -e "There's something wrong when adding the group($grpname)."
                        echo -e " *** Please recovered the group file. *** "
                        echo -e "You can cp /etc/group.$$ to /etc/group to recover."
                    fi
                else
                    echo "Error! Please check the program or your disk space."
                    exit 0
                fi
            fi
            ;;
            *) : ;;
        esac
        done
        }
    setGRPNAME
    }

addUSER(){
    if cp /etc/passwd /etc/passwd.$$ && cp /etc/shadow /etc/shadow.$$ ; then

    for user in `sed 's/ .*//' `
    do
    pass=`awk '{
     ~/$name/
     {print ;exit}
     } name=$user' `
    if [ -z "$pass" ] ; then
        echo -e "The passwd is used by default 000000."
        pass=000000
    fi
    if [ ${#pass} -lt 6 ] ; then
        echo -e "The user($user)'s password is too short!"
        echo -e "Use default password: 000000."
        pass=000000
    fi
    if useradd $user -g $grpname ; then
        echo -e "The user($user) is added."
            if echo $pass|passwd $user --stdin ; then
                echo -e "The user($user)'s password is setted!"
            else
                echo -e "The user($user)'s password is NOT set!"
            fi
    else
        echo -e "The user($user) is NOT add."
    fi
    done
    rm -f /etc/passwd.$$ /etc/shadow.$$
    else
        echo -e "There something wrong when backup the passwd and shadow file."
    fi
    }

if [ $# -ne 2 ] ; then
    usagePRT
    exit 0
fi

case "" in
    -f)
    if [ -f "" ] ; then
        echo -e "Reading usernamelist file""("")" "..."
        chkFILE
        userCHK
        setOPT
        addUSER
    else
        echo -e "There's no usernamelist file!"
    fi
    ;;
    *) usagePRT
    exit 0
    ;;
esac
` '-f namelistfile'}
    }
chkFILE(){
echo
    if [ ! "`awk 'NF!=2{print NF;exit;}' `" ] && [ "`awk 'NF!=2{print NF;exit;}' `" -ne 2 ] ; then
        echo -e "The file's format is not right!"
        exit 0
    fi
    }

userCHK(){
    for USER in `awk '{print ;}' `
    do
        if grep -wq $USER /etc/passwd ; then
            echo -e "The user($USER) has been added!"
            exit 1
        fi
        if echo $USER|grep -wq "^[0-9].*" ; then
            echo -e "The user($USER)'s name is wrong format!"
            exit 1
        fi
    done
    }

setOPT(){
    echo -e "Now Let's set some options or you can use default settings."
    setGRPNAME(){
        while :
        do
        echo -e "Would you like to add a new group to add these users to it?"
        echo -e "Enter (yes)to create a new group otherwise you must verify the group."
        printf "Your Answer: "
        read grpopt
        case $grpopt in
            yes)
            printf "Please enter the group's name: "
            read grpoptnew
            if cat /etc/group|sed 's/:.*//'|grep -wq $grpoptnew ; then
                echo "The group's name($grpoptnew) exist."
                exit
            else
                grpname=$grpoptnew
                echo -e "All these users will be added to group($grpname)..."
                echo -e "Adding group ..."
                if cp /etc/group /etc/group.$$ ; then
                    if groupadd $grpname ; then
                        echo -e "The group($grpname) is added!"
                        rm -f /etc/group.$$
                        break 1
                    else
                        echo -e "There's something wrong when adding the group($grpname)."
                        echo -e " *** Please recovered the group file. *** "
                        echo -e "You can cp /etc/group.$$ to /etc/group to recover."
                    fi
                else
                    echo "Error! Please check the program or your disk space."
                    exit 0
                fi
            fi
            ;;
            *) : ;;
        esac
        done
        }
    setGRPNAME
    }

addUSER(){
    if cp /etc/passwd /etc/passwd.$$ && cp /etc/shadow /etc/shadow.$$ ; then

    for user in `sed 's/ .*//' `
    do
    pass=`awk '{
     ~/$name/
     {print ;exit}
     } name=$user' `
    if [ -z "$pass" ] ; then
        echo -e "The passwd is used by default 000000."
        pass=000000
    fi
    if [ ${#pass} -lt 6 ] ; then
        echo -e "The user($user)'s password is too short!"
        echo -e "Use default password: 000000."
        pass=000000
    fi
    if useradd $user -g $grpname ; then
        echo -e "The user($user) is added."
            if echo $pass|passwd $user --stdin ; then
                echo -e "The user($user)'s password is setted!"
            else
                echo -e "The user($user)'s password is NOT set!"
            fi
    else
        echo -e "The user($user) is NOT add."
    fi
    done
    rm -f /etc/passwd.$$ /etc/shadow.$$
    else
        echo -e "There something wrong when backup the passwd and shadow file."
    fi
    }

if [ $# -ne 2 ] ; then
    usagePRT
    exit 0
fi

case "" in
    -f)
    if [ -f "" ] ; then
        echo -e "Reading usernamelist file""("")" "..."
        chkFILE
        userCHK
        setOPT
        addUSER
    else
        echo -e "There's no usernamelist file!"
    fi
    ;;
    *) usagePRT
    exit 0
    ;;
esac
阅读(838) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~