shell用戶管理練習->
阿新 • • 發佈:2018-09-25
name tmp ted 4th cat 想要 生成 bash 名稱
用戶的添加與刪除練習
-> 腳本1(if then)
思路:
1.條件測試, 腳本使用案例, 創建用戶【交互式創建】
1.怎麽交互式 read -p
2.接收到對應字符串怎麽創建用戶 useradd
3.用戶是否存在,如果存在則不執行,如果不存在則執行
腳本實現:
#!/usr/bin/bash read -p "Please input a username: " user id $user &>/dev/null if [ $? -eq 0 ]; then echo "user $user already exists" else useradd $userif [ $? -eq 0 ];then echo "$user is created." fi fi
-> 腳本2(創建用戶+密碼)
[root@jumpserver-70 scripts]# cat user.sh #!/bin/bash #1.判斷用戶輸入是否為空 read -p "請輸入要創建用戶的數量 :" num if [[ -z $num ]];then echo "輸入的用戶名不能為空" exit 1 fi #2.判斷用戶輸入的是否為數字 if [[ ! "$num" =~ ^[0-9]+$ ]];thenecho "你輸入的不是數字" exit 1 fi #3.判斷用戶輸入的是否為空 read -p "請輸入要創建用戶的名稱 :" name if [[ -z $name ]];then echo "用戶名不能為空值" exit 1 fi #4.判斷用戶輸入的是否為數字 if [[ ! "$name" =~ ^[a-z]+$ ]];then echo "你輸入的不能是小寫字母" exit 1 fi #5.遍歷用戶輸入的數字 for i in $(seq $num);do useradd $name$i # 創建用戶echo "123" |passwd --stdin $name$i &>/dev/null #給新創建的用戶設置密碼123 echo "$name$i用戶創建成功,密碼為:123" done
-> 腳本3(for)
[root@jumpserver-70 scripts]# cat useradd.sh #!/usr/bin/bash for i in $(cat tt.txt) do id $i &>/dev/null if [ $? -ne 0 ];then useradd $i echo -e "\033[32m $i 用戶創建成功... \033[0m " else echo -e "\033[31m $i 用戶已經存在... \033[0m " fi done
tt.txt 只是一個測試文件,可以隨便寫內容
-> 腳本4(while)
[root@jumpserver-70 scripts]# cat while_user.sh #!/bin/bash while read user do id $user &>/dev/null if [ $? -ne 0 ];then useradd $user echo -e "\033[32m $user 用戶創建成功... \033[0m " else echo -e "\033[31m $user 用戶已經存在... \033[0m " fi done<user.txt
-> 腳本5 (while-添加指定用戶和密碼)
[root@jumpserver-70 scripts]# cat while_user_2.sh #!/bin/bash while read test do user=$(echo $test | awk ‘{print $1}‘) passwd=$(echo $test | awk ‘{print $2}‘) id $user &>/dev/null if [ $? -eq 0 ];then echo "用戶 $user 已經存在" else useradd $user && echo "$passwd" |passwd --stdin $user &>/dev/null echo "用戶 $user 創建成功。。密碼是 $passwd" fi done<test.txt
test文件內容:
[root@jumpserver-70 scripts]# cat test.txt
zhuzhu 123 haha 234 hehe 345
[root@jumpserver-70 scripts]# sh while_user_2.sh (實現的效果)
用戶 zhuzhu 創建成功。。密碼是 123
用戶 haha 創建成功。。密碼是 234
用戶 hehe 創建成功。。密碼是 345
-> 腳本6(隨機創建用戶和密碼)
8.添加user_00->user_09 10個用戶, 並且給他們設置一個隨機密碼, 密碼要求10位包含大小寫字母以及數字, 註意需要把每個用戶的密碼記錄到一個日誌文件中
[root@jumpserver-70 scripts]# cat user_random.sh #!/bin/bash user=user_0 for i in {0..9} do user_add=$user$i user_pass=$(openssl rand -base64 40 |sed ‘s#[^a-zA-Z0-9]##g‘|cut -c1-10) id $user_add &>/dev/null if [ $? -eq 0 ];then echo "用戶已經存在了" else useradd $user_add && echo "$user_pass" | passwd --stdin $user_add &>/dev/null echo "$user_add 用已經創建成功,密碼是 $user_pass" echo "$user_add $user_pass" >>user_passwd.txt fi done
echo "用戶是: $username 密碼是: $password" |tee -a /tmp/user.txt 也可以用漏鬥的方式,一遍生成一邊直接寫入腳本
->實現效果
[root@jumpserver-70 scripts]# cat user_passwd.txt user_00 +QuwBA1i06 user_01 4CrXFwP0ME user_02 xVYV8uB7a6 user_03 4OA6cTB5pu user_04 mj/4K48pYA user_05 vq5ogC4FAX user_06 xCszOnBEc1 user_07 ct4tHHcjNK user_08 iwLxFI2Rfx user_09 5/pYJRhoRK
-> 腳本7 (隨機字符-批量添加)-面試題
10.寫一個腳本,實現批量添加20個用戶,用戶名為user01-20,密碼為用戶名後面跟5個隨機字符。
[root@jumpserver-70 zuoye]# cat 10_user_add.sh #!/bin/bash for i in {01..20} do passwd=$( uuidgen | sed -r ‘s#[0-9-]+##g‘ | cut -c 1-5) user_pass=user$i$passwd id user$i &>/dev/null if [ $? -eq 0 ];then echo "用戶 user$i 已經存在。。。" else useradd user$i && echo "${user_pass}" | passwd --stdin user$i >>/dev/null echo "用戶 user$i 創建成功。。。" fi done
->批量刪除用戶腳本(不完善,僅作為練習參考)
[root@jumpserver-70 scripts]# cat del_user.sh #!/usr/bin/bash read -p "請輸入你想要刪除的用戶名稱:" name read -p "請輸入你想要刪除的用戶數量:" num if [[ ! $name =~ ^[a-Z]+$ ]];then echo "輸入錯誤,請輸入字母" exit 1 fi if [[ ! $num =~ ^[0-9]+$ ]];then echo "輸入純數字" exit 1 fi echo "你要刪除的用戶名稱是: $name 你要刪除的用戶數量是多少個: $num" read -p "你確定你要刪除? " ready for i in $(seq $num);do username=$name$i case $ready in y|Y|Yes|YES) id $username &>/dev/null if [ $? -eq 0 ];then echo "正在刪除${username}......" userdel -r $username echo "刪除完成" else echo "${username}不存在,無法刪除" fi ;; n|No|NO|no) echo "你選擇退出,不會刪除任何用戶....." exit 1 ;; *) echo "您輸入錯誤...." exit 1 ;; esac done
shell用戶管理練習->