Linux bash从一个文件读取,然后从adduser读取到linux

时间:2014-09-27 14:23:48

标签: linux bash authentication pam

我想添加linux用户然后我想尽可能地限制它们。 (noshell等)来自名为users的文件。

这是我的代码但没有工作:

    while read line
    do
input = echo ($input | tr ":" "\n")

        #!/bin/bash
        # Script to add a user to Linux system
        if [ $(id -u) -eq 0 ]; then
            if [ $? -eq 0 ]; then
                echo "$username exists!"
                exit 1
            else
                pass=$(perl -e 'print crypt($ARGV[0], "password")' $input[1)
                useradd -m -p $input[1] $input[0]
                [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
            fi
        else
            echo "Only root may add a user to the system"
            exit 2
        fi
    done < /var/www/users

然后我想用noshell限制他们的帐户。 (但我知道我该怎么办。但是我无法正确地将输入与文件分开:/)

输入(用户):

john:lol
rambo:sanyi
cula:kari

非常感谢!

1 个答案:

答案 0 :(得分:0)

我认为,以下脚本将会:

#!/bin/bash

USERS=`cat /etc/passwd | cut -d: -f1`

if [ `id -u` -ne 0 ]
then
    echo "Login as Root"
else
    while read line
    do
        USER=`echo $line | cut -d ":" -f1`
        PASS=`echo $line | cut -d ":" -f2`

        echo $USERS | grep "${USER}" > /dev/null

        if [ $? -eq 0 ]
        then
            echo "Username ${USER} Exists!"
        else
            password=`perl -e 'print crypt("${PASS}", "salt")', "\n"`
            useradd -p "${password}" ${USER}

            echo "User ${USER} created!"
        fi

        i+=1
    done < /var/www/users
fi