zoukankan      html  css  js  c++  java
  • shell用户管理->

    用户的添加与删除练习

    -> 脚本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 $user
        if [ $? -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]+$ ]];then
        echo "你输入的不是数字"
        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 "33[32m $i 用户创建成功... 33[0m "
        else
            echo -e "33[31m $i 用户已经存在... 33[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 "33[32m $user 用户创建成功... 33[0m "
        else 
            echo -e "33[31m $user 用户已经存在... 33[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

      

  • 相关阅读:
    Android测试入门篇
    SQL的基本知识
    正则表达式
    ES5语法
    vscode
    继承小结
    工作遇到的问题
    后台程序员的HTTP缓存
    xhr下载图片/服务器向客户端推送消息
    HTTP2.0
  • 原文地址:https://www.cnblogs.com/tim1blog/p/9698468.html
Copyright © 2011-2022 走看看