1妨托、編寫腳本實現(xiàn)登陸遠(yuǎn)程主機派诬。(使用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個隨機數(shù)保存于數(shù)組中秦忿,并找出其最大值和最小值
[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、輸入若干個數(shù)值存入數(shù)組中蛾娶,采用冒泡算法進(jìn)行升序或降序排序
升序
[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灯谣、總結(jié)查看系統(tǒng)負(fù)載的幾種命令,總結(jié)top命令的指標(biāo)大概什么含義(不要求全部寫出來)
uptime
系統(tǒng)平均負(fù)載(1蛔琅、5胎许、15分鐘的平均負(fù)載,一般不會超過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相關(guān)統(tǒng)計罗售,需先下載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
顯示內(nèi)存使用空間
[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
虛擬內(nèi)存信息
procs:
r:可運行(正運行或等待運行)進(jìn)程的個數(shù)辜窑,和核心數(shù)有關(guān)
b:處于不可中斷睡眠態(tài)的進(jìn)程個數(shù)(被阻塞的隊列的長度)
memory:
swpd: 交換內(nèi)存的使用總量
free:空閑物理內(nèi)存總量
buffer:用于buffer的內(nèi)存總量
cache:用于cache的內(nèi)存總量
swap:
si:從磁盤交換進(jìn)內(nèi)存的數(shù)據(jù)速率(kb/s)
so:從內(nèi)存交換至磁盤的數(shù)據(jù)速率(kb/s)
io:
bi:從塊設(shè)備讀入數(shù)據(jù)到系統(tǒng)的速率(kb/s)
bo: 保存數(shù)據(jù)至塊設(shè)備的速率
system:
in: interrupts 中斷速率,包括時鐘
cs: context switch 進(jìn)程切換速率
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
統(tǒng)計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
# 系統(tǒng)當(dāng)前時間穆碎,啟動時間,在線用戶职恳,平均負(fù)載(等同于uptime)
Tasks: 200 total, 1 running, 199 sleeping, 0 stopped, 0 zombie
# 任務(wù)數(shù)(線程數(shù))200所禀,1個在運行,199個休眠放钦,0個停止色徘,0個僵尸進(jìn)程
%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:內(nèi)核空間
# ni:調(diào)整nice時間
# id:空閑
# wa:等待IO時間
# hi:硬中斷時間
# si:軟中斷(模式切換)時間
# st:虛擬機偷走(占用)的時間
MiB Mem : 1790.0 total, 1167.3 free, 253.2 used, 369.5 buff/cache
# 物理內(nèi)存(KB)總空間,空閑內(nèi)存(KB)操禀,已使用內(nèi)存(KB)褂策,緩存
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 1380.6 avail Mem
# 虛擬內(nèi)存(KB)總空間,空閑內(nèi)存(KB)床蜘,已使用內(nèi)存(KB)辙培,可用物理內(nèi)存(KB)大小
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+COMMAND
# PID進(jìn)程號蔑水,用戶邢锯,系統(tǒng)優(yōu)先級(0-39,數(shù)字越小優(yōu)先級越高)搀别,nice優(yōu)先級(-20-19丹擎,數(shù)字越小優(yōu)先級越高),占用虛擬內(nèi)存情況歇父,占用物理內(nèi)存情況蒂培,共享內(nèi)存空間,進(jìn)程狀態(tài)榜苫,cpu使用率护戳,內(nèi)存使用率,占用cpu時間(1%/s)垂睬,進(jìn)程關(guān)聯(lián)的程序名
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分別實現(xiàn)192.168.0.0/24網(wǎng)段內(nèi),地址是否能夠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