1. Write a script to log in to the remote host. (use expect and shell script).
[root@centos8-hkping ~]#yum install expect -y
[root@centos8-hkping ~]#rpm -qa expect
expect-5.45.4-5.el8.x86_64
#use expect form
[root@centos8-hkping data]#vim expect_login
#!/usr/bin/expect
set ip 10.0.0.161
set user root
set password 123456
set timeout 10
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
interact
[root@centos8-hkping data]#chmod +x expect_login
[root@centos8-hkping data]#./expect_login
spawn ssh root@10.0.0.161
root@10.0.0.161's password:
Last login: Tue Jan 25 22:20:59 2022 from 10.0.0.150
[root@centos7 ~]##use shell form
[root@centos8-hkping data]#vim shell_login.sh
#!/bin/bash
ip=10.0.0.161
user=root
password=123456
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-hkping data]#chmod +x shell_login.sh
[root@centos8-hkping data]#./shell_login.sh
spawn ssh root@10.0.0.161
root@10.0.0.161's password:
Last login: Tue Jan 25 22:32:48 2022 from 10.0.0.150
[root@centos7 ~]#
2. Generate 10 random numbers, save them in the array, and find out their maximum and minimum values
[root@centos8-hkping data]#vim min_max.sh
#!/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@centos8-hkping data]#sh min_max.sh
All numbers are 18206 10323 31566 18073 289 11359 15395 10981 21730 30847
Max is 31566
Min is 289
3. Input several values into the array, and use the bubbling algorithm to sort in ascending or descending order
[root@centos7 hkping]# vim paixu.sh #!/bin/bash read -p "Please enter a positive integer number,Separated by spaces:" 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[@]}
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)
The w command can output the system load
The output in the first line shows the current system time, the running time of the system FROM Startup to now, the number of users logged in to the system and the average load of the system. Average load refers to the load condition of the system within 1min, 5min and 15min· USER: indicates the USER who logs in to the system· TTY: indicates the TTY name used by the USER· FROM: indicates where the USER logs in FROM. Generally, the IP address or host name of the remote LOGIN host is displayed· LOGIN @: the date and time the USER logged in· IDLE: displays the IDLE time of the terminal· JCPU: indicates the total time that all processes and sub processes on the terminal use the system· PCPU: the system time used by the current active process· WHAT: the name and options of the process executed by the current USER.
[root@centos7 hkping]# w 22:23:46 up 1:09, 1 user, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 10.0.0.1 21:15 2.00s 0.11s 0.00s w
The uptime command can output the current system time, the running time from system startup to now, how many users are online and the average load of the system.
[root@centos7 hkping]# uptime 22:29:51 up 1:15, 1 user, load average: 0.00, 0.01, 0.05
top - 22:30:11 up 1:16, 1 user, load average: 0.00, 0.01, 0.05 Tasks: 127 total, 2 running, 125 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.0 us, 1.6 sy, 0.0 ni, 98.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 995776 total, 601376 free, 133484 used, 260916 buff/cache KiB Swap: 2097148 total, 2097148 free, 0 used. 716064 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2015 root 20 0 162140 2188 1544 R 6.7 0.2 0:00.01 top 1 root 20 0 125856 4384 2588 S 0.0 0.4 0:02.96 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd 4 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H 5 root 20 0 0 0 0 S 0.0 0.0 0:00.08 kworker/u25+ 6 root 20 0 0 0 0 S 0.0 0.0 0:00.17 ksoftirqd/0 7 root rt 0 0 0 0 S 0.0 0.0 0:09.76 migration/0 8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh 9 root 20 0 0 0 0 R 0.0 0.0 0:03.16 rcu_sched 10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 lru-add-dra+ 11 root rt 0 0 0 0 S 0.0 0.0 0:00.16 watchdog/0 12 root rt 0 0 0 0 S 0.0 0.0 0:00.05 watchdog/1 13 root rt 0 0 0 0 S 0.0 0.0 0:09.65 migration/1 14 root 20 0 0 0 0 S 0.0 0.0 0:00.25 ksoftirqd/1 15 root 20 0 0 0 0 S 0.0 0.0 0:01.14 kworker/1:0 16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/1:0H 17 root rt 0 0 0 0 S 0.0 0.0 0:00.02 watchdog/2
The first line, task queue information, is the same as the execution result of uptime command· 22:10:02 represents the current system time· Up 55 min means that the system has been running for 122 minutes· 1 user means that one user is currently logged into the system· Load average: 0.00, 0.01 and 0.05. The following three numbers of load average are the average load of 1 minute, 5 minutes and 15 minutes respectively.
In the second line, Tasks are Tasks (processes). It can be seen from the above information that the system now has 128 processes, including 1 in running state, 127 in sleep, 0 in stopped state and 0 in zombie state.
The third line is CPU status information· Percentage of CPU occupied by 0.0%us user space· Percentage of CPU occupied by 0.0%sy kernel space· 0.0%ni the percentage of CPU used by processes that have changed their priority· 100.0%id percentage of idle CPU· Percentage of CPU consumed by 0.0%wa I/O wait· Percentage of CPU occupied by 0.0%hi Hardware IRQ· Percentage of CPU occupied by 0.0%si Software Interrupts· Percentage of CPU consumed by 0.0%st virtual machine.
The fourth line, memory status· Total total physical memory. Free total free memory· used the total amount of memory in use·· buffers/cache the amount of memory buffered.
The fifth line is swap to exchange partition information· Total total exchange area. Total free swap area· Total number of swap areas used·· Total available memory of available mem.
Line 6, blank line
The seventh line gives the status monitoring of each process (task)· PID process id· USER process owner· PR process priority· NI nice value, negative value indicates high priority and positive value indicates low priority· The total amount of virtual memory used by the VIRT process, in kb· The size of physical memory used by RES process and not swapped out, in kb· SHR shared memory size in kb. S process status. D = non interruptible sleep state R = running s = sleep T = tracking / stopping Z = zombie process. ·% Percentage of CPU TIME consumed since the last CPU update. ·% The percentage of physical memory used by the MEM process· Total CPU TIME used by TIME + process, unit: 1 / 100 second· COMMAND process name (COMMAND name / COMMAND line).
(1) Press s: the default is to refresh once every 3s. Press s to modify the refresh time.
(2) Press Space key: refresh immediately.
(3) Press q key: exit.
(4) Press P Key: sort by CPU utilization in descending order.
(5) Press the M key: sort by memory.
(6) Press T key: sort by time.
(7) Press the p Key: process IP to view the status of a process.
(8) Press the number 1 key: display the CPU utilization of each kernel.
(9) Press u/U key: specify the displayed user.
(10) Press h key: help.
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@centos7 hkping]# vim shell_ping.sh #!/bin/bash net=192.168.0 declare -i address=1 while [ $address -le 254 ];do { ping -c1 -w1 ${net}.${address} &>/dev/null if [ $? -eq 0 ];then echo $net.$address is success! else echo $net.$address is fail! fi }& let address++ done wait
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
[root@centos7 hkping] vim /home/hkping/backup_etc.sh #!/bin/bash DAY=$(date +"%F-%H" -d "-1 days" ) if [ ! -d /backup ]; then rm -rf /backup mkdir -p /backup fi tar -zcvf /backup/etcbak-`date +"%F-%H" -d "-1 days"`.tar.xz /etc [root@centos7 hkping]# chmod +x backup_etc.sh [root@centos7 hkping]# crontab -e 30 1 * * 1-5 /home/hkping/backup_etc.sh &>/dev/null
[root@centos7 hkping]# vim shell_ping.sh