1、编写脚本实现登陆远程主机。(使用expect和shell脚本两种形式)。

expect脚本:①安装expect

root@ubuntu:~# apt  install expect -y
root@ubuntu:~# dpkg -l expect
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-====================================================-===============================-===============================-==============================================================================================================
ii expect 5.45.4-1 amd64 Automates interactive applications

②使用expect写脚本并运行

root@ubuntu:~# cat remotelogin_expect.sh
#!/usr/bin/expect
spawn ssh root@192.168.112.129
expect {
"*(yes/no*" { send "yes\n";exp_continue }
"*password:*" { send "root\n";exp_continue }
"*]#" { send "hostname -I\n" }
}
interact
root@ubuntu:~# ./remotelogin_expect.sh
spawn ssh root@192.168.112.129
root@192.168.112.129's password:
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Tue Dec 21 16:23:16 2021 from 192.168.112.130
[root@Centos8 ~]#hostname -I
192.168.112.129
想要退出运行一下exit命令就好了,那样就可以回到那个主机了
[root@Centos8 ~]#exit
logout
Connection to 192.168.112.129 closed.

shell脚本

root@ubuntu:~# cat remotelogin.sh 
#!/bin/bash
IP=192.168.112.129
USER=root
PASSWD=root
expect << EOF
spawn ssh $USER@$IP
expect {
"*(yes/no*" { send "yes\n";exp_continue }
"*password:*" { send "$PASSWD\n";exp_continue }
"*]#" { send "hostname -I\n" }
}
expect eof;
执行写好的脚本remotelogin.sh
root@ubuntu:~# ./remotelogin.sh
./remotelogin.sh: line 13: warning: here-document at line 6 delimited by end-of-file (wanted `EOF')
spawn ssh root@192.168.112.129
root@192.168.112.129's password:
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Tue Dec 21 16:46:06 2021 from 192.168.112.130
[root@Centos8 ~]#hostname -I
192.168.112.129
2、生成10个随机数保存于数组中,并找出其最大值和最小值
root@ubuntu:~# cat max_min.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[$i]} && max=${nums[$i]}&& continue
[ ${nums[$i]} -gt $max ] && max=${nums[$i]}
[ ${nums[$i]} -lt $min ] && min=${nums[$i]}
done
echo “All numbers are ${nums[*]}”
echo Max is $max
echo Min is $min
root@ubuntu:~# bash max_min.sh
“All numbers are 31226 5817 24046 15179 27696 24069 15955 24371 15002 1984”
Max is 31226
Min is 1984
root@ubuntu:~# bash max_min.sh
“All numbers are 7302 11216 31002 17231 8879 10572 27876 5576 15367 1989”
Max is 31002
Min is 1989
3、输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序
升序:
root@ubuntu:~# cat bubble.sh
#!/bin/bash
arr=( 14 5 78 92 2 99 45 68 )
len=${#arr[*]}
echo "原始数组是:" ${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 "数组从小到大的顺序是:" ${arr[*]}
root@ubuntu:~# bash bubble.sh 运行的结果
原始数组是: 14 5 78 92 2 99 45 68
数组从小到大的顺序是: 2 5 14 45 68 78 92 99

降序:
root@ubuntu:~# cat bubble.sh
#!/bin/bash
arr=( 14 5 78 92 2 99 45 68 )
len=${#arr[*]}
echo "原始数组是:" ${arr[*]}
for ((j=0;j<$len;j++));do
for ((i=0;i<$len-1;i++));do
if [ ${arr[$i]} -lt ${arr[$i+1]} ];then
x=${arr[$i]}
arr[$i]=${arr[$i+1]}
arr[$i+1]=$x
fi
done
done
echo "数组从小到大的顺序是:" ${arr[*]}
root@ubuntu:~# bash bubble.sh
原始数组是: 14 5 78 92 2 99 45 68
数组从小到大的顺序是: 99 92 78 68 45 14 5 2
4、总结查看系统负载的几种命令,总结top命令的指标大概什么含义(不要求全部写出来)

①uptime 一次性输出当前系统负载情况

root@ubuntu:~# uptime
19:52:26 up 4:04, 2 users, load average: 0.01, 0.01, 0.00

②mpstat 根据参数返回系统负载情况,这个需要安装sysstat包

root@ubuntu:~# apt install sysstat -y
root@ubuntu:~# mpstat
Linux 4.15.0-163-generic (ubuntu) 12/21/2021 _x86_64_ (1 CPU)
07:54:58 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
07:54:58 PM all 0.10 0.04 0.13 0.11 0.00 0.00 0.00 0.00 0.00 99.61

③top动态显示系统负载情况

shell进阶、进程和计划任务_bash

④w命令也可以查看系统负载情况

root@ubuntu:~# w
19:57:03 up 4:09, 2 users, load average: 0.01, 0.01, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.112.1 19:20 20:15 0.01s 0.01s -bash
root pts/1 192.168.112.1 19:31 2.00s 0.01s 0.00s w

top命令的指标

top - 19:59:44 up  4:12,  2 users,  load average: 0.00, 0.00, 0.00
`当前时间` `开机时间` `当前登录用户数` `平均负载,5分钟、10分钟、15分钟`
Tasks: 175 total,   1 running,  98 sleeping,   0 stopped,   0 zombie
`总任务数` `当前正在运行的任务数` `处于休眠的任务数` `停止状态的任务数` `僵尸态的进程数`
%Cpu(s):  0.0 us,  0.3 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
上一次刷新后间隔内CPU状态的百分比情况;是多核CPU的平均值,可以按1切换显示为独立的CPU统计;
us:用户空间内没有nice调整过优先级的进程运行的时间
sy:内核空间进程运行的时间
ni:调整过nice的用户空间进程运行时间
id:空闲时间
wa:等待IO的时间
hi,硬中断时间
si,软中断(模式切换)
st,虚拟机偷走的时间(虚拟机内进程执行的时间)
KiB Mem :  2017408 total,  1156012 free,   300244 used,   561152 buff/cache
内存和交换分区的使用情况:
total:总大小
free:空闲大小,加上buff/cache后是真正可以使用的总空闲空间
used:已使用的大小
buff/cache:数据缓存占用的大小
PID  %MEM    VIRT    RES   CODE    DATA    SHR nMaj nDRT  %CPU COMMAND

PID:进程ID
%MEM:内存使用百分比
VIRT:使用的虚拟内存大小,包括已经被映射但还未被使用的页表
RES:使用的物理内存大小
CODE:可用于执行代码的物理内存数量
DATA:进程保留的私有内存空间,可能还未被映射到物理内存,但已经被算到虚拟内存空间中
SHR:共享内存空间,可以被其他进程共享
PR:top命令显示的优先级0-39之间,越小
NI:nice优先级
%CPU:CPU利用率
TIME+:进程启动之后占用的CPU时间,是时间片的累计
COMMAND:进程关联程序的名称,或启动进程的命令
5、编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"

for脚本:

root@ubuntu:~# cat for_ping.sh 
#!/bin/bash
#
NET=192.168.0.
for ID in {1..254};do
{
if ping -c1 -w1 $NET$ID > /dev/null;then
echo "$NET$ID lived,ping test Success"
else
echo "$NET$ID lived,ping test Fail"
fi
} &
done
wait

while脚本:

root@ubuntu:~# cat while_ping.sh 
#!/bin/bash
#
NET=192.168.0.
declare -i ID=1
while [ $ID -le 254 ];do
{
ping -c1 -w1 $NET$ID > /dev/null
if [ $? -eq 0 ];then
echo echo "$NET$ID lived,ping test Success"
else
echo "$NET$ID not lived,ping test Fail"
fi
} &
let ID++
done
wait
6、每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间
先写好一个备份的脚本
root@ubuntu:/data/scripts# cat etcback.sh
#!/bin/bash
#
PATH=/apps/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
BACKDIR="/backup"
FILENAME_TAR="etcbak-"`date -d "-1 day" +%F-%H`".tar.xz"
[ ! -d $BACKDIR ] && mkdir -p /backup
tar Jcvf $BACKDIR/$FILENAME_TAR /etc &>/dev/null
在使用crontab -e来添加任务
root@ubuntu:/data/scripts# crontab -e
root@ubuntu:/data/scripts# crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
30 1 * * 1-5 /data/scripts/etcback.sh