Linux shell脚本练习
- 一、编写脚本登录远程主机。expect和shell脚本有两种形式)
- 二、在数组中生成10个随机数,找出其最大值和最小值
- 三、输入多个数值存储在数组中,冒泡算法升序或降序排序
- 四、编写脚本,使用for和while分别实现192.168.0.地址能否在0/24网段内ping通,若ping通则输出"success!",若ping不通则输出"fail!"
- 5.每周工作日1:30,/etc备份至/backup在目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz,日期是前一天
- 六、检查系统负载的几个命令,总结top命令的指标是什么意思?
一、编写脚本登录远程主机。expect和shell脚本有两种形式)
[root@localhost ~]# cat auto_login_expect #!/bin/expect set ip 192.168.1.118 set user root set password 123456 set timeout 15 spawn ssh $user@$ip expect {
"yes/no" {
send "yes\n"; exp_continue } "password" {
send "$password\n" } } interact [root@localhost ~]# expect auto_login_expect spawn ssh root@192.168.1.30 The authenticity of host '192.168.1.30 (192.168.1.30)' can't be established. ECDSA key fingerprint is SHA256:td1/tqC4WTpifU86hM76rYi9en5DtfSBICpCBdoj/pw. ECDSA key fingerprint is MD5:31:b4:9c:d6:b9:61:1c:5b:6d:23:23:d7:32:f9:1a:91. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.1.30' (ECDSA) to the list of known hosts. root@192.168.1.30's password: Last login: Fri Feb 11 22:57:37 2022 [root@study ~]# exit logout Connection to 192.168.1.30 closed. [root@localhost ~]# expect auto_login_expect spawn ssh root@192.168.1.30 root@192.168.1.30's password: Last login: Fri Feb 11 22:58:42 2022 from 192.168.1.31 [root@study ~]# [root@localhost ~]# cat remotelogin.sh #!/bin/bash IP=$1 USER=$2 PASSWORD=$3 sshpass -p $PASSWORD ssh -o StrictHostKeyChecking=no $USER@$IP
[root@localhost ~]# bash remotelogin.sh 192.168.1.30 root 123456
Last login: Fri Feb 11 23:42:12 2022 from 192.168.1.31
[root@study ~]#
[root@study ~]# hostname -I
192.168.1.30
[root@study ~]#
二、生成10个随机数保存于数组中,并找出其最大值和最小值
思想:使用RANDOM生成10个数放在数组中,把数组中的第一个数作为最大值(变量:max),也做为最小值(变量:min)。 然后取数组中的第二个和第一个数做比较,如果大于第一个数,则赋值为max,如果小于第一个数则保持第一个数为最大值; 同时,数组中的第二个和第一个数做比较,如果小于第一个数,则赋值为mix,如果大于第一个数则min保持第一个数为最小值 依照此法,一直到数组中最后一个数(即数组小标为9的数)的比较完成,就可得出最大值和最小值。
[root@felix ~]# cat arr_min_max.sh
#!/bin/bash
declare -i min max
declare -a nums
for ((i=0;i<10;i++)); do
nums[$i]=$RANDOM
[ $i -eq 0 ] && min=${nums[0]} && max=${nums[0]} && continue
[ ${nums[i]} -gt $max ] && max=${nums[i]} && continue
[ ${nums[i]} -lt $min ] && min=${nums[i]}
done
echo "all numbers are ${nums[*]}"
echo "max is" $max
echo "min is" $min
[root@felix ~]# bash arr_min_max.sh
all numbers are 26473 20770 11745 11618 32526 29224 17443 1254 12763 10058
max is 32526
min is 1254
[root@felix ~]#
三、输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序
[root@localhost ~]# cat arr_mao.sh
#!/bin/bash
read -p "请输入数组的长度:" lenth
for ((n=1;n<=$lenth;n++)); do
read -p "请输入1个数到数组,共[$lenth]个数,这是第[$n]个:" arr_nums[$n-1]
done
len=${
#arr_nums[*]}
echo "原来的数组:${arr_nums[*]}"
for ((j=0;j<$len;j++)); do
for ((i=0;i<$len-1;i++)); do
if [ ${arr_nums[$i]} -gt ${arr_nums[$i+1]} ];then
x=${arr_nums[$i]}
arr_nums[$i]=${arr_nums[$i+1]}
arr_nums[$i+1]=$x
fi
done
done
echo "整理后的数组:${arr_nums[@]}"
[root@localhost ~]# bash arr_mao.sh
请输入数组的长度:4
请输入1个数到数组,共[4]个数,这是第[1]个:3
请输入1个数到数组,共[4]个数,这是第[2]个:45
请输入1个数到数组,共[4]个数,这是第[3]个:422
请输入1个数到数组,共[4]个数,这是第[4]个:1
原来的数组:3 45 422 1
整理后的数组:1 3 45 422
[root@localhost ~]#
四、编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"
[root@localhost ~]# cat ping.sh
#!/bin/bash
ip=192.168.1.
for ((i=1;i<=254;i++));do
ping -c 1 $ip$i &>/dev/null
if [ $? -eq 0 ];then
echo $ip$i:success!
else
echo $ip$i:fail!
fi
done
[root@localhost ~]# bash ping.sh
192.168.1.1:success!
192.168.1.2:fail!
192.168.1.3:fail!
192.168.1.4:fail!
192.168.1.5:fail!
192.168.1.6:fail!
192.168.1.7:fail!
192.168.1.8:success!
*省略部分输出*
[root@localhost ~]# cat ping1.sh
#!/bin/bash
ip=192.168.1.
i=1
while [ $i -lt 255 ];do
ping -c 1 $ip$i &>/dev/null
[ $? -eq 0 ] && echo $ip$i:success! || echo $ip$i:fail!
let i++
done
[root@localhost ~]# bash ping1.sh
192.168.1.1:success!
192.168.1.2:fail!
192.168.1.3:fail!
*省略部分输出*
五、每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间
[root@localhost ~]# cat etcbak.sh
#!/bin/bash
[ -d /backup ] || mkdir -p /backup
tar -cJf /backup/etcbak-`date -d yesterday +%F-%H`.tar.xz /etc
[root@localhost ~]#
[root@localhost ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@localhost ~]# crontab -l
30 1 * * 1-5 bash baketc.sh &>/dev/null
[root@localhost ~]#
六、查看系统负载的几种命令,总结top命令的指标大概什么含义
- uptime命令可以获取主机运行的时间,以及系统cpu负载的情况(包括1分钟,5分钟,15分钟内的平均负载) 语法格式 uptime [参数]
[root@localhost ~]# uptime
09:45:15 up 6:16, 2 users, load average: 0.05, 0.03, 0.05
- top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,常用于服务端性能分析。
[root@localhost ~]# top
top - 09:52:37 up 6:23, 2 users, load average: 0.00, 0.01, 0.05
Tasks: 109 total, 1 running, 108 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.2 us, 0.2 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1863032 total, 1490776 free, 193936 used, 178320 buff/cache
KiB Swap: 3145724 total, 3145724 free, 0 used. 1514864 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2539 root 20 0 162092 2220 1544 R 0.7 0.1 0:00.07 top
31 root 20 0 0 0 0 S 0.3 0.0 0:42.72 kworker/0:1
1 root 20 0 128040 6616 4172 S 0.0 0.4 0:04.34 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.05 kthreadd
4 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
5 root 20 0 0 0 0 S 0.0 0.0 0:00.11 kworker/u256:0
参数 | 说明 |
---|---|
0.2 us, | 系统用户进程使用 CPU 百分比 |
0.0 sy | 内核中的进程占用 CPU 百分比 |
load average: 0.00, 0.01, 0.05 | 系统1分钟,5分钟,15分钟内平均负载 |
1863032 total | 内存总大小 |
193936 used | 已使用的内存大小 |
列名 | 含义 |
---|---|
PID | 进程ID |
NI | 进程优先级 |
S | 进程状态 |
%CPU | 上次更新到现在的 CPU 时间占用百分比 |
%MEM | 进程使用的物理内存百分比 |
TIME+ | 进程使用的 CPU 时间总计,单位 1/100 秒 |
COMMAND | 命令名/命令行 |