N63044 - week 6

Advanced shell script
1. ubuntu network configuration (61 minutes)
2. shell script programming purchase price while loop and control (46 minutes)
3. shell script advanced shift and select(49 minutes)
4. shell script advanced function implementation (57 minutes)
5. shell script advanced function recursion and signal capture (61 minutes)
6. shell script advanced common tools (44 minutes)
7. shell script advanced array (50 minutes)

Process and scheduled tasks
1. shell Scripting advanced string slicing and advanced variables (60 minutes)
2. Linux Process and memory management (57 minutes)
3. Process status and related concepts (41 minutes)
4. Process management tool (67 minutes)
5. Performance related tools (50 minutes)
6. Pre and post execution and concurrent execution of processes (48 minutes)
7. Task plan management (69 minutes)

1. Write a script to log in to the remote host. (use expect and shell script).

[root@centos8 ~]# cat /data/scripts/expect.sh
#!/bin/bash
#********************************************************************
#Author:            linxiaodong
#QQ:                916794060
#Date:              2022-01-27
#FileName:          /data/scripts/expect.sh
#Description:       The test script
#Copyright (C):     2022 All rights reserved
#********************************************************************
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 eof
EOF

[root@centos8 ~]# bash /data/scripts/expect.sh 10.0.0.7 root 123456
spawn ssh root@10.0.0.7
root@10.0.0.7's password: 
Last login: Thu Jan 27 18:07:57 2022 from 10.0.0.8
[root@centos7 ~]# 

2. Generate 10 random numbers, save them in the array, and find out their maximum and minimum values

[root@centos8 ~]# cat max_min.sh
#!/bin/bash
#********************************************************************
#Author:            linxiaodong
#QQ:                916794060
#Date:              2022-01-27
#FileName:          max_min.sh
#Description:       The test script
#Copyright (C):     2022 All rights reserved
#********************************************************************
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@centos8 ~]# bash max_min.sh
All numbers are 28459 4665 25970 11635 30186 2211 17681 9013 14109 27165
Max is 30186
Min is 2211

3. Input several values into the array, and use the bubbling algorithm to sort in ascending or descending order

[root@centos8 scripts]# cat maopao.sh 
#!/bin/bash
#********************************************************************
#Author:            linxiaodong
#QQ:                916794060
#Date:              2022-01-28
#FileName:          maopao.sh
#Description:       The test script
#Copyright (C):     2022 All rights reserved
#********************************************************************
arr=( 11 3 91 47 36 58 98 2 88 76 9 )
len=${#arr[*]}
echo "The original array is:" ${arr[*]}
for ((j=0;j<$len;j++));do
for ((i=0;i<$len-1;i++));do
        if [ ${arr[$i]} -gt ${arr[$i+1]} ];then
                x=${arr[$i]}
                arr[$i]=${arr[$i+1]}
                arr[$i+1]=$x              
        fi
done
done
echo "The sorted array is:" ${arr[*]}

[root@centos8 scripts]# bash maopao.sh 
The original array is: 11 3 91 47 36 58 98 2 88 76 9
 The sorted array is: 2 3 9 11 36 47 58 76 88 91 98

4. Summarize several commands for viewing the system load, and summarize the general meaning of the indicators of the top command (it is not required to write them all)

4.1 load query uptime

/proc/uptime includes two values, in s

  • System startup time
  • Total duration of idle processes (calculated by total CPU cores)

uptime and w display the following

  • current time
  • Time when the system has started
  • Current online population
  • Average load of the system (the average load of 1, 5 and 15 minutes will generally not exceed 1, and an alarm is recommended when it exceeds 5)

System average load: refers to the average number of processes in the running queue within a specific time interval. Usually, the current number of active processes in each CPU core is small
At 3, the performance of the system is good. If the number of tasks per CPU core is greater than 5, the performance of this host has serious problems

For example, the linux host is a dual core CPU. When the Load Average is 6, the machine has been fully used

example:

[root@centos8 ~]# uptime
 21:19:15 up 1 day, 30 min,  1 user,  load average: 0.03, 0.03, 0.00

[root@centos8 ~]# w
 21:19:45 up 1 day, 31 min,  1 user,  load average: 0.01, 0.02, 0.00
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    10.0.0.1         18:21    0.00s  0.21s  0.00s w

4.2 display CPU related statistics mpstat

#Install sysstat package
[root@centos8 ~]# yum install -y sysstat

[root@centos8 ~]# mpstat
Linux 4.18.0-240.el8.x86_64 (centos8.magedu.org) 	01/27/2022 	_x86_64_	(2 CPU)

09:24:24 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
09:24:24 PM  all    0.05    0.01    0.18    0.00    0.13    0.11    0.00    0.00    0.00   99.53

#View the load within 3 seconds
[root@centos8 ~]# mpstat 1 3
Linux 4.18.0-240.el8.x86_64 (centos8.magedu.org) 	01/27/2022 	_x86_64_	(2 CPU)

09:29:44 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
09:29:45 PM  all    0.00    0.00    0.50    0.00    0.00    0.00    0.00    0.00    0.00   99.50
09:29:46 PM  all    0.00    0.00    0.00    0.00    0.50    0.00    0.00    0.00    0.00   99.50
09:29:47 PM  all    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
Average:     all    0.00    0.00    0.17    0.00    0.17    0.00    0.00    0.00    0.00   99.67

4.3 viewing process real-time status top and htop

4.3.1 top

top provides dynamic real-time process status

There are many built-in commands

help: h Or, Press q or esc Exit help
 Sort:
P: To occupy CPU percentage,%CPU
M: Percentage of memory occupied,%MEM
T: Cumulative occupation CPU duration,TIME+

Header information display:
uptime Information: l command
tasks and cpu Information: t command
cpu Display separately: 1 (number)
memory Information: m command

Exit command: q
 Modify refresh interval: s
 Terminate the specified process: k
 Save file: W

Introduction to top command field information

us: User space
sy: Kernel space
ni: adjustment nice time
id: free
wa: wait for IO time
hi: Hard interrupt
si: Soft interrupt (mode switching)
st: Virtual machine stolen time

top option:

-d # 	Specifies the refresh interval, which is 3 seconds by default
-b 		Show all all processes
-n # 	How many times to refresh and exit
-H  	Thread mode

4.3.2 htop

The htop command is an enhanced version of the top command, which comes from the EPEL source and is more powerful than top

#Install epel source and htop package in advance
[root@centos8 ~]# yum install -y epel-release
[root@centos8 ~]# yum install -y htop


Options:

-d #: specify the delay time;
-u UserName: Show only the processes of the specified user
-s COLUME: Sort by specified field

Subcommand:

s: Tracks system calls for the selected process
l: Displays a list of files opened by the selected process
a: Bind the selected process to a specified CPU core
t: Show process tree

5. Write a script and use for and while to realize whether the address can be pinged in the 192.168.0.0/24 network segment respectively. If so, it will output "success!", If ping fails, output "fail!"

[root@centos8 ~]# cat /data/scripts/for_scan_host.sh
#!/bin/bash
#********************************************************************
#Author:            linxiaodong
#QQ:                916794060
#Date:              2022-01-27
#FileName:          /data/scripts/for_scan_host.sh
#Description:       The test script
#Copyright (C):     2022 All rights reserved
#********************************************************************
NET=10.0.0
for ID in {1..254};do
    {
        ping -c1 -W1 $NET.$ID &> /dev/null && echo $NET.$ID is "success!" || echo $NET.$ID is "fail!"
    }&
done
wait
[root@centos8 ~]# bash /data/scripts/for_scan_host.sh
10.0.0.1 is success!
10.0.0.6 is success!
10.0.0.2 is success!
10.0.0.8 is success!
10.0.0.7 is success!
10.0.0.100 is success!
10.0.0.175 is success!
10.0.0.3 is fail!
10.0.0.20 is fail!
10.0.0.21 is fail!

.

6. At 1:30 every working day, back up / etc to the / backup directory. The file name format is "etcbak-yyyy-mm-dd-HH.tar.xz", where the date is the time of the previous day

#Create scheduled task
[root@centos8 ~]# crontab -e
30 1 * * 1-5 /bin/cp -a /etc /data/backup/etcbak-`date -d "-1 day" +\%F-\%T.tar.xz`

#Show scheduled tasks
[root@centos8 ~]# crontab -l
30 1 * * 1-5 /bin/cp -a /etc /data/backup/etcbak-`date -d "-1 day" +\%F-\%T.tar.xz` 

#View Backup folders
[root@centos8 backup]# ll -d /data/backup/etcbak-2022-01-27-15:35:01.tar.xz
drwxr-xr-x. 80 root root 8192 Jan 28 14:37 /data/backup/etcbak-2022-01-27-15:35:01.tar.xz

Keywords: Linux Ubuntu bash

Added by centered effect on Sat, 29 Jan 2022 18:52:48 +0200