编写脚本实现登陆远程主机;生成10个随机数保存于数组中找出其最大值和最小值;赋值数组用冒泡算法实现升序或降序排序;总结查看系统负载的几种命令;编写脚本,PING测试指定网段内IP通断;计划备份任务

1、编写脚本实现登陆远程主机

具体要求:使用expect和shell脚本两种形式

1.1使用shell脚本

# 安装expect包
[root@centos8301]#yum -y install expect
[root@centos8301]#rpm -qa expect
expect-5.45.4-5.el8.x86_64
[root@centos8301]#
[root@centos8301]#
[root@centos8301]#cat ssh_shell.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        WuDongWuXia
#QQ:            1050572574@qq.com
#Date:          2021-12-27
#FileName:      ssh_shell.sh
#URL:           www.shoneinfo.cn
#Description:   The Test Script
#Copyright (C):2021 All rights reserved
#*********************************************************************************************
#!/bin/bash
ip=$1
user=$2
password=$3
expect <<EOF
set timeout 20
spawn ssh $user@$ip
expect {
"yes/no ?" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
expect "]#" { send "hostname -I\n" }
expect eof
EOF
[root@centos8301]#bash ssh_shell.sh 192.168.250.7 root sre2021
spawn ssh root@192.168.250.7
root@192.168.250.7's password:
Last login: Sun Dec 26 10:07:40 2021 from 192.168.250.101
[root@CentOS7 ~]# hostname -I
192.168.250.7
[root@CentOS7 ~]# [root@centos8301]#
自动退回

注:ssh连接出现 The authenticity of host can't be eshell脚本的执行方式stablished的问题,试验就无法完成,需要修改/etc/ssh/ssh_config文件的配置,以后则不会再出现此问题,解决方案就是在/etc/ssh/ssh_config最后面添加:

StrictHostKeyChecking no

UserKnownHostsFile /dev/null

##centos7配置ip地址#1.2使用expect

PDF文档 《6.4 交互式转centos7关闭防火墙化批处理工具 expect》

[root@centos8301]#yum -y install expect
方法一:
[root@centos8301]#chmod +x remote_ssh1
[root@centos8301]#cat remote_ssh1
#!/usr/bin/expect
spawn ssh 192.168.250.7
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "sre2021\n" }
}
interact
[root@centos8301]#./remote_ssh1
spawn ssh 192.168.250.7
root@192.168.250.7's password:
Last login: Sun Dec 26 09:44:06 2021 from 192.168.250.101
[root@CentOS7 ~]# hostname -I
192.168.250.7
[root@CentOS7 ~]#
方法二:
[root@centos8301]#chmod +x remote_ssh2
[root@centos8301]#cat remote_ssh2
#!/usr/bin/expect
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
interact
[root@centos8301]#./remote_ssh2 192.168.250.7 root sre2021
spawn ssh root@192.168.250.7
root@192.168.250.7's password:
Last login: Sun Dec 26 09:55:00 2021 from 192.168.250.101
[root@CentOS7 ~]# hostname -I
192.168.250.7
[root@CentOS7 ~]# 

2、生成10个shell脚本编程100例随机数保存于数组中,并找出其最大值和最小值

[root@centos8301]#cat max_min_array.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        WuDongWuXia
#QQ:            1050572574@qq.com
#Date:          2021-12-27
#FileName:      max_min_array.sh
#URL:           www.shoneinfo.cn
#Description:   The Test Script
#Copyright (C):2021 All rights reserved
#*********************************************************************************************
#!/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@centos8301]#ll
total 64
-rw-r--r--  1 root root   754 Dec 27 23:15 max_min_array.sh
[root@centos8301]#chmod +x max_min_array.sh
[root@centos8301]#ll
total 64
-rwxr-xr-x  1 root root   754 Dec 27 23:15 max_min_array.sh
[root@centos8301]#./max_min_array.sh
All numbers are 28768 27993 1327 4408 9101 15429 24600 23973 13690 25369
Max is 28768
Min is 1327

3、赋值数组,用冒泡算法实现升序或降序排序

输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序

#  手工输入多个数,实现升序排序
[root@centos8301]#
[root@centos8301]#cat maopao_paixu.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        WuDongWuXia
#QQ:            1050572574@qq.com
#Date:          2021-12-27
#FileName:      maopao_paixu.sh
#URL:           www.shoneinfo.cn
#Description:   The Test Script
#Copyright (C):2021 All rights reserved
#*********************************************************************************************
read -p "请输入正整数字,空格隔开:" num
a=( $num )
for ((i=1;i<${#a[*]};i++))
do
for ((j=0;j<${#a[*]}-i;j++))
do
if [ ${a[$j]} -gt ${a[`expr $j + 1`]} ]
then
tmp=${a[`expr $j + 1`]}
a[`expr $j + 1`]=${a[$j]}
a[$j]=$tmp
fi
done
done
echo ${a[@]}
[root@centos8301]#
# 执行结果
[root@centos8301]#bash maopao_paixu.sh
请输入正整数字,空格隔开:12 32 65 2 6 9 8 7 333 666 999 10000
2 6 7 8 9 12 32 65 333 666 999 10000
# 随机生成十个数,并实现升序排序
[root@centos8301]#cat maopao_paixu2.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        WuDongWuXia
#QQ:            1050572574@qq.com
#Date:          2021-12-27
#FileName:      maopao_paixu2.sh
#URL:           www.shoneinfo.cn
#Description:   The Test Script
#Copyright (C):2021 All rights reserved
#*********************************************************************************************
#!/bin/bash
declare -a nums
for ((i=0;i<10;i++));do
nums[$i]=$RANDOM
done
echo "All numbers are ${nums[*]}"
a=( ${nums[*]} )
for ((i=1;i<${#a[*]};i++))
do
for ((j=0;j<${#a[*]}-i;j++))
do
if [ ${a[$j]} -gt ${a[`expr $j + 1`]} ]
then
tmp=${a[`expr $j + 1`]}
a[`expr $j + 1`]=${a[$j]}
a[$j]=$tmp
fi
done
done
echo  "升序排序结果:" ${a[@]}
[root@centos8301]#
#运行结果
[root@centos8301]#bash maopao_paixu2.sh
All numbers are  21222 4767 6289 29151 28710 30871 14052 16569 25375 17685
升序排序结果:      4767 6289 14052 16569 17685 21222 25375 28710 29151 30871

4、总结查看系统负载的几种命令

具体要求:总结查看系统负载的几种命令,总结top命令的指标大概什么含义

4.1 CPU

top指标详解
load系统运维包括哪些内容 average:显示的是最近1linux操作系统基础知识分钟、5分钟和15分钟的系统平均负载,即任务队列的平均长度。
监控标准:单个CPU的平均负载= load average / CPU核心数

单个CPU的平均负载在0.7的上下时,应该考虑shell脚本入门就去花时间调查原因了。系统负载长期徘徊于 1.00,那么就应该马上解决这个问题
Task系统运维面试题及答案s:八省联考成绩查询入口 29 total 进程总数为29
running 正在运行的进程数
sleeping 睡眠的进程数

stopped 停止的进程数

zombie 僵尸进程数

Cpu(s):用户空间占用CPU百分比

sy 内核空间占用CPU百分比

ni 用户进程空间内改变过优先级的进程占用CPU百分比

id 空闲CPU百分比

wa 等待输入输出的CPU时间百分比

hi 硬件中断

si 软件中断

[root@centos8301-shone-cn ]#top
top - 00:18:12 up  5:43,  2 users,  load average: 0.00, 0.00, 0.00
Tasks: 243 total,   1 running, 242 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.1 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.1 hi,  0.0 si,  0.0 st
MiB Mem :   3735.6 total,   3036.5 free,    251.0 used,    448.1 buff/cache
MiB Swap:   4096.0 total,   4096.0 free,      0.0 used.   3251.8 avail Mem
PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
6641 root      20   0   65564   5068   4156 R   0.7   0.1   0:00.14 top
1 root      20   0  249736  11536   8644 S   0.0   0.3   0:02.91 systemd
2 root      20   0       0      0      0 S   0.0   0.0   0:00.01 kthreadd
3 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_gp
4 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_par_gp
6 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 kworker/0:0H-events_highpri
9 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 mm_percpu_wq
10 root      20   0       0      0      0 S   0.0   0.0   0:00.02 ksoftirqd/0
# 安装sysstat工具包,可以实现对很多参数的监测
[root@centos8301-shone-cn ]#yum -y install sysstat-11.7.3-6.el8.x86_64
[root@centos8301-shone-cn ]#iostat
Linux 4.18.0-305.3.1.el8.x86_64 (centos8301-shone-cn)   12/28/2021      _x86_64_        (4 CPU)
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
0.10    0.01    0.12    0.03    0.00   99.74
Device             tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               0.75        18.48         7.00     383370     145309
scd0              0.00         0.00         0.00          2          0
[root@centos8301-shone-cn ]#
[root@centos8301-shone-cn ]#mpstat
Linux 4.18.0-305.3.1.el8.x86_64 (centos8301-shone-cn)   12/28/2021      _x86_64_        (4 CPU)
12:23:09 AM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
12:23:09 AM  all    0.10    0.01    0.07    0.03    0.02    0.03    0.00    0.00    0.00   99.74
[root@centos8301-shone-cn ]#
# 显示2秒内的CPU使用率,总共显示5次
[root@centos8301-shone-cn ]#sar -u 2 5
Linux 4.18.0-305.3.1.el8.x86_64 (centos8301-shone-cn)   12/28/2021      _x86_64_        (4 CPU)
12:24:26 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
12:24:28 AM     all      0.00      0.00      0.00      0.00      0.00    100.00
12:24:30 AM     all      0.00      0.00      0.25      0.00      0.00     99.75
12:24:32 AM     all      0.00      0.00      0.00      0.00      0.00    100.00
12:24:34 AM     all      0.12      0.00      0.12      0.00      0.00     99.75
12:24:36 AM     all      0.00      0.00      0.00      0.00      0.00    100.00
Average:        all      0.03      0.00      0.07      0.00      0.00     99.90
[root@centos8301-shone-cn ]#

4.2 内存

total:总计物理内存的大小系统运维工资一般多少
used:已使用的物理内存的大小。
free:可用物理内存有多少。
shared:多个进程共享的内存总额。
buffers:写入磁盘内存缓冲区的大小(经常进行磁盘IO的效率比较低,所以先将要写入磁盘的文件进行一定数量的shell脚本面试题缓冲,等缓冲数据到shell脚本编程100例达一定大小是一次性写进磁盘系统/运维,提升效率)
cached:从磁盘中读取shell脚本的执行方式内容的缓存大小(原理差不多)。

[root@centos8301-shone-cn ]#free -h
total        used        free      shared  buff/cache   available
Mem:          3.6Gi       254Mi       3.0Gi       9.0Mi       452Mi       3.2Gi
Swap:         4.0Gi          0B       4.0Gi
[root@centos8301-shone-cn ]#free -mh |awk /Mem/'{print $3}'
254Mi
[root@centos8301-shone-cn ]#free -mh |awk /Mem/'{print $2}'
3.6Gi
[root@centos8301-shone-cn ]#

4.3 硬盘

[root@centos8301]#lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk
├─sda1   8:1    0  200M  0 part /boot/efi
├─sda2   8:2    0  100G  0 part /
├─sda3   8:3    0    4G  0 part [SWAP]
└─sda4   8:4    0   50G  0 part /data
sr0     11:0    1 1024M  0 rom
[root@centos8301]#lsblk | grep disk
sda      8:0    0  200G  0 disk
[root@centos8301]#lsblk |grep disk|awk -F "[ G]+" '{print $4}'
200
[root@centos8301]#

4.4 I/O

[root@centos8301]#iostat
Linux 4.18.0-305.3.1.el8.x86_64 (centos8301-shone-cn)   12/28/2021      _x86_64_        (4 CPU)
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
0.10    0.01    0.12    0.03    0.00   99.74
Device             tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               0.75        18.02         7.42     383370     157737
scd0              0.00         0.00         0.00          2          0
[root@centos8301]#iostat -kx | grep sda| awk '{print $4}'
18.02
[root@centos8301]#

5、编写脚本,Pcentos查看系统版本ING测试指定网段内IP地址通断

具体要求:编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"facentosil!"

#for 循环
[root@centos8301-shone-cn ]#cat ping_for.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        WuDongWuXia
#QQ:            1050572574@qq.com
#Date:          2021-12-28
#FileName:      ping_for.sh
#URL:           www.shoneinfo.cn
#Description:   The Test Script
#Copyright (C):2021 All rights reserved
#*********************************************************************************************
#!/bin/bash
for i in {1..254}
do
ping -c 3 192.168.250.$i &>/dev/null
if [ $? -eq 0 ]
then
echo "$i连通性 success"
else
echo "$i连通性 fail"
fi
done
[root@centos8301-shone-cn ]#
# 输出结果
[root@centos8301-shone-cn ]#bash ping_for.sh
1连通性 fail
2连通性 fail
3连通性 fail
4连通性 fail
5连通性 fail
6连通性 fail
7连通性 success
8连通性 fail
9连通性 fail
...... 省略
# while 循环
[root@centos8301-shone-cn ]#cat ping_while.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        WuDongWuXia
#QQ:            1050572574@qq.com
#Date:          2021-12-28
#FileName:      ping_while.sh
#URL:           www.shoneinfo.cn
#Description:   The Test Script
#Copyright (C):2021 All rights reserved
#*********************************************************************************************
#!/bin/bash
i=1
while [ $i -lt 255 ]
do
ping -c3 192.168.250.$i &>/dev/null
if [ $? -eq 0 ]
then
echo "$i连通性 success"
else
echo "$i连通性 fail"
fi
let i++
done
[root@centos8301-shone-cn ]#
# 执行结果
[root@centos8301-shone-cn ]#bash ping_while.sh
1连通性 fail
2连通性 fail
3连通性 fail
4连通性 fail
5连通性 fail
6连通性 fail
7连通性 success
8连通性 fail
...... 省略

6、计划备份任务

具体要求:每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间系统运维工程师

[root@centos8301-shone-cn ]#cat back_etc.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        WuDongWuXia
#QQ:            1050572574@qq.com
#Date:          2021-12-28
#FileName:      back_etc.sh
#URL:           www.shoneinfo.cn
#Description:   The Test Script
#Copyright (C):2021 All rights reserved
#*********************************************************************************************
#!/bin/bash
BackupDIR="/backup"
Filename_tar="etcbak-"`date -d "-1 days" +%Y-%m-%d-%H`".tar.xz"
Filename_log="etcbak-"`date -d "-1 days" +%Y-%m-%d-%H`".log"
[ ! -d $BackupDIR ]&& mkdir -p /backup
tar Jcf $BackupDIR/$Filename_tar /etc > $BackupDIR/$Filename_log   2>&1
[root@centos8301-shone-cn ]#
[root@centos8301-shone-cn ]#chmod +x back_etc.sh
[root@centos8301-shone-cn ]#ln -s /data/back_etc.sh /usr/local/bin/
[root@centos8301-shone-cn ]#crontab -l
30 1 * * 1-5 /usr/local/bin/back_etc.sh
[root@centos8301-shone-cn ]#
# 预执行
[root@centos8301-shone-cn ]#
[root@centos8301-shone-cn ]#./back_etc.sh
[root@centos8301-shone-cn ]#ll -ah /backup
total 3.7M
drwxr-xr-x   2 root root   73 Dec 28 01:42 .
dr-xr-xr-x. 19 root root  282 Dec 28 01:11 ..
-rw-r--r--   1 root root   44 Dec 28 01:42 etcbak-2021-12-27-01.log
-rw-r--r--   1 root root 3.7M Dec 28 01:42 etcbak-2021-12-27-01.tar.xz
[root@centos8301-shone-cn ]#