目录
1.1、用expect语言实现登录:这里要切记expect后要加空格在打括号。
3、输入若干个数值保存于数组中,采用冒泡算法进行升序或降序排序。
4、总结查看系统负载的几种命令,总结top命令的指标大概是什么含义。
7、统计CPU和设备io信息iostat,可以提供丰富的io性能状态数据,由sysstat包提供。
11、网络监视工具 iptraf-ng,对终端窗口大小有要求,太小无法展示。
12、系统资源统计 dstat,由pcp-system-tools包提供,但安装dstat包即可,可用于替代vmstat,iostat的功能。
13、综合监控工具 glances,由epel源提供包,centos7和8不能相互监控使用。
14、查看进程打开文件 lsof,list open files
5、编写脚本,使用for和while分别实现10.0.0.0/24网段内,地址是否能ping通,若能ping通则输出“success!”,若不能ping通则输出“fail!”
6、每周的工作日1.30,将/etc备份至/backup目录中,保存的文件格式为:“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间。
1、编写脚本实现登录远程主机
1.1、用expect语言实现登录:这里要切记expect后要加空格在打括号。
#!/usr/bin/expect
spawn ssh 10.0.0.7
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "123\n" }
}
interact
#编写如下cat代码
[00:07:32 root@Centos8 ~]#vim expectload
[00:07:38 root@Centos8 ~]#chmod +x ./expectload
[00:07:48 root@Centos8 ~]#cat ./expectload
#!/usr/bin/expect
spawn ssh 10.0.0.7
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "123\n" }
}
interact
[00:07:57 root@Centos8 ~]#./expectload
spawn ssh 10.0.0.7
root@10.0.0.7's password:
Last login: Sun Aug 7 00:07:12 2022 from 10.0.0.8
[00:08:12 root@Centos7 ~]#ls
#以下用变量法
[00:12:00 root@Centos8 ~]#vim expectloadbl
[00:15:13 root@Centos8 ~]#chmod +x ./expectloadbl
[00:15:29 root@Centos8 ~]#cat ./expectloadbl
#!/usr/bin/expect
set ip 10.0.0.7
set user root
set password 123
set timeout 10
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
interact
[00:15:36 root@Centos8 ~]#./expectloadbl
spawn ssh root@10.0.0.7
root@10.0.0.7's password:
Last login: Sun Aug 7 00:08:12 2022 from 10.0.0.8
[00:16:00 root@Centos7 ~]#ls
#以下用位置参数法
[00:24:00 root@Centos8 ~]#vim expectloadwzcs
[00:24:15 root@Centos8 ~]#cat expectloadwzcs
#!/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
[00:24:19 root@Centos8 ~]#chmod +x ./expectloadwzcs
[00:24:26 root@Centos8 ~]#./expectloadwzcs 10.0.0.7 root 123
spawn ssh root@10.0.0.7
root@10.0.0.7's password:
Last login: Sun Aug 7 00:16:00 2022 from 10.0.0.8
[00:24:44 root@Centos7 ~]#ls
# 0 代表第一个位置参数,1 代表第二个位置参数,2 代表第三个位置参数
1.2、用shell实现expect非交互式远程登录:
#!/bin/bash
# /#############################################################/
# Author: liuqixin
# Email: 450131xxx@qq.com
# Time: 2022-08-07 00:30:12
# Name: expectloads.sh
# Version: The test script V1.0
# Description:
# /#############################################################/
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 "useradd xixi\n" }
#expect " ]#" { send "echo 123 |passwd --stdin xixi\n" }
expect " root" { send "useradd xixi\n" } #获取root字符效果一致
expect " root" { send "echo 123 |passwd --stdin xixi\n" } #获取root字符
expect " root" { send "exit\n" }
expect eof
EOF
[00:48:03 root@Centos8 ~vim expectloads.sh23
[00:49:07 root@Centos8 ~]#cat expectloads.sh
#!/bin/bash
# /#############################################################/
# Author: liuqixin
# Email: 450131xxx@qq.com
# Time: 2022-08-07 00:30:12
# Name: expectloads.sh
# Version: The test script V1.0
# Description:
# /#############################################################/
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 "useradd xixi\n" }
expect " ~]#" { send "echo 123 |passwd --stdin xixi\n" }
expect " ~]#" { send "exit\n" }
expect eof
EOF
[00:49:12 root@Centos8 ~]#bash expectloads.sh 10.0.0.7 root 123
spawn ssh root@10.0.0.7
root@10.0.0.7's password:
Last login: Sun Aug 7 00:47:36 2022 from 10.0.0.8
#使用scp传输文件
[23:17:07 root@Centos8 ~]#vim expectsh.sh
[23:18:51 root@Centos8 ~]#cat expectsh.sh
#!/bin/bash
# /#############################################################/
# Author: liuqixin
# Email: 450131xx@qq.com
# Time: 2022-08-07 21:59:31
# Name: expectsh.sh
# Version: The test script V1.0
# Description:
# /#############################################################/
net=10.0.0
user=root
password=123
for i in 7 17;do
ip=$net.$i
expect <<EOF
set timeout 20
#spawn ssh $user@$ip
spawn scp /etc/sysconfig/selinux $user@$ip:/data
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
#expect "root" { send "useradd hehe\n" }
#expect "root" { send "echo 123 |passwd --stdin hehe\n" }
#expect "root" { send "exit\n" } #退出expect脚本及远程登录
expect eof #expect执行结束 退出
EOF
done
[23:19:05 root@Centos8 ~]#bash expectsh.sh
spawn scp /etc/sysconfig/selinux root@10.0.0.7:/data
root@10.0.0.7's password:
selinux 100% 547 807.7KB/s 00:00
spawn scp /etc/sysconfig/selinux root@10.0.0.17:/data
root@10.0.0.17's password:
selinux 100% 547 865.8KB/s 00:00
[23:19:11 root@Centos8 ~]#
2、生成10个随机数保存于数组中,并找出其最大值和最小值。
#代码分析:先循环生成10个随机数,如果第一个数是0,则定义为最小值和最大值,本轮循环结束。如果不是0,且此轮的数比上个最大值大,则把次轮数定义为最大值,本轮循环结束。如果次轮数比上个最小值更小,则把次轮数定义为新的最小值。
#!/bin/bash
# /#############################################################/
# Author: liuqixin
# Email: 450131xxx@qq.com
# Time: 2022-08-07 23:49:12
# Name: maxmin.sh
# Version: The test script V1.0
# Description:
# /#############################################################/
declare -i min max
declare -a numbers
for ((i=0;i<10;i++));do
numbers[$i]=$RANDOM
[ $i -eq 0 ] && min=${numbers[0]} && max=${numbers[0]} && continue
[ ${numbers[$i]} -gt $max ] && max=${numbers[$i]} && continue
[ ${numbers[$i]} -lt $min ] && min=${numbers[$i]}
done
echo "所有数字为:" ${numbers[*]}
echo "最大值是:" $max
echo "最小值是:" $min
[23:57:44 root@Centos8 ~]#vim maxmin.sh
[23:58:03 root@Centos8 ~]#cat maxmin.sh
#!/bin/bash
# /#############################################################/
# Author: liuqixin
# Email: 450131xxx@qq.com
# Time: 2022-08-07 23:49:12
# Name: maxmin.sh
# Version: The test script V1.0
# Description:
# /#############################################################/
declare -i min max
declare -a numbers
for ((i=0;i<10;i++));do
numbers[$i]=$RANDOM
[ $i -eq 0 ] && min=${numbers[0]} && max=${numbers[0]} && continue
[ ${numbers[$i]} -gt $max ] && max=${numbers[$i]} && continue
[ ${numbers[$i]} -lt $min ] && min=${numbers[$i]}
done
echo "所有数字为:" ${numbers[*]}
echo "最大值是:" $max
echo "最小值是:" $min
[23:58:08 root@Centos8 ~]#bash maxmin.sh
所有数字为: 5782 27484 32613 23592 20362 28537 12276 15480 3171 20447
最大值是: 32613
最小值是: 3171
[23:58:12 root@Centos8 ~]#
3、输入若干个数值保存于数组中,采用冒泡算法进行升序或降序排序。
[22:56:52 root@Centos8 ~]#vim random_num_sort.sh
[22:57:01 root@Centos8 ~]#cat random_num_sort.sh
#!/bin/bash
# /#############################################################/
# Author: liuqixin
# Email: 450131xxx@qq.com
# Time: 2022-08-08 00:18:31
# Name: random_num_sort.sh
# Version: The test script V1.0
# Description:
# /#############################################################/
declare -a num
#生成10个随机数字 数组即(python列表)
for i in {1..10};do
num[$i]=$RANDOM
echo ${num[$i]}
done
#获取数组的长度即个数
length=${#num[@]}
#两层循环,外层循环定义比较的轮数,轮数为数组长度减去1,从1开始
for ((i=1; i<=$length; i++));do
#确定比较相邻两个元素的位置,较大往后放,并且每轮比较后的最后一个元素下标要递减
#这里使用变量j代表左边比较元素的下标范围,下标从0开始
for ((j=0; j<=$length-i; j++));do
#定义左边比较的元素的值
left=${num[$j]}
#定义右边比较的元素的值
t=$[$j + 1]
right=${num[$t]}
#如果左边的元素比右边的元素的值大就互换元素的位置
if [[ $left -gt $right ]];then
#把左边的值保存到临时变量temp中
temp=$left
#把右边元素的值赋给左边的元素
num[$j]=$right
num[$t]=$temp
fi
done
done
echo "排序后的数字顺序为:" ${num[@]}
[22:57:06 root@Centos8 ~]#bash random_num_sort.sh
25096
32552
16606
18429
22285
8168
29951
10426
29794
25049
排序后的数字顺序为: 8168 10426 16606 18429 22285 25049 25096 29794 29951 32552
[22:57:11 root@Centos8 ~]#
4、总结查看系统负载的几种命令,总结top命令的指标大概是什么含义。
1、负载查询uptime
[23:22:48 root@Centos8 ~]#uptime
23:58:22 up 1:36, 2 users, load average: 0.00, 0.07, 0.08
[23:58:22 root@Centos8 ~]#w
23:58:46 up 1:36, 2 users, load average: 0.00, 0.06, 0.08
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 10.0.0.1 22:24 1.00s 0.05s 0.00s w
root pts/1 10.0.0.1 22:24 1:34m 10.36s 10.35s top
2、显示CPU相关统计 mpstat
来自于sysstat包
[23:58:46 root@Centos8 ~]#yum -y install sysstat
[00:00:20 root@Centos8 ~]#mpstat
Linux 4.18.0-348.el8.x86_64 (Centos8) 08/11/22 _x86_64_ (2 CPU)
00:00:26 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
00:00:26 all 1.16 0.03 2.63 0.30 0.21 0.18 0.00 0.00 0.00 95.49
[00:00:26 root@Centos8 ~]#mpstat 1 5
Linux 4.18.0-348.el8.x86_64 (Centos8) 08/11/22 _x86_64_ (2 CPU)
00:00:36 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
00:00:37 all 0.50 0.00 1.49 0.00 0.00 0.50 0.00 0.00 0.00 97.52
00:00:38 all 1.01 0.00 3.52 0.00 0.00 0.50 0.00 0.00 0.00 94.97
00:00:39 all 0.00 0.00 1.01 0.00 0.00 0.00 0.00 0.00 0.00 98.99
00:00:40 all 0.50 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 98.50
00:00:41 all 0.51 0.00 3.55 0.00 0.51 0.00 0.00 0.00 0.00 95.43
Average: all 0.50 0.00 2.11 0.00 0.10 0.20 0.00 0.00 0.00 97.09
3、查看进程实时状态 top和htop
top提供实时动态进程
排序:
P:已占据的CPU百分比,%CPU
top - 00:05:16 up 1:42, 2 users, load average: 0.00, 0.06, 0.07
Tasks: 163 total, 1 running, 162 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.5 us, 1.3 sy, 0.0 ni, 97.8 id, 0.0 wa, 0.2 hi, 0.2 si, 0.0 st
MiB Mem : 1950.0 total, 1306.3 free, 232.9 used, 410.8 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 1553.5 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
18 root 20 0 0 0 0 S 0.3 0.0 0:00.95 ksoftirqd/1
812 root 20 0 496952 31288 16376 S 0.3 1.6 0:55.04 tuned
1106 root 20 0 153816 8488 6896 S 0.3 0.4 0:34.96 sshd
1 root 20 0 172424 10420 7940 S 0.0 0.5 0:01.58 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.77 ksoftirqd/0
11 root 20 0 0 0 0 I 0.0 0.0 0:02.11 rcu_sched
12 root rt 0 0 0 0 S 0.0 0.0 0:00.05 migration/0
13 root rt 0 0 0 0 S 0.0 0.0 0:00.00 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.00 watchdog/1
17 root rt 0 0 0 0 S 0.0 0.0 0:00.05 migration/1
20 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker/1:0H-events_highpri
23 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs
24 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 netns
25 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_tasks_trace
26 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_tasks_rude_
0[ 0.0%] Tasks: 40, 21 thr, 123 kthr; 1 running
1[|| 3.0% 15 0.10 0.09
Mem[||||||||| 233M/1.90G] Uptime: 01:54:04
Swp[ 0K/2.00G]
[Main] [I/O]
PID USER PRI NI VIRT RES SHR S CPU%-MEM% TIME+ Command
812 root 20 0 485M 3 485M 31288 16376 S 1.0 1.6 0:59.53 /usr/libexec/platform-python -Es /usr
232986 root 20 0 28632 4212 3400 R 1.0 0.2 0:00.18 htop -d 10
1 root 20 0 168M 10420 7940 S 0.0 0.5 0:01.60 /usr/lib/systemd/systemd --switched-r
637 root 20 0 89588 8844 7892 S 0.0 0.4 0:00.19 /usr/lib/systemd/systemd-journald
676 root 20 0 116M 13096 7556 S 0.0 0.7 0:01.28 /usr/lib/systemd/systemd-udevd
712 rpc 20 0 67224 5552 4824 S 0.0 0.3 0:00.06 /usr/bin/rpcbind -w -f
733 root 20 0 86216 11596 9920 S 0.0 0.6 0:00.04 /usr/bin/VGAuthService -s
734 root 20 0 271M 10024 8584 S 0.0 0.5 0:05.38 /usr/bin/vmtoolsd
737 root 20 0 213M 14288 12192 S 0.0 0.7 0:00.14 /usr/sbin/sssd -i --logger=files
738 root 20 0 122M 5452 4776 S 0.0 0.3 0:00.33 /usr/sbin/irqbalance --foreground
739 dbus 20 0 76664 5508 4944 S 0.0 0.3 0:00.18 /usr/bin/dbus-daemon --system --addre
740 root 20 0 382M 18416 16108 S 0.0 0.9 0:00.21 /usr/sbin/NetworkManager --no-daemon
741 polkitd 20 0 1592M 26336 17356 S 0.0 1.3 0:00.12 /usr/lib/polkit-1/polkitd --no-debug
787 chrony 20 0 147M 4772 4060 S 0.0 0.2 0:00.05 /usr/sbin/chronyd
788 root 20 0 122M 5452 4776 S 0.0 0.3 0:00.00 /usr/sbin/irqbalance --foreground
797 root 20 0 271M 10024 8584 S 0.0 0.5 0:00.13 /usr/bin/vmtoolsd
800 root 20 0 382M 18416 16108 S 0.0 0.9 0:00.08 /usr/sbin/NetworkManager --no-daemon
803 polkitd 20 0 1592M 26336 17356 S 0.0 1.3 0:00.00 /usr/lib/polkit-1/polkitd --no-debug
805 polkitd 20 0 1592M 26336 17356 S 0.0 1.3 0:00.02 /usr/lib/polkit-1/polkitd --no-debug
808 root 20 0 382M 18416 16108 S 0.0 0.9 0:00.00 /usr/sbin/NetworkManager --no-daemon
F1Help F2Setup F3SearchF4FilterF5Tree F6SortByF7Nice -F8Nice +F9Kill F10Quit
4、内存空间 free 可以显示内存的使用状态
[00:16:56 root@Centos8 ~]#free -h
total used free shared buff/cache available
Mem: 1.9Gi 232Mi 1.3Gi 8.0Mi 411Mi 1.5Gi
Swap: 2.0Gi 0B 2.0Gi
5、进程对应的内存映射 pmap
[00:20:36 root@Centos8 ~]#pmap 1106
1106: sshd: root@pts/0,pts/1
000055e4dfc27000 824K r-x-- sshd
000055e4dfef4000 16K r---- sshd
000055e4dfef8000 4K rw--- sshd
000055e4dfef9000 16K rw--- [ anon ]
000055e4e1872000 388K rw--- [ anon ]
000055e4e18d3000 344K rw--- [ anon ]
00007f1dda28c000 856K r-x-- libnss_systemd.so.2
00007f1dda362000 2044K ----- libnss_systemd.so.2
00007f1dda561000 20K r---- libnss_systemd.so.2
00007f1dda566000 4K rw--- libnss_systemd.so.2
00007f1dda567000 4K rw--- [ anon ]
00007f1dda568000 11300K r--s- initgroups
00007f1ddb071000 12K r-x-- pam_lastlog.so
00007f1ddb074000 2048K ----- pam_lastlog.so
00007f1ddb274000 4K r---- pam_lastlog.so
6、虚拟内存信息 vmstat
[00:36:32 root@Centos8 ~]#vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
2 0 0 1337272 5300 415872 0 0 20 7 252 251 1 3 96 0 0
[00:36:37 root@Centos8 ~]#vmstat -s
1996812 K total memory
238036 K used memory
234504 K active memory
204508 K inactive memory
1337604 K free memory
5300 K buffer memory
415872 K swap cache
2097148 K total swap
0 K used swap
2097148 K free swap
17080 non-nice user cpu ticks
330 nice user cpu ticks
39649 system cpu ticks
1539177 idle cpu ticks
3539 IO-wait cpu ticks
3087 IRQ cpu ticks
2800 softirq cpu ticks
0 stolen cpu ticks
322597 pages paged in
108378 pages paged out
0 pages swapped in
0 pages swapped out
4043750 interrupts
4033180 CPU context switches
1660141337 boot time
276351 forks
7、统计CPU和设备io信息iostat,可以提供丰富的io性能状态数据,由sysstat包提供。
[21:51:37 root@Centos8 ~]#iostat
Linux 4.18.0-348.el8.x86_64 (Centos8) 08/11/22 _x86_64_ (2 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
1.04 0.00 2.65 1.31 0.00 95.00
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 5.19 160.71 45.00 120425 33718
scd0 0.05 1.39 0.00 1041 0
[22:02:08 root@Centos8 ~]#iostat 1 3
Linux 4.18.0-348.el8.x86_64 (Centos8) 08/11/22 _x86_64_ (2 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
1.02 0.00 2.63 1.25 0.00 95.09
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 4.95 153.02 42.87 120433 33739
scd0 0.05 1.32 0.00 1041 0
avg-cpu: %user %nice %system %iowait %steal %idle
3.02 0.00 5.03 0.00 0.00 91.96
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 0.00 0.00 0.00 0 0
scd0 0.00 0.00 0.00 0 0
avg-cpu: %user %nice %system %iowait %steal %idle
0.00 0.00 1.50 0.00 0.00 98.50
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 0.00 0.00 0.00 0 0
scd0 0.00 0.00 0.00 0 0
[22:02:48 root@Centos8 ~]#iostat -d sda -t -k 1 3
Linux 4.18.0-348.el8.x86_64 (Centos8) 08/11/22 _x86_64_ (2 CPU)
08/11/22 22:07:47
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 3.63 110.73 31.31 120501 34069
08/11/22 22:07:48
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 0.00 0.00 0.00 0 0
08/11/22 22:07:49
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 0.00 0.00 0.00 0 0
[22:07:49 root@Centos8 ~]#iostat -d sda 1 3 -x
Linux 4.18.0-348.el8.x86_64 (Centos8) 08/11/22 _x86_64_ (2 CPU)
Device r/s w/s rkB/s wkB/s rrqm/s wrqm/s %rrqm %wrqm r_await w_await aqu-sz rareq-sz wareq-sz svctm %util
sda 3.11 0.28 103.49 29.28 0.02 0.04 0.71 11.32 19.61 1.22 0.06 33.25 103.62 5.22 1.77
Device r/s w/s rkB/s wkB/s rrqm/s wrqm/s %rrqm %wrqm r_await w_await aqu-sz rareq-sz wareq-sz svctm %util
sda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Device r/s w/s rkB/s wkB/s rrqm/s wrqm/s %rrqm %wrqm r_await w_await aqu-sz rareq-sz wareq-sz svctm %util
sda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
8、监视磁盘i/o iotop 由iotop包提供
Total DISK READ : 0.00 B/s | Total DISK WRITE : 0.00 B/s
Actual DISK READ: 0.00 B/s | Actual DISK WRITE: 0.00 B/s
TID PRIO USER DISK READ DISK WRITE> COMMAND
1 be/4 root 0.00 B/s 0.00 B/s systemd --switched-root --system --deserialize 18
2 be/4 root 0.00 B/s 0.00 B/s [kthreadd]
3 be/0 root 0.00 B/s 0.00 B/s [rcu_gp]
4 be/0 root 0.00 B/s 0.00 B/s [rcu_par_gp]
6 be/0 root 0.00 B/s 0.00 B/s [kworker/0:0H-events_highpri]
9 be/0 root 0.00 B/s 0.00 B/s [mm_percpu_wq]
10 be/4 root 0.00 B/s 0.00 B/s [ksoftirqd/0]
11 be/4 root 0.00 B/s 0.00 B/s [rcu_sched]
12 rt/4 root 0.00 B/s 0.00 B/s [migration/0]
13 rt/4 root 0.00 B/s 0.00 B/s [watchdog/0]
14 be/4 root 0.00 B/s 0.00 B/s [cpuhp/0]
15 be/4 root 0.00 B/s 0.00 B/s [cpuhp/1]
16 rt/4 root 0.00 B/s 0.00 B/s [watchdog/1]
17 rt/4 root 0.00 B/s 0.00 B/s [migration/1]
18 be/4 root 0.00 B/s 0.00 B/s [ksoftirqd/1]
20 be/0 root 0.00 B/s 0.00 B/s [kworker/1:0H-events_highpri]
22 be/4 root 0.00 B/s 0.00 B/s [kworker/u256:1-events_unbound]
23 be/4 root 0.00 B/s 0.00 B/s [kdevtmpfs]
24 be/0 root 0.00 B/s 0.00 B/s [netns]
25 be/4 root 0.00 B/s 0.00 B/s [rcu_tasks_trace]
26 be/4 root 0.00 B/s 0.00 B/s [rcu_tasks_rude_]
27 be/4 root 0.00 B/s 0.00 B/s [kauditd]
29 be/4 root 0.00 B/s 0.00 B/s [khungtaskd]
30 be/4 root 0.00 B/s 0.00 B/s [oom_reaper]
31 be/0 root 0.00 B/s 0.00 B/s [writeback]
32 be/4 root 0.00 B/s 0.00 B/s [kcompactd0]
9、显示网络带宽使用情况 iftop 由epel提供包
[22:24:21 root@Centos8 ~]#iftop -ni eth0
interface: eth0
IP address is: 10.0.0.8
MAC address is: 00:0c:29:d0:74:54
12.5Kb 25.0Kb 37.5Kb 50.0Kb 62.5Kb
└─┴─┴─┴─┴──
10.0.0.8 => 10.0.0.1 => 10.0.0.1 17.2Kb 43.5Kb 36.5Kb
<= 6.29Kb 10.3Kb 8.63Kb
10.0.0.8 => 192.168.100.32 448b 448b 411b
<= 736b 736b 675b
──
TX: cum: 15.5KB pe55.4 60.2Kb 3.5Kb rates: 17.7Kb 43.9Kb 36.9Kb
RX: 13.9KB 14.3Kb 7.01Kb 11.0Kb 9.29Kb
TOTAL: 69.3KB 76.5Kb 24.7Kb 54.9Kb 46.2Kb
10、查看网络实时吞吐量 nload,由epel源提供包
[22:28:31 root@Centos8 ~]#nload
Device eth0 [10.0.0.8] (1/2):
==
Incoming:
Curr: 26.21 kBit/s
Avg: 15.66 kBit/s
Min: 8.34 kBit/s
Max: 33.50 kBit/s
Ttl: 4.00 MByte
Outgoing:
Curr: 101.52 kBit/s
Avg: 55.89 kBit/s
Min: 14.95 kBit/s
Max: 210.49 kBit/s
Ttl: 13.01 MByte
11、网络监视工具 iptraf-ng,对终端窗口大小有要求,太小无法展示。
[22:30:52 root@Centos8 ~]#iptraf-ng
iptraf-ng 1.2.1
┌─┐
│ IP traffic monitor │
│ General interface statistics │
│ Detailed interface statistics │
│ Statistical breakdowns... │
│ LAN station monitor │
│─ │
│ Filters... │
│─ │
│ Configure... │
│─ │
│ About... │
│─ │
│ Exit │
└─┘
Displays current IP traffic information
Up/Down-Move selector Enter-execute
iptraf-ng 1.2.1
┌ TCP Connections (Source Host:Port) ─ Packets ────── Bytes ─ Flag ─ Iface ─────┐
│┌10.0.0.8:22 > 10 794 1336139600 -Pth0 │
│└10.0.0.1:12371 > 6 614 276 35670 --h0 │
│┌10.0.0.1:12373 > 1 4 124 -340 --h0 │
│└10.0.0.8:22 > 1 762 -PA- 1520 │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└ TCP: 2 entries ─┘ Active
┌─┐
│ ICMP echo rply (28 bytes) from 10.0.0.8 to 192.168.100.32 on eth0 │
│ ICMP echo req (46 bytes) from 192.168.100.32 to 10.0.0.8 on eth0 │
│ ICMP echo rply (28 bytes) from 10.0.0.8 to 192.168.100.32 on eth0 │
│ ICMP echo req (46 bytes) from 192.168.100.32 to 10.0.0.8 on eth0 │
│ ICMP echo rply (28 bytes) from 10.0.0.8 to 192.168.100.32 on eth0 │
│ ICMP echo req (46 bytes) from 192.168.100.32 to 10.0.0.8 on eth0 │
│ ICMP echo rply (28 bytes) from 10.0.0.8 to 192.168.100.32 on eth0 │
└ Bottom Time: 0:00 │ Abou Drops: 0 │
Packets captured: 0 1488 TCP flow rate: 38.37 kbps
Up/Dn/PgUp/PgDn-scroll M-more TCP info W-chg actv win S-sort TCP X-exit
12、系统资源统计 dstat,由pcp-system-tools包提供,但安装dstat包即可,可用于替代vmstat,iostat的功能。
[22:40:29 root@Centos8 ~]#dstat 1 6
You did not select any stats, using -cdngy by default.
----total-usage---- -dsk/total- -net/total- ---paging-- ---system--
usr sys idl wai stl| read writ| recv send| in out | int csw
5 7 87 0 0| 0 0 |3416B 16k| 0 0 | 921 767
0 1 98 0 0| 0 0 |1350B 2431B| 0 0 | 653 579
0 1 98 0 0| 0 0 |1145B 2157B| 0 0 | 370 411
1 3 95 0 0| 0 0 |2927B 15k| 0 0 | 624 618
2 4 85 12 0| 0 55M| 448B 1103B| 0 0 | 454 698
0 0 98 0 0| 0 0 |1207B 2366B| 0 0 | 362 419
13、综合监控工具 glances,由epel源提供包,centos7和8不能相互监控使用。
[22:43:38 root@Centos8 ~]#glances
Centos8 (CentOS Linux 8.5.2111 64bit / Linux 4.18.0-348.el8.x86_64) Uptime: 0:54:58
CPU - 4.0% MEM - 22.0% SWAP - 0.0% LOAD 2-core
CPU [ 4.0%] user 1.5% total 1.90G total 2.00G 1 min: 0.00
MEM [ 22.0%] system 2.4% used 430M used 0 5 min: 0.04
SWAP [ 0.0%] iowait 0.0% free 1.48G free 2.00G 15 min: 0.07
NETWORK Rx/s Tx/s TASKS 172 (193 thr), 1 run, 106 slp, 65 oth Threads sorted automatically
eth0 17Kb 62Kb
lo 0b 0b CPU% MEM% VIRT RES PID USER TIME+ THR NI S R/s W/s
>2.9 1.6 212M 32.0M 111232 root 0:01 1 0 R 0 0 pyt
FILE SYS Used Total 0.5 1.7 485M 32.5M 857 root 0:25 5 0 S 0 0 pla
/ (sda1) 3.12G 100.0G 0.5 0.4 150M 8.40M 1117 root 0:18 1 0 S 0 0 ssh
/data 389M 50.0G 0.0 2.0 224M 39.8M 868 root 0:00 1 0 S 0 0 sss
0.0 1.2 1.55G 23.4M 721 polkitd 0:00 6 0 S 0 0 pol
0.0 0.9 383M 17.7M 726 root 0:00 3 0 S 0 0 Net
0.0 0.8 222M 15.1M 859 root 0:00 1 0 S 0 0 sss
0.0 0.7 214M 13.9M 724 root 0:00 1 0 S 0 0 sss
0.0 0.7 116M 13.0M 661 root 0:01 1 0 S 0 0 sys
0.0 0.6 84.2M 11.1M 719 root 0:00 1 0 S 0 0 VGA
0.0 0.5 169M 10.6M 1 root 0:01 1 0 S 0 0 sys
0.0 0.5 272M 9.91M 720 root 0:02 3 0 S 0 0 vmt
0.0 0.5 87.3M 9.09M 1109 root 0:00 1 0 S 0 0 sys
0.0 0.4 87.5M 8.76M 623 root 0:00 1 0 S 0 0 sys
0.0 0.4 136M 8.59M 1105 root 0:00 1 0 S 0 0 ssh
0.0 0.4 136M 8.45M 1135 root 0:00 1 0 S 0 0 ssh
0.0 0.4 213M 8.41M 902 root 0:00 3 0 S 0 0 rsy
2022-08-11 22:44:37 CST
14、查看进程打开文件 lsof,list open files
[22:46:38 root@Centos8 ~]#lsof |head
COMMAND PID TID TASKCMD USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 root cwd DIR 8,1 256 128 /
systemd 1 root rtd DIR 8,1 256 128 /
systemd 1 root txt REG 8,1 1605456 365812 /usr/lib/systemd/systemd
systemd 1 root mem REG 8,1 1599008 67366706 /usr/lib64/libm-2.28.so
systemd 1 root mem REG 8,1 636776 67567015 /usr/lib64/libudev.so.1.6.11
systemd 1 root mem REG 8,1 735192 67416325 /usr/lib64/libsepol.so.1
systemd 1 root mem REG 8,1 1805368 67416468 /usr/lib64/libunistring.so.2.1.0
systemd 1 root mem REG 8,1 145984 67416344 /usr/lib64/libgpg-error.so.0.24.2
systemd 1 root mem REG 8,1 66784 67416602 /usr/lib64/libjson-c.so.4.0.0
[22:46:46 root@Centos8 ~]#lsof /var/log/messages
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsyslogd 902 root 5w REG 8,1 1474875 672838 /var/log/messages
5、编写脚本,使用for和while分别实现10.0.0.0/24网段内,地址是否能ping通,若能ping通则输出“success!”,若不能ping通则输出“fail!”
#for循环测试ip状态
[23:59:46 root@Centos8 ~]#vim forscanhost.sh
[00:03:53 root@Centos8 ~]#cat forscanhost.sh
#!/bin/bash
# /#############################################################/
# Author: liuqixin
# Email: 450131xxx@qq.com
# Time: 2022-08-08 23:48:17
# Name: forscanhost.sh
# Version: The test script V1.0
# Description:
# /#############################################################/
NET=10.0.0
#测试10.0.0.1-50的ip状态,ping -c 1 设置发送1个包,-w 1 指定超时时间1秒。
#能ping通的保存到/data/hostlist文件。
for ID in {1..50};do
{
ping -c1 -w1 $NET.$ID &> /dev/null && echo $NET.$ID is success! |tee -a /data/hostlist.log || echo $NET.$ID is fail!
}&
done
wait
[00:03:58 root@Centos8 ~]#bash forscanhost.sh
10.0.0.1 is success!
10.0.0.8 is success!
10.0.0.7 is success!
10.0.0.2 is success!
10.0.0.17 is success!
10.0.0.27 is success!
10.0.0.37 is success!
10.0.0.4 is fail!
10.0.0.5 is fail!
10.0.0.3 is fail!
10.0.0.16 is fail!
#while循环测试
[00:36:40 root@Centos8 ~]#vim whilescanhost.sh
[00:37:02 root@Centos8 ~]#cat whilescanhost.sh
#!/bin/bash
# /#############################################################/
# Author: liuqixin
# Email: 450131xxx@qq.com
# Time: 2022-08-09 00:05:46
# Name: whilescanhost.sh
# Version: The test script V1.0
# Description:
# /#############################################################/
NET=10.0.0
#测试10.0.0.1-50的ip状态,ping -c 1 设置发送1个包,-w 1 指定超时时间1秒。
#能ping通的保存到/data/hostlist文件。
ID=0
while ((ID<=50));do
echo $ID &> /dev/null
ID=$(($ID+1))
{
ping -c1 -w1 $NET.$ID &> /dev/null && echo $NET.$ID is success! |tee -a /data/hostlist.log || echo $NET.$ID is fail!
}&
done
wait
[00:37:07 root@Centos8 ~]#bash whilescanhost.sh
10.0.0.1 is success!
10.0.0.8 is success!
10.0.0.37 is success!
10.0.0.7 is success!
10.0.0.17 is success!
10.0.0.2 is success!
10.0.0.27 is success!
10.0.0.15 is fail!
10.0.0.4 is fail!
10.0.0.5 is fail!
10.0.0.13 is fail!
10.0.0.10 is fail!
10.0.0.3 is fail!
6、每周的工作日1.30,将/etc备份至/backup目录中,保存的文件格式为:“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间。
1、未来的某个时间点执行一次任务:
at:指定时间点,执行一次性任务
batch:系统自行选择空闲时间去执行此处指定的任务
2、周期性运行某个任务:
cron
2.1、系统cron计划任务:
[23:10:21 root@Centos8 ~]#cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
2.2、anacron
2.3、管理临时文件:
2.4、用户计划任务:
#编写脚本
[00:32:26 root@Centos8 ~]#vim etcbak.sh
[00:50:21 root@Centos8 ~]#cat etcbak.sh
#!/bin/bash
# /#############################################################/
# Author: liuqixin
# Email: 450131xxx@qq.com
# Time: 2022-08-12 00:29:36
# Name: etcbak.sh
# Version: The test script V1.0
# Description:
# /#############################################################/
mkdir /data/backup
tar cvfJ /data/backup/etcbak-`date -d '-1 day' +%F-%H`.tar.xz /etc/
[00:52:55 root@Centos8 ~]#crontab -e
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
30 1 * * 0 /root/etcbak.sh