1. 使用case实现成绩优良中差的判断
[root@localhost ~]# vim score.sh #!/bin/bash read -p "请输入成绩:" score case $score in [0-5][0-9]) echo "差!" ;; 6[0-9]) echo "中" ;; [7-8][0-9]) echo "良" ;; 9[0-9]|100) echo "优!" ;; esac
测试:
[root@localhost ~]# sh score.sh 请输入成绩:40 差! [root@localhost ~]# sh score.sh 请输入成绩:65 中 [root@localhost ~]# sh score.sh 请输入成绩:99 优!
2. for创建20用户
用户前缀由用户输入
用户初始密码由用户输入
例如:test01,test10
[root@localhost ~]# vim uname.sh #!/bin/bash read -p "请输入用户前缀:" prefix read -p "请输入用户密码:" pas for i in {1..20} do user=$prefix$i password=$pas$i; if ! id -u $user &> /dev/null then useradd $user echo $password | passwd --stdin $user &> /dev/null else echo "用户已存在!" fi done
测试:
[root@localhost ~]# sh uname.sh 请输入用户前缀:CHQ 请输入用户密码:123456 [root@localhost ~]# cat /etc/passwd | grep CHQ* CHQ1:x:1019:1019::/home/CHQ1:/bin/bash CHQ2:x:1020:1020::/home/CHQ2:/bin/bash CHQ3:x:1021:1021::/home/CHQ3:/bin/bash CHQ4:x:1022:1022::/home/CHQ4:/bin/bash CHQ5:x:1023:1023::/home/CHQ5:/bin/bash CHQ6:x:1024:1024::/home/CHQ6:/bin/bash CHQ7:x:1025:1025::/home/CHQ7:/bin/bash CHQ8:x:1026:1026::/home/CHQ8:/bin/bash CHQ9:x:1027:1027::/home/CHQ9:/bin/bash CHQ10:x:1028:1028::/home/CHQ10:/bin/bash CHQ11:x:1029:1029::/home/CHQ11:/bin/bash CHQ12:x:1030:1030::/home/CHQ12:/bin/bash CHQ13:x:1031:1031::/home/CHQ13:/bin/bash CHQ14:x:1032:1032::/home/CHQ14:/bin/bash CHQ15:x:1033:1033::/home/CHQ15:/bin/bash CHQ16:x:1034:1034::/home/CHQ16:/bin/bash CHQ17:x:1035:1035::/home/CHQ17:/bin/bash CHQ18:x:1036:1036::/home/CHQ18:/bin/bash CHQ19:x:1037:1037::/home/CHQ19:/bin/bash CHQ20:x:1038:1038::/home/CHQ20:/bin/bash
3. for ping测试指网段的主机
网段由用户输入,例如用户输入192.168.128 ,则ping 192.168.128.10 — 192.168.128.20
UP: /tmp/host_up.txt
Down: /tmp/host_down.txt
[root@localhost ~]# vim forping.sh #!/bin/bash read -p "请输入网段:" ip for i in {10..20} do IP="$ip"."$i" echo $IP if ping -c 1 $IP &> /dev/null then echo "$IP is up " >> /tmp/host_up.txt else echo "$IP is down " >> /tmp/host_down.txt fi done
测试:
[root@localhost ~]# sh forping.sh 请输入网段:192.168.128 192.168.128.10 192.168.128.11 192.168.128.12 192.168.128.13 192.168.128.14 192.168.128.15 192.168.128.16 192.168.128.17 192.168.128.18 192.168.128.19 192.168.128.20 [root@localhost ~]# cat /tmp/host_down.txt 192.168.128.10 is down 192.168.128.11 is down 192.168.128.12 is down 192.168.128.13 is down 192.168.128.14 is down 192.168.128.15 is down 192.168.128.16 is down 192.168.128.17 is down 192.168.128.18 is down 192.168.128.19 is down 192.168.128.20 is down
4. 使用for实现批量主机root密码的修改
成功或失败都必须记录
提示:主机IP存放在一个文件中
SSH:实现公钥认证,执行远程中主机命令
实现公钥认证
ssh-keygen 在用于管理的主上生成密钥对
ssh-copy-id -i 192.168.2.3
[root@localhost ~]# vim ssh.sh #!/bin/bash ssh-keygen -f /root/.ssh/id_rsa -P '' -q &>/dev/null for ip in `cat ipfile` do echo $ip ssh-copy-copy-id root@$ip done for ip in `cat ipfile` do echo $ip ssh root@ip "echo redhat | passwd --stdin root" &>/dev/null if [ "$?" -eq 0 ];then echo "host $ip successfully update passwd" else echo "host $ip error update passwd" fi done