1、用shell腳本批量建立Linux用戶
實現要求:創建用戶student1到student50,指定組為student組!而且每個用戶需要設定一個不同的密碼!
#!/bin/bash for i in `seq 1 10` do useradd -G student student$i; echo student$i | passwd student$i --stdin; done
注意: `seq 1 10`不是‘’哦
實現要求:創建用戶,設置相同密碼
#!/bin/bash password="123456" for USER in user1 user2 user3 do useradd -m $USER echo -e "${password}\n${password}" | passwd $USER done
【說明: echo -n 不換行輸出: $echo -n "123" $echo "456" 最終輸出 123456 而不是 123 456 echo -e 處理特殊字符: \n 換行且光標移至行首 】
2、批量創建用戶(用戶交互版)
#!/bin/bash ################################# #user_add #v1.0 huangjiaping 2020-06-11 ################################ read -p "please input username password number of user: " username pass num echo " #===================================# #have add user #username:$username #password:$pass #number:$num #===================================# " read -p "Are you sure[y/n]: " action if [ "$action" = "y" ];then for i in `seq 1 $num`#注意:seq支持參數,而{1..$num}這種是錯誤,{}不支持參數的。 do { user=$username$i useradd $user echo $password|passwd $user --stdin &>/dev/null echo "$user is created" }& done wait echo "finish..." else echo "退出" exit fi
實現效果:
3、實現批量創建用戶(文件版)
#!/bin/bash if [ $# -eq 0 ];then #判斷是否有參數 echo "usage:`basename $0` file" exit 1 fi if [ ! -f $1 ];then #判斷參數是否是文件 echo "error file" exit 2 fi i=/root/user_file.txt
#for處理文件默認是按回車分隔,而不會以空格或tab鍵
#所以要重新定義分隔符
#IFS內部字段分隔符
#IFS=$'\n'
IFS=$'\n'
for line in `cat $i` do if [ ${#line} -eq 0 ];then #跳過用戶文件內容中的空行 continue fi user=`echo $line|awk '{print $1}'` pass=`echo $line|awk '{print $2}'` id $user &>/dev/null if [ $? -eq 0 ];then echo "user $user already exists" else useradd $user echo $pass | passwd $user --stdin echo "$user is created" fi done
用戶文件:user_file.txt
[root@localhost ~]# cat user_file.txt #這里是空格,注意要跳過 pp 123
實現效果:
[root@localhost ~]# bash -xv file_user_add.sh user_file.txt #!/bin/bash if [ $# -eq 0 ];then echo "usage:`basename $0` file" exit 1 fi + '[' 1 -eq 0 ']' if [ ! -f $1 ];then echo "error file" exit 2 fi + '[' '!' -f user_file.txt ']' i=/root/user_file.txt + i=/root/user_file.txt IFS=$'\n' + IFS=' ' for line in `cat $i` do if [ ${#line} -eq 0 ];then continue fi user=`echo $line|awk '{print $1}'` pass=`echo $line|awk '{print $2}'` id $user &>/dev/null if [ $? -eq 0 ];then echo "user $user already exists" else useradd $user echo $pass | passwd $user --stdin echo "$user is created" fi done cat $i ++ cat /root/user_file.txt + for line in '`cat $i`' + '[' 6 -eq 0 ']' echo $line|awk '{print $1}' ++ awk '{print $1}' ++ echo 'pp 123'注意:line獲取的數值 + user=pp echo $line|awk '{print $2}' ++ awk '{print $2}' ++ echo 'pp 123' + pass=123 + id pp + '[' 1 -eq 0 ']' + useradd pp + echo 123 + passwd pp --stdin 更改用戶 pp 的密碼 。 passwd:所有的身份驗證令牌已經成功更新。 + echo 'pp is created' pp is created
4、批量修改root用戶密碼
#!/bin/bash ############################### #user pass is check #v1.0 huangjiaping 2020-06-12 ############################### read -p "Please input your new passwd: " pass for ip in $(cat ip.txt) do { if [ ${#ip} -eq 0 ];then continue fi ping -c1 $ip &>/dev/null if [ $? -eq 0 ];then ssh $ip "echo $pass | passwd --stdin root" if [ $? -eq 0 ];then etho "$ip" >>ok_`date +%F`.txt else echo "$ip" >>fail_`date +%F`.txt fi else echo "$ip" >>fail_`date +%F`.txt fi }& done
實現效果:
4、實現一個系統工具箱
#!/bin/bash ################################ #system manage #v1.0,huangjiaping ############################### while : do menu(){ cat <<-EOF +....................................+ + h. help + + f. disk partition + + d. filesystem mount + + m. memory + + u. systemd load + + q. exit + +....................................+ EOF } read -p "please input [h for help: ]" action case "$action" in h) clear menu ;; f) fdisk -l ;; d) df -Th ;; u) uptime ;; q) break ;; "") ;; *) echo "error" ;; esac done echo "你退出了"
效果:
5、簡單實現自動化安裝apache
#!/bin/bash ########################## #install apache #v1.0 huangjiaping 2020-6-10 ########################## gateway=10.0.0.2 ping -c1 baidu.com &>/dev/null if [ $? -eq 0 ];then yum -y install httpd &>/dev/null systemctl restart httpd systemctl enable httpd sed -ri '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config setenforce 0 curl http://127.0.0.1 &>/dev/null if [ $? -eq 0 ];then echo "Apache is installed" else echo "Apache is error" fi elif ping -c1 $gateway &>/dev/null;then echo "gateway is ok,check dns... " else echo "check ip address" fi
實現效果:
6、簡單實現自動化多系統配置yum源
#!/bin/bash os_station=`cat /etc/redhat-release | awk '{print$4}' | awk -F"." '{print $1"."$2}'` yum_server=10.0.0.101 [ -d /etc/yum.repos.d ] || mkdir /etc/yum.repos.d/bak [ -d /etc/yum.repos.d/bak ] || mv /etc/yum.repos.d/* /etc/yum.repos.d/bak if [ "$os_station" = "7.4" ];then cat > /etc/yum.repos.d/centos7u3.repo <<-EOF [centos7] name=centos7 baseurl=file://$yum_server/centos7u3 enabled=1 gpgcheck=0 EOF echo "7.3 yum configure..." elif [ "$os_station" = "6.3" ];then cat > /etc/yum.repos.d/centos7u3.repo <<-EOF [centos7] name=centos7 baseurl=file://$yum.server/centos7u3 enabled=1 gpgcheck=0 EOF else echo "Error" fi
實現效果:
7、實現批量ip嗅探
#!/bin/bash for i in {2..200} do { ip=10.0.0.$i ping -c1 $ip &>/dev/null touch ip.txt if [ $? -eq 0 ];then echo "$ip is ok">>ip.txt else echo "$ip is no">>ip.txt fi }& done wait echo "finish..."
實現效果: