第6周作业

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

#expect方式
[root@centos8-fosun shell]#cat ssh_expect 
#!/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
[root@centos8-fosun shell]#./ssh_expect 172.16.6.242 root Fxcz@999
spawn ssh root@172.16.6.242
The authenticity of host '172.16.6.242 (172.16.6.242)' can't be established.
RSA key fingerprint is SHA256:LxvFcTH+QAWB84zgAHwvp62++osvOqIDhZHeJ63ybRo.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '172.16.6.242' (RSA) to the list of known hosts.
root@172.16.6.242's password: 
Last login: Sat Dec 25 17:52:59 2021 from 10.10.10.84
[root@centos6-fosun ~]# exit
logout
Connection to 172.16.6.242 closed.


#shell
[root@centos8-fosun shell]#cat ssh_shell.sh 
#!/bin/bash
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-fosun shell]#bash ssh_shell.sh 172.16.6.242 root Fxcz@999
spawn ssh root@172.16.6.242
root@172.16.6.242's password: 
Last login: Sun Dec 26 23:31:10 2021 from 172.16.6.240
[root@centos6-fosun ~]# 

2、生成10个随机数保存于数组中,并找出其最大值和最小值

[root@centos8-fosun shell]#cat max_min.sh 
#!/bin/bash
declare -i min max
echo "-----------------------"
declare -a nums
for ((i=0;i<10;i++));do
    nums[$i]=$RANDOM
    echo ${nums[$i]}
    [ $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-fosun shell]#bash max_min.sh 
29893
17979
23110
26154
137
13779
22336
20156
15252
9353
All numbers are 29893 17979 23110 26154 137 13779 22336 20156 15252 9353
Max is 29893
Min is 137

3、输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序

[root@centos8-fosun shell]#cat sort.sh
#!/bin/sh
#sorting following array
echo "please input a number list:"
read -a arr
for (( i=0; i<${#arr[@]}; i++ ));do
    for (( j=${#arr[@]} - 1; j>i; j-- ));do
    echo $j
        if  [[ ${arr[j]} -lt ${arr[j-1]} ]];then
            t=${arr[j]}
            arr[j]=${arr[j-1]}
            arr[j-1]=$t
        fi
    done
done
echo "after sorting:"
echo ${arr[@]}

echo ${arr[@]}[root@centos8-fosun shell]#bash sort.sh 
please input a number list:
123 234 111 98 345 211
5
4
3
2
1
5
4
3
2
5
4
3
5
4
5
after sorting:
98 111 123 211 234 345

4、总结查看系统负载的几种命令,总结top命令的指标大概什么含义(不要求全部写出来)

#查看系统负载有top\htop\uptime\mpstat等命令:
[root@centos8-fosun ~]#uptime 
 11:12:43 up 6 days, 14:29,  4 users,  load average: 0.08, 0.02, 0.01
 
 [root@centos8-fosun ~]#mpstat
Linux 4.18.0-240.el8.x86_64 (centos8-fosun) 	12/23/2021 	_x86_64_	(4 CPU)

11:13:55 AM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
11:13:55 AM  all    0.04    0.01    0.03    0.00    0.06    0.02    0.00    0.00    0.00   99.85
[root@centos8-fosun ~]#mpstat 1 3
Linux 4.18.0-240.el8.x86_64 (centos8-fosun) 	12/23/2021 	_x86_64_	(4 CPU)

11:14:38 AM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
11:14:39 AM  all    0.00    0.00    0.00    0.00    0.25    0.00    0.00    0.00    0.00   99.75
11:14:40 AM  all    0.25    0.00    0.25    0.00    0.00    0.00    0.00    0.00    0.00   99.50
11:14:41 AM  all    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
Average:     all    0.08    0.00    0.08    0.00    0.08    0.00    0.00    0.00    0.00   99.75

top命令的指标含义:

image-20211121182929116

第一行(top):
“18:29:05为系统当前时刻;
“up 3 days,21:17”为系统启动后到现在的运作时间;
“2 users”为当前登录到系统的用户,更确切的说是登录到用户的终端数 -- 同一个用户同一时间对系统多个终端的连接将被视为多个用户连接到系统,这里的用户数也将表现为终端的数目;
“load average”为当前系统负载的平均值,后面的三个值分别为1分钟前、5分钟前、15分钟前进程的平均数,一般的可以认为这个数值超过 CPU 数目时,CPU 将比较吃力的负载当前系统所包含的进程;本服务器有32个CPU,虽然数值为6,但负载还是有些吃力的。

第二行(Tasks):
“224 total”为当前系统进程总数;
“1 running”为当前运行中的进程数;
“223 sleeping”为当前处于等待状态中的进程数;
“0 stoped”为被停止的系统进程数;
“0 zombie”为被复原的进程数;

第三行(Cpus):
分别表示了 CPU 当前的使用率;

Cpu(s): 0.0 us	用户空间占用CPU百分比
1.4 sy	内核空间占用CPU百分比
0.0 ni	用户进程空间内改变过优先级的进程占用CPU百分比
97.1 id	空闲CPU百分比
0 wa	等待输入输出的CPU时间百分比

第四行(Mem):
分别表示了内存总量、当前使用量、空闲内存量、以及缓冲使用中的内存量;

第五行(Swap):
表示类别同第四行(Mem),但此处反映着交换分区(Swap)的使用情况。通常,交换分区(Swap)被频繁使用的情况,将被视作物理内存不足而造成的。

5、编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"

[root@centos8-fosun shell]# cat ping_for.sh 
#!/bin/bash
net=192.168.0.
for i in {1..254};do
    { ping -c1 -w1 ${net}${i} &> /dev/null && echo $net$i "success!" || echo $net$i "fail!"; }&
done
wait
[root@centos8-fosun shell]#bash ping_for.sh 
192.168.0.1 success!
192.168.0.10 success!
...
192.168.0.251 fail!
192.168.0.222 fail!

[root@centos8-fosun shell]#cat ping_while.sh 
#!/bin/bash
net=192.168.0.
declare -i hostid=1
while [ $hostid -lt 255 ];do
     ping -c1 -w1 ${net}${hostid} &> /dev/null && echo $net$hostid "success!" || echo $net$hostid "fail!";
     let hostid++
done
wait
[root@centos8-fosun shell]#bash ping_while.sh 
192.168.0.1 success!
192.168.0.10 success!
...
192.168.0.251 fail!
192.168.0.222 fail!

6、每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间

[root@centos8-fosun shell]#cat crontab-back.sh 
#!/bin/bash
backtime=`date +%Y-%m-%d-%H`
/usr/bin/tar -JcPf /backup/etc-$backtime.tar.xz /etc/ &> /dev/null
[root@centos8-fosun shell]#ll /backup/
total 3188
-rw-r--r-- 1 root root 3264268 Dec 27 00:44 etc-2021-12-27-00.tar.xz
[root@centos8-fosun shell]#chmod +x crontab-back.sh 
[root@centos8-fosun shell]#crontab -e
crontab: installing new crontab
[root@centos8-fosun shell]#crontab -l
30 1 * * 1-5 /data/shell/crontab-backup.sh 
posted @ 2021-12-27 00:49  %华&仔%  阅读(32)  评论(0编辑  收藏  举报