当前位置:网站首页>shell循环练习
shell循环练习
2022-07-27 06:50:00 【空丶白灬】
1、 使用case实现成绩优良差的判断
#!/bin/bash
read -p "please input the grade:" grade
case "$grade" in
8[5-9]|9[0-9]|100)
echo "A"
;;
7[0-9]|8[0-5])
echo "B"
;;
6[0-9])
echo "C"
;;
*)
echo "D"
esac
2、for创建20用户
用户前缀由用户输入
用户初始密码由用户输入
例如:test01,test10
#!/bin/bash
read -p "please input qianzhui:" name
read -p "please input passwd:" password
for ((i=1;i<=20;i++))
do
if [ $i -lt 10 ];then
user="$name"0$i
else
user=$name$i
fi
if ! id -u $user &>/dev/null ; then
useradd $user
echo "$password" | passwd --stdin $user &>/dev/null
else
echo "$user is exists"
fi
done
3、 for ping测试指网段的主机
网段由用户输入,例如用户输入192.168.2 ,则ping 192.168.2.10 — 192.168.2.20
UP: /tmp/host_up.txt
Down: /tmp/host_down.txt
#!/bin/bash
read -p "please input the ip duan :" ip
for i in {
10..20}
do
ping -c3 $ip.$i &> /dev/null
if [ $? -eq 0 ] ;then
echo "$ip.$i is up " >> /tmp/host_up.txt
else
echo "$ip.$i is down " >> /tmp/host_down.txt
fi
done
4、 使用for实现批量主机root密码的修改
成功或失败都必须记录
提示:主机IP存放在一个文件中
SSH:实现公钥认证,执行远程中主机命令
实现公钥认证
ssh-keygen 在用于管理的主上生成密钥对
ssh-copy-id -i 192.168.2.3
#!/bin/bash
read -p "changed passwd: " password
num=`cat ip | wc -l`
for ((i=1;i<=$num;i++))
do
ip=`head -$i ip | tail -1 | awk '{print $1}'`
port=`head -$i ip | tail -1 | awk '{print $2}'`
ping -c2 $ip &>/dev/null
if [ $? -eq 0];then
ssh -p $port root$ip echo $password | passwd --stdin root >/dev/null
if [ $? -eq 0];then
echo "success"
else
echo "fail"
fi
else
echo "not exist"
fi
done
边栏推荐
- Systematic explanation of unit testing: mockito
- The DrawImage method calls the solution of not displaying pictures for the first time
- vlan间路由(讲解+验证)
- Prior Attention Enhanced Convolutional Neural Network Based Automatic Segmentation of Organs at Risk
- Flink de duplication (I) summary of common de duplication schemes in Flink and Flink SQL
- 单元测试系统化讲解之Mockito
- Single arm routing (explanation + experiment)
- C程序代码的内存结构分析
- Plato farm is expected to further expand its ecosystem through elephant swap
- when的多条件查询
猜你喜欢

opengl-shader学习笔记:varying变量

STM32_找到导致进入HardFault_Handler的函数

js中的数组方法与循环

闭散列和开散列解决哈希冲突

在mac中使用docker来搭建oracle数据库服务器

C common function integration-3

Top ten interview questions for software testing (with answers and analysis)

TCP/IP协议分析(TCP/IP三次握手&四次挥手+OSI&TCP/IP模型)

(2022 Hangdian multi school III) 1009.package delivery (greedy)

Haikang H9 camera cannot be connected with xshell (SSH is not enabled)
随机推荐
STM32_ Find the cause of entering hardfault_ Handler's function
C common function integration-3
Cadence(十一)丝印调整和后续事项
Am I delayed by the code... Unfortunately, I became a programmer
Bingbing's learning notes: classes and objects (middle)
DRConv-pytorch改称输出和输入一样的尺寸
Understanding and learning of properties class and properties configuration file
单臂路由(讲解+实验)
C程序代码的内存结构分析
drawImage方法第一次调用不显示图片的解决方式
Regular expression foundation sorting
漏风的小棉袄……
ClickHouse 笔记1 | 简介、特点 | 基于CentOS7系统的安装与使用 | 常用数据类型 | MergeTree 表引擎 | SQL操作
Properties类和properties配置文件的理解学习
Gossip: Recently, many friends talk about going abroad
Plato Farm有望通过Elephant Swap,进一步向外拓展生态
Flink de duplication (I) summary of common de duplication schemes in Flink and Flink SQL
Codeforces Round #810 (Div.2) A-C
Graylog 日志服务器单节点部署
Will Flink CDC constantly occupy Oracle's memory by extracting Oracle's data, and finally cause oracle-040