1、编写脚本实现登陆远程主机。(使用expect和shell脚本两种形式)。
expect
[root@centos8 data]# cat autologin
#!/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 "y\n";exp_continue }
"password" { send "$password\n" }
}
interact
[root@centos8 data]# ll
total 4
-rwxr-xr-x 1 root root 211 Feb 19 15:47 autologin
[root@centos8 data]# ./autologin 10.0.0.8 root '1111111'
spawn ssh root@10.0.0.8
root@10.0.0.8's password:
Last login: Sat Feb 19 15:46:44 2022 from 10.0.0.17
[root@sakura:~] #
shell
[root@centos8 data]# cat autologin.sh
#!/bin/bash
ip=$1
user=$2
password=$3
expect <<EOF
spawn ssh $user@$ip
expect {
"yes/no" { send "y\n";exp_continue }
"password" { send "$password\n" }
}
expect eof
EOF
[root@centos8 data]# chmod +x autologin.sh
[root@centos8 data]# ll
total 8
-rwxr-xr-x 1 root root 211 Feb 19 15:47 autologin
-rwxr-xr-x 1 root root 171 Feb 19 16:02 autologin.sh
[root@centos8 data]# ./autologin.sh 10.0.0.8 root '1111111'
spawn ssh root@10.0.0.8
root@10.0.0.8's password:
Last login: Sat Feb 19 15:49:43 2022 from 10.0.0.17
[root@sakura:~] #
2、生成10个随机数保存于数组中,并找出其最大值和最小值
[root@centos8 data]# cat array
#!/bin/bash
declare -i min max
declare -a numb
for ((i=0;i<10;i++));do
numb[$i]=$[RANDOM%100]
[ $i -eq 0 ] && min=${numb[0]} && max=${numb[0]} && continue
[ ${numb[$i]} -gt $max ] && max=${numb[$i]} && continue
[ ${numb[$i]} -lt $min ] && min=${numb[$i]}
done
echo "all numbers are ${numb[*]}"
echo max is $max
echo min is $min
[root@centos8 data]# chmod +x array
[root@centos8 data]# ll
total 12
-rwxr-xr-x 1 root root 339 Feb 19 16:17 array
[root@centos8 data]# ./array
all numbers are 26696 28140 10773 27791 23354 23733 4711 15482 3627 15597
max is 28140
min is 3627
3、输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序
升序
[root@centos8 data]# cat bubblesort_up.sh
#!/bin/bash
declare -a num_arr
bubblesort_up(){
for (( i=0; i<10; i++)); do
numb[i]=$[RANDOM%100]
done
echo "numb are:${numb[*]}"
counts=$[${#numb[*]}-1]
for (( i=$counts; i>1; i-- )); do
for (( j=1; j<=i; j++ )); do
if [ ${numb[j-1]} -gt ${numb[j]} ];then
temp=${numb[j]}
numb[j]=${numb[j-1]}
numb[j-1]=$temp
fi
done
done
echo "sort numb:${numb[*]}"
}
bubblesort_up
[root@centos8 data]#chmod +x bubblesort_up.sh
[root@centos8 data]# ./bubblesort_up.sh
numb are:14 49 79 18 21 49 14 71 25 57
sort numb:14 14 18 21 25 49 49 57 71 79
降序
#!/bin/bash
declare -a num_arr
bubblesort_down(){
for (( i=0; i<10; i++)); do
numb[i]=$[RANDOM%100]
done
echo "numb are:${numb[*]}"
counts=$[${#numb[*]}-1]
for (( i=$counts; i>1; i-- )); do
for (( j=1; j<=i; j++ )); do
if [ ${numb[j-1]} -lt ${numb[j]} ];then
temp=${numb[j]}
numb[j]=${numb[j-1]}
numb[j-1]=$temp
fi
done
done
echo "sort numb:${numb[*]}"
}
bubblesort_down
[root@centos8 data]# chmod +x bubblesort_done.sh
[root@centos8 data]# ./bubblesort_done.sh
numb are:89 21 95 83 58 34 60 93 16 56
sort numb:95 93 89 83 60 58 56 34 21 16
4、总结查看系统负载的几种命令,总结top命令的指标大概什么含义(不要求全部写出来)
uptime
系统平均负载(1、5、15分钟的平均负载,一般不会超过1)
[root@centos8 data]# uptime
17:27:44 up 1 day, 9:03, 1 user, load average: 0.00, 0.00, 0.00
mpstat
显示CPU相关统计,需先下载systat包
[root@centos8 data]#yum -y install systat
[root@centos8 data]# mpstat
Linux 4.18.0-348.el8.x86_64 (centos8.sakura.com) 02/19/2022 _x86_64_ (8 CPU)
05:28:56 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
05:28:56 PM all 0.00 0.00 0.01 0.00 0.02 0.01 0.00 0.00 0.00 99.96
free
显示内存使用空间
[root@centos8 data]# free -h
total used free shared buff/cache available
Mem: 1.7Gi 252Mi 1.1Gi 8.0Mi 369Mi 1.3Gi
Swap: 2.0Gi 0B 2.0Gi
vmstat
虚拟内存信息
procs:
r:可运行(正运行或等待运行)进程的个数,和核心数有关
b:处于不可中断睡眠态的进程个数(被阻塞的队列的长度)
memory:
swpd: 交换内存的使用总量
free:空闲物理内存总量
buffer:用于buffer的内存总量
cache:用于cache的内存总量
swap:
si:从磁盘交换进内存的数据速率(kb/s)
so:从内存交换至磁盘的数据速率(kb/s)
io:
bi:从块设备读入数据到系统的速率(kb/s)
bo: 保存数据至块设备的速率
system:
in: interrupts 中断速率,包括时钟
cs: context switch 进程切换速率
cpu:
us:Time spent running non-kernel code
sy: Time spent running kernel code
id: Time spent idle. Linux 2.5.41前,包括IO-wait time.
wa: Time spent waiting for IO. 2.5.41前,包括in idle.
st: Time stolen from a virtual machine. 2.6.11前, unknown
[root@centos8 data]# vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 1196308 5208 372828 0 0 0 0 13 17 0 0 100 0 0
isostat
统计CPU和IO信息
[root@centos8 data]# iostat
Linux 4.18.0-348.el8.x86_64 (centos8.sakura.com) 02/19/2022 _x86_64_ (8 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.00 0.00 0.04 0.00 0.00 99.96
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
nvme0n1 0.18 2.53 3.12 303397 374199
scd0 0.00 0.01 0.00 1041 0
df
查看磁盘利用率
[root@centos8 data]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 878M 0 878M 0% /dev
tmpfs 896M 0 896M 0% /dev/shm
tmpfs 896M 8.7M 887M 1% /run
tmpfs 896M 0 896M 0% /sys/fs/cgroup
/dev/nvme0n1p2 50G 2.0G 49G 4% /
/dev/nvme0n1p3 50G 390M 50G 1% /data
/dev/nvme0n1p1 1014M 206M 809M 21% /boot
tmpfs 179M 0 179M 0% /run/user/0
top
[root@centos8 data]# top
top - 20:22:24 up 1 day, 9:22, 1 user, load average: 0.00, 0.00, 0.00
# 系统当前时间,启动时间,在线用户,平均负载(等同于uptime)
Tasks: 200 total, 1 running, 199 sleeping, 0 stopped, 0 zombie
# 任务数(线程数)200,1个在运行,199个休眠,0个停止,0个僵尸进程
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
# us:用户空间
# sy:内核空间
# ni:调整nice时间
# id:空闲
# wa:等待IO时间
# hi:硬中断时间
# si:软中断(模式切换)时间
# st:虚拟机偷走(占用)的时间
MiB Mem : 1790.0 total, 1167.3 free, 253.2 used, 369.5 buff/cache
# 物理内存(KB)总空间,空闲内存(KB),已使用内存(KB),缓存
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 1380.6 avail Mem
# 虚拟内存(KB)总空间,空闲内存(KB),已使用内存(KB),可用物理内存(KB)大小
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+COMMAND
# PID进程号,用户,系统优先级(0-39,数字越小优先级越高),nice优先级(-20-19,数字越小优先级越高),占用虚拟内存情况,占用物理内存情况,共享内存空间,进程状态,cpu使用率,内存使用率,占用cpu时间(1%/s),进程关联的程序名
827 root 20 0 352176 10716 8932 S 0.4 0.6 1:42.89 vmtoolsd
11211 root 20 0 153440 5512 4252 S 0.4 0.3 0:00.71 sshd
1 root 20 0 238204 10644 7896 S 0.0 0.6 0:01.91 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.06 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.00 ksoftirqd/0
11 root 20 0 0 0 0 I 0.0 0.0 0:28.34 rcu_sched
12 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
13 root rt 0 0 0 0 S 0.0 0.0 0:00.33 watchdog/0
14 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cpuhp/0
15 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cpuhp/1
16 root rt 0 0 0 0 S 0.0 0.0 0:00.37 watchdog/1
17 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/1
18 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/1
20 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker/1:0H-events_highpri
21 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cpuhp/2
22 root rt 0 0 0 0 S 0.0 0.0 0:00.29 watchdog/2
23 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/2
24 root 20 0 0 0 0 S 0.0 0.0 0:00.01 ksoftirqd/2
26 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker/2:0H-events_highpri
27 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cpuhp/3
28 root rt 0 0 0 0 S 0.0 0.0 0:00.26 watchdog/3
29 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/3
30 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/3
5、编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"
for
[root@centos8 data]# cat chknet.sh
#!/bin/bash
net=192.168.0.
for i in {1..254};do
{
if
ping -c1 -w1 ${net}.${i} &>/dev/null;then
echo $net$i is success!
else
echo $net$i is fail!
fi
}&
done
wait
while
[root@centos8 data]# cat chknet2.sh
#!/bin/bash
net=192.168.0.
declare -i addr=1
while [ $addr -le 254 ];do
{
ping -c1 -w1 ${net}.${addr} &> /dev/null
if [ $? -eq 0 ];then
echo $net$addr is success!
else
echo $net$addr is fail!
fi
} &
let addr++
done
wait
6、每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间
[root@centos8 data]# cat backup.sh
#!/bin/bash
filename_tar=bak-`date -d '-1 day' +%Y-%m-%d-%H`.tar.gz
filename_log=bak-`date -d '-1 day' +%Y-%m-%d-%H`.log
[ ! -d $backup ] || mkdir -p /data/backup
tar -czvf /data/backup/${filename_tar} /etc/ > /data/backup/${filename_log} &> /dev/null
[root@centos8 data]# crontab -l
30 1 * * 1-5 bash /data/backup.sh