马哥教育SRE笔记【作业】week04

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

# expect 方式

[root@centos83localdomain data]#yum install expect
...
[root@centos83localdomain data]#cat ./expect 
#!/usr/bin/expect
#
#**********************************
#Author:                laozhang
#Date:                  2022-05-01
#********************************** 
set ip 10.0.0.154
set user root
set password zz
set timeout 10
spawn ssh $user@$ip
expect {
        "yes/no" { send "yes\n";exp_continue }
        "password" { send "zz\n" }
}
interact
[root@centos83localdomain data]#chmod u+x ./expect
[root@centos83localdomain data]#./expect
spawn ssh root@10.0.0.154
root@10.0.0.154's password: 
Last login: Sun May  1 09:48:41 2022 from 10.0.0.150
[root@centos79 ~]# 
# shell 脚本方式

[root@centos83localdomain data]#yum install -y sshpass
...
[root@centos83localdomain data]#cat sshtest.sh
#!/bin/bash
#
#**********************************
#Author:                laozhang
#Date:                  2022-05-01
#FileName:              sshtest.sh
#Description:   The test script
#**********************************
IP=10.0.0.154
USER="root"
PASSWORD="zz"
sshpass -p $PASSWORD ssh $USER@$IP

[root@centos83localdomain data]#chmod u+x sshtest.sh
[root@centos83localdomain data]#./sshtest.sh 
Last login: Sun May  1 09:51:47 2022 from 10.0.0.150

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

[root@centos83localdomain data]#cat ./nums_test.sh
#!/bin/bash
#
#**********************************
#Author:                laozhang
#Date:                  2022-05-01
#FileName:              nums_test.sh
#Description:   The test script
#**********************************
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@centos83localdomain data]#chmod u+x nums_test.sh 
[root@centos83localdomain data]#./nums_test.sh
all numbers are 1325 22326 19803 6089 16762 18891 16842 20446 5566 7078
max is 22326
min is 1325
[root@centos83localdomain data]#./nums_test.sh
all numbers are 14856 31913 28668 3055 8327 12192 24329 23274 16743 2313
max is 31913
min is 2313

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

[root@centos83localdomain data]#cat nums.sh 
#!/bin/bash
#
#**********************************
#Author:                laozhang
#Date:                  2022-05-01
#FileName:              nums.sh
#Description:   The test script
#**********************************

#输入数据组成数组(100个数以内)
declare -a nums
for (( i=0; i<100; i++ ));do
        read  -p "please input numbers:" NUM

        nums[$i]=$NUM

		# 从大到小排序并打印
        declare -i n=${#nums[@]}
        for (( j=0;j<n-1;j++ ));do
                for (( k=0;k<n-1-j;k++ ));do
                        let x=$k+1
                        if (( ${nums[$k]} < ${nums[$x]} ));then
                                tmp=${nums[$x]}
                                nums[$x]=${nums[$k]}
                                nums[$k]=$tmp
                        fi
                done
        done
        echo "The order of numbers is ${nums[@]}"
done


[root@centos83localdomain data]#chmod u+x nums.sh
[root@centos83localdomain data]#./nums.sh 
please input numbers:5
The order of numbers is 5
please input numbers:2
The order of numbers is 5 2
please input numbers:8
The order of numbers is 8 5 2
please input numbers:111
The order of numbers is 111 8 5 2
please input numbers:999
The order of numbers is 999 111 8 5 2
please input numbers:33
The order of numbers is 999 111 33 8 5 2
please input numbers:2222
The order of numbers is 2222 999 111 33 8 5 2
please input numbers:

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

(1)常用查看系统负责的命令

# 负载查询 uptime
[root@centos83localdomain data]#uptime
 15:29:54 up  7:48,  3 users,  load average: 0.00, 0.00, 0.00
 
# 显示CPU相关统计 mpstat
[root@centos83localdomain data]#mpstat
Linux 4.18.0-240.el8.x86_64 (centos83localdomain)       05/01/2022      _x86_64_ (2 CPU)

03:31:06 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
03:31:06 PM  all    0.28    0.02    0.26    0.02    0.08    0.06    0.00    0.00    0.00   99.28

# 查看进程实时状态 top
top - 15:32:07 up  7:50,  3 users,  load average: 0.02, 0.01, 0.00
Tasks: 279 total,   1 running, 278 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.2 us,  0.0 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  
MiB Mem :   1949.4 total,    121.4 free,   1433.6 used,    394.4 buff/cac
MiB Swap:   2048.0 total,   1730.2 free,    317.8 used.    338.5 avail Me

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ 
   2379 root      20   0  205244  25580   5168 S   0.3   1.3   0:37.09 
      1 root      20   0  246256  11004   6904 S   0.0   0.6   0:04.35 
      2 root      20   0       0      0      0 S   0.0   0.0   0:00.02 
      3 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 
      4 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 
      6 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 
      9 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 
     10 root      20   0       0      0      0 S   0.0   0.0   0:00.19 
     
# 查看内存空间 free
[root@centos83localdomain data]#free -h
              total        used        free      shared  buff/cache   available
Mem:          1.9Gi       1.4Gi       122Mi       8.0Mi       394Mi       339Mi
Swap:         2.0Gi       317Mi       1.7Gi

# 统计CPU和设备IO信息 iostat
[root@centos83localdomain data]#iostat
Linux 4.18.0-240.el8.x86_64 (centos83localdomain)       05/01/2022      _x86_64_ (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.28    0.02    0.40    0.02    0.00   99.29

Device             tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               2.44       107.69        53.49    3049307    1514569
sdb               0.00         0.08         0.00       2224          0
scd0              0.00         0.07         0.00       2089          0

# 监视磁盘I/O 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  SWAPIN     IO>    COMMAND 
      1 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % syste~ze 18
      2 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kthreadd]
      3 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_gp]
      4 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_~r_gp]
      6 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kwor~ockd]
      9 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [mm_p~u_wq]
     10 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ksof~qd/0]

# 显示网络带宽使用情况 iftop
              12.5Kb         25.0Kb        37.5Kb         50.0Kb   62.5Kb
+-------------+--------------+-------------+--------------+--------------
10.0.0.150             => 10.0.0.1                 864b   2.01Kb  2.01Kb
                       <=                          208b    347b    347b


# 查看网络实时吞吐量 nload
Device ens33 [10.0.0.150] (1/4):
=========================================================================
Incoming:
                                                  Curr: 1.02 kBit/s
                                                  Avg: 1.07 kBit/s
                                                  Min: 1.02 kBit/s
                                                  Max: 2.04 kBit/s
                                                  Ttl: 944.36 MByte
Outgoing:
                                                  Curr: 7.77 kBit/s
                                                  Avg: 7.58 kBit/s
                                                  Min: 4.14 kBit/s
                                                  Max: 7.79 kBit/s
                                                  Ttl: 5.26 MByte


# 系统资源统计 dstat
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 
  0   0  99   0   0|   0     0 |  66B  246B|   0     0 |  93   135 
  0   0 100   0   0|   0     0 |  66B  182B|   0     0 |  86   136 
  1   0  99   0   0|   0     0 |  66B  182B|   0     0 | 119   178 
  0   0 100   0   0|   0     0 | 126B  242B|   0     0 |  75   130 

(2)top指标含义

  • us:用户空间

  • sy:内核空间

  • ni:调整nice时间

  • id:空闲

  • wa:等待IO时间

  • hi:硬中断

  • si:软中断(模式切换)

  • st:虚拟机偷走的时间

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

(1)for方法

[root@centos83localdomain data]#cat ./for_scan_host.sh
#!/bin/bash
#
#**********************************
#Author:                laozhang
#Date:                  2022-05-01
#FileName:              for_scan_host.sh
#Description:   The test script
#**********************************
NET=192.168.0
for ID in {1..254};do
        {
                ping -c1 -W1 $NET.$ID &> /dev/null && echo $NET.$ID is success! | tee -a host_list.log || echo $NET.$ID is fail!
        }&
done
wait
[root@centos83localdomain data]#chmod u+x for_scan_host.sh 
[root@centos83localdomain data]#./for_scan_host.sh 
192.168.0.1 is fail!
192.168.0.4 is fail!
192.168.0.30 is fail!
192.168.0.58 is fail!
192.168.0.74 is fail!
192.168.0.96 is fail!
192.168.0.114 is fail!
192.168.0.41 is fail!
192.168.0.19 is fail!
192.168.0.39 is fail!
192.168.0.13 is fail!
192.168.0.29 is fail!
...

(2)while方法

[root@centos83localdomain data]#cat while_scan_host.sh 
#!/bin/bash
#
#**********************************
#Author:                laozhang
#Date:                  2022-05-01
#FileName:              for_scan_host.sh
#Description:   The test script
#**********************************
NET=192.168.0
ID=1
while (($ID<255));do 
        ping -c1 -W1 $NET.$ID &> /dev/null && echo $NET.$ID is success! || echo $NET.$ID is fail!
        ID=$(($ID+1))
done
wait
[root@centos83localdomain data]#chmod u+x while_scan_host.sh 
[root@centos83localdomain data]#./while_scan_host.sh
192.168.0.1 is fail!
192.168.0.2 is fail!
192.168.0.3 is fail!
192.168.0.4 is fail!
192.168.0.5 is fail!
192.168.0.6 is fail!
192.168.0.7 is fail!
192.168.0.8 is fail!
192.168.0.9 is fail!
192.168.0.10 is fail!
...

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

[root@centos83localdomain data]#crontab -e
[root@centos83localdomain data]#crontab -l
30 1 * * 1-5 /data/backup.sh
[root@centos83localdomain data]#cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/data
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

[root@centos83localdomain data]#cat /data/backup.sh 
#!/bin/bash
#
#**********************************
#Author:                laozhang
#Date:                  2022-05-01
#FileName:              backup.sh
#Description:   The test script
#**********************************
COLOR='echo -e \E[1;32m'
END='\E[0m'
BACKUP=/backup/
SRC=/etc
DATE=`date -d '-1 day' +%F-%H`
if [ ! -d $BACKUP ];then
   mkdir -p $BACKUP
fi
${COLOR}Starting backup...$END
sleep 2
tar Jcf ${BACKUP}etcbak-$DATE.tar.xz $SRC
${COLOR}Backup is finished$END

# 提前执行脚本验证一下
[root@centos83localdomain data]#/data/backup.sh
Starting backup...
tar: Removing leading `/' from member names
Backup is finished
[root@centos83localdomain data]#ll /backup/
total 3832
-rw-r--r--. 1 root root 3922852 May  1 17:06 etcbak-2022-04-30-17.tar.xz
[root@centos83localdomain data]#

7、描述密钥交换的过程

  • 客户端发起链接请求
  • 服务端返回自己的公钥,以及一个会话ID(这一步客户端得到服务端公钥)客户端生成密钥对
  • 客户端用自己的公钥异或会话ID,计算出一个值Res,并用服务端的公钥加密
  • 客户端发送加密后的值到服务端,服务端用私钥解密,得到Res
  • 服务端用解密后的值Res异或会话ID,计算出客户端的公钥(这一步服务端得到客户端公钥)
  • 最终:双方各自持有三个秘钥,分别为自己的一对公、私钥,以及对方的公钥,之后的所有通讯都会被加密

8、https的通信过程

  1. 客户端发起HTTPS请求
    用户在浏览器里输入一个https网址,然后连接到服务器的443端口
  2. 服务端的配置
    采用HTTPS协议的服务器必须要有一套数字证书,可以自己制作,也可以向组织申请。区别就是自己颁发的证书需要客户端验证通过,才可以继续访问,而使用受信任的公司申请的证书则不会弹出提示页面。这套证书其实就是一对公钥和私钥
  3. 传送服务器的证书给客户端
    证书里其实就是公钥,并且还包含了很多信息,如证书的颁发机构,过期时间等等
  4. 客户端解析验证服务器证书
    这部分工作是由客户端的TLS来完成的,首先会验证公钥是否有效,比如:颁发机构,过期时间等等,如果发现异常,则会弹出一个警告框,提示证书存在问题。如果证书没有问题,那么就生成一个随机值。然后用证书中公钥对该随机值进行非对称加密
  5. 客户端将加密信息传送服务器
    这部分传送的是用证书加密后的随机值,目的就是让服务端得到这个随机值,以后客户端和服务端的通信就可以通过这个随机值来进行加密解密了
  6. 服务端解密信息
    服务端将客户端发送过来的加密信息用服务器私钥解密后,得到了客户端传过来的随机值
  7. 服务器加密信息并发送信息
    服务器将数据利用随机值进行对称加密,再发送给客户端
  8. 客户端接收并解密信息
    客户端用之前生成的随机值解密服务段传过来的数据,于是获取了解密后的内容

9、使用awk以冒号分隔获取/ettc/passwd文件第一列

[root@centos83localdomain ~]#awk -F: '{print $1}' /etc/passwd | head
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值