2014-09-27 51 views
0

我想添加linux用戶,然後我想盡可能地限制它們。 (noshell等)從一個名爲用戶的文件。Linux bash從文件讀取然後adduser到linux

這是我的代碼,但不工作:

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 

非常感謝您!

+0

它看起來像Mandar已經回答了你的問題,但以供將來參考,shebang行'#/斌/ bash'應該是非常_first_線!你的腳本。它在你的腳本中間沒用。因此,在通過複製和粘貼創建腳本時,您需要更加謹慎。 :) – 2014-09-27 16:31:41

回答

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 
+0

不知道爲什麼,但是當我用這個創建一個用戶,他不能夠驗證(與ssh,或與openvpn(PAM驗證)) – csib 2014-09-30 21:16:52

+0

但如果我:passwd gyuri它的工作正常。所以這是密碼問題。或者與我的知識:) – csib 2014-09-30 21:29:59