zoukankan      html  css  js  c++  java
  • 第六周练习题

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

    expect脚本:

    [10:40:45 root@centos8 ~]#cat expect{1..3}
    #!/usr/bin/expect
    spawn ssh 10.0.0.151
    expect {
           "yes/no" { send "yes ";exp_continue }
           "password" { send "123456 " }
    }
    interact

    ##################################################

    #!/usr/bin/expect
    set ip 10.0.0.151
    set user root
    set password 123456
    set timeout 10
    spawn ssh $user@$ip
    expect {
           "yes/no" { send "yes ";exp_continue }
           "password" { send "$password " }

    }
    interact

    ##################################################

    #!/usr/bin/expect
    set ip [lindex $argv 0]
    set user [lindex $argv 1]
    set password [lindex $argv 2]
    set timeout 10
    spawn ssh $user@$ip
    expect {
           "yes/no" { send "yes " ;exp_continue }
           "password" { send "$password " }

    }
    expect "#" { send "useradd hha " }
    send "exit "
    expect eof
    #使用expect -f expect1执行脚本,或者给执行权限后./expect1执行。

    shell脚本:


    [10:43:08 root@centos8 ~]#cat expect4
    #!/bin/bash
    network=10.0.0
    user=root
    password=123456
    iplist="
    151
    "


    for ID in $iplist;do
    ip=$network.$ID

    expect <<fff
    spawn ssh $user@$ip
    set timeout 3
    expect {
           "yes/no" { send "yes ";exp_continue }
           "password" { send "$password " }
    }
    expect "#" { send "useradd hhh " }
    expect eof
    fff
    done
    #使用bash expect4,或给执行权限后./expect4运行脚本
    2、生成10个随机数保存于数组中,并找出其最大值和最小值
    [11:04:11 root@centos8 ~]#./arr.sh
    All numbers are 2527 14178 12156 9055 23564 13074 302 13216 21235 20627
    MAX is 23564
    MIN is 2527
    [11:04:17 root@centos8 ~]#cat arr.sh
    #!/bin/bash

    #================================================================
    #   Copyright (C) 2021 IEucd Inc. All rights reserved.
    #
    #   文件名称:arr.sh
    #   创 建 者:TanLiang
    #   创建日期:2021年10月17日
    #   描   述:This is a test file
    #
    #================================================================

    #!/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 $mix ] && mix=${nums[$i]}
    done
    echo "All numbers are ${nums[*]}"
    echo MAX is $max
    echo MIN is $min
    3、输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序

    [12:38:09 root@centos8 ~]#bash sort.sh
    before sort:
    14756 26459 13397 30603 24422 17590 16022 5566 10825 20888 1210 220 9242 11466 6855 7255 9758 5254 28678 17400
    after sort:
    30603 28678 26459 24422 20888 17590 17400 16022 14756 13397 11466 10825 9758 9242 7255 6855 5566 5254 1210 220
    The max integer is 30603,the min integer is 220

    [12:39:18 root@centos8 ~]#cat sort.sh
    #!/bin/bash

    #================================================================
    #   Copyright (C) 2021 IEucd Inc. All rights reserved.
    #
    #   文件名称:sort.sh
    #   创 建 者:TanLiang
    #   创建日期:2021年10月17日
    #   描   述:This is a test file
    #
    #================================================================

    declare -a number
    for (( i=0;i<20;i++)) ;do
          number[$i]=$RANDOM
    done

    echo "before sort:"
    echo ${number[@]}

    declare -i n=20
    for ((i=0;i<n-1;i++));do
           for ((j=0;j<n-1-i;j++));do
                  let next=$j+1
                   if (( ${number[$j]}<${number[$next]})); then
                           tmp=${number[$next]}
                          number[$next]=${number[$j]}
                          number[$j]=$tmp
                   fi
           done
    done
    echo "after sort:"
    echo ${number[*]}
    echo "The max integer is ${number[0]},the min integer is ${number[$((n-1))]}"
    4、总结查看系统负载的几种命令,总结top命令的指标大概什么含义(不要求全部写出来)
    [12:57:20 root@centos8 ~]#uptime
    12:57:22 up 2 min,  1 user, load average: 0.12, 0.15, 0.07

    12:57:22 up 2 min 系统当前时间12:57:22,up表示系统运行的状态,开机运行了2分钟

    1 user 一个用户登录系统

    load average: 0.12, 0.15, 0.07   表示最近1,5,15分钟的系统负载情况12%,15%,7%

    [13:02:12 root@centos8 ~]#mpstat
    Linux 4.18.0-240.el8.x86_64 (centos8.magedu.org)        10/17/2021     _x86_64_       (2 CPU)

    01:02:18 PM CPU   %usr   %nice   %sys %iowait   %irq   %soft %steal %guest %gnice   %idle
    01:02:18 PM all    1.33    0.00    1.29    0.04    0.14    0.09    0.00    0.00    0.00   97.12

    mpstat 一次性返回系统负载情况

    mpstat 1 3 每秒显示一次负载,共输出三次

    字段的含义:

    CPU: 处理器编号。关键字all表示统计信息计算为所有处理器之间的平均值。

    %usr: 显示在用户级(应用程序)执行时发生的CPU利用率百分比。

    %nice: 显示以优先级较高的用户级别执行时发生的CPU利用率百分比。

    %sys: 显示在系统级(内核)执行时发生的CPU利用率百分比。请注意,这不包括维护硬件和软件的时间中断。

    %Iowait: 显示系统具有未完成磁盘I / O请求的CPU或CPU空闲的时间百分比。

    %irq: 显示CPU或CPU用于服务硬件中断的时间百分比。

    %soft: 显示CPU或CPU用于服务软件中断的时间百分比。

    %steal: 显示在虚拟机管理程序服务另一个虚拟处理器时虚拟CPU或CPU无意中等待的时间百分比。

    %guest: 显示CPU或CPU运行虚拟处理器所花费的时间百分比。

    %idle: 显示CPU或CPU空闲且系统没有未完成的磁盘I / O请求的时间百分比。


    [13:02:18 root@centos8 ~]#top
    top - 13:09:55 up 15 min,  1 user, load average: 0.00, 0.00, 0.00
    Tasks: 169 total,   1 running, 168 sleeping,   0 stopped,   0 zombie
    %Cpu(s):  0.0 us,  0.0 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.2 hi,  0.2 si,  0.0 st
    MiB Mem :   1949.4 total,   1395.6 free,    292.5 used,    261.3 buff/cache
    MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   1496.4 avail Mem

      PID USER     PR NI   VIRT   RES   SHR S %CPU %MEM     TIME+ COMMAND
      1514 root      20   0       0      0      0 I   0.3   0.0   0:00.71 kworker/1:0-events
      1588 root      20   0       0      0      0 I   0.3   0.0   0:00.27 kworker/0:0-event+
         1 root      20   0  177364  11268   8368 S   0.0   0.6   0:01.38 systemd
         2 root      20   0       0      0      0 S   0.0   0.0   0:00.00 kthreadd
         3 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_gp
         4 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_par_gp
         6 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 kworker/0:0H-kblo+
         8 root      20   0       0      0      0 I   0.0   0.0   0:00.10 kworker/u256:0-bo+
         9 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 mm_percpu_wq
        10 root      20   0       0      0      0 S   0.0   0.0   0:00.01 ksoftirqd/0

    第一行,返回结果相当于uptime
    top - 13:09:55 up 15 min,  1 user, load average: 0.00, 0.00, 0.00

    第二行,返回结果是任务进程信息。
    Tasks: 169 total,   1 running, 168 sleeping,   0 stopped,   0 zombie
    169 total , “所有启动的进程数”
    1 running , “正在运行的进程数”
    168 sleeping , “挂起的进程数”
    0 stopped , “停止的进程数”
    0 zombie , “僵尸进程数”

    第三行,返回结果是cpu相关信息。类似mpstat,sar命令返回结果
    %Cpu(s):  0.0 us,  0.0 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.2 hi,  0.2 si,  0.0 st
    0.0 us , “用户空间占用CPU百分比”、
    0.0 sy , “内核空间占用CPU百分比”、
    0.0 ni , “用户空间内改变过优先级的进程占用CPU百分比”、
    99.7 id , “空闲CPU百分比”、
    0.0 wa , “等待输入输出CPU时间百分比”、
    0.2 hi , “CPU服务于硬件中断所耗费的时间总额”、
    0.2 si , “CPU服务软中断所耗费的时间总额”、
    0.0 st , “Steal Time是指虚拟机vm需要执行某个命令但是没有抢到物理cpu来执行的时间”

    第四五行,返回结果是内存相关信息,类似free命令
    MiB Mem :   1949.4 total,   1395.6 free,    292.5 used,    261.3 buff/cache
    MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   1496.4 avail Mem
    显示内容依次为“物理内存总量”、“已使用的物理内存”、“空闲物理内存”、“内核缓存内存量”。
    显示内容依次为“交换区总量”、“已使用交互区总量”、“空闲交换区总量”、“缓冲的交换区总量”。

    第六行,“PID USER     PR NI VIRT RES SHR S %CPU %MEM   TIME+ COMMAND ”显示内容依次为“进程ID”、“进程所有者”、“优先级”、“nice值,负值表示高优先级,正值表示低优先级”、“进程使用的虚拟内存总量”、“进程使用的、未被换出的物理内存大小”、“共享内存大小”、“进程状态”、“上次更新到现在的CPU时间占用百分比”、“进程使用的物理内存百分比”、“进程使用CPU总时间”、“命令名、命令行”。
    5、编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"

    for方法:


    [14:20:07 root@centos8 ~]#cat ping_for.sh
    #!/bin/bash

    #================================================================
    #   Copyright (C) 2021 IEucd Inc. All rights reserved.
    #
    #   文件名称:ping_for.sh
    #   创 建 者:TanLiang
    #   创建日期:2021年10月17日
    #   描   述:This is a test file
    #
    #================================================================

    #!/bin/bash
    NETID=192.168.0
    for ip in {1..254};do
          {
                  if /bin/ping -c1 -w1 $NETID.$ip >/dev/null;then
                          echo "ping $NETID.$ip is success!"
                  else
                          echo "ping $NETID.$ip is fail!"
                  fi
          }&
    done
    wait

    while方法:


    [14:43:33 root@centos8 ~]#cat ping_while.sh
    #!/bin/bash

    #================================================================
    #   Copyright (C) 2021 IEucd Inc. All rights reserved.
    #
    #   文件名称:ping_for.sh
    #   创 建 者:TanLiang
    #   创建日期:2021年10月17日
    #   描   述:This is a test file
    #
    #================================================================

    #!/bin/bash
    NETID=192.168.0
    declare -i ip=1
          while [ $ip -lt 255 ];do
          {
          /bin/ping -c1 -w1 $NETID.$ip &>/dev/null
          if [ $? -eq 0 ];then
                  echo "ping $NETID.$ip is success!"

          else
                  echo "ping $NETID.$ip is fail!"
          fi
          }&
          let ip++
    done
    wait

    [14:46:59 root@centos8 ~]#bash ping_for.sh |sort -n -t"." -k 4
    6、每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间

    [14:55:00 root@centos8 ~]#crontab -l
    30 1 * * 1-5 /home/backup.sh
    [14:55:09 root@centos8 ~]#cat /home/backup.sh
    #!/bin/bash

    #================================================================
    #   Copyright (C) 2021 IEucd Inc. All rights reserved.
    #
    #   文件名称:backup.sh
    #   创 建 者:TanLiang
    #   创建日期:2021年10月17日
    #   描   述:This is a test file
    #
    #================================================================

    #将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间
    dir=etcbak-`date -d '-1 day' +%Y-%m-%d-%H`
    tar -Jcvf /backup/${dir}.tar.xz /etc/ &> /dev/null
  • 相关阅读:
    C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本
    C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本
    C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本
    C#.NET 大型通用信息化系统集成快速开发平台 4.0 版本
    C#.NET 大型通用信息化系统集成快速开发平台 4.0 版本
    C#.NET 大型通用信息化系统集成快速开发平台 4.0 版本
    C#.NET 大型通用信息化系统集成快速开发平台 4.0 版本
    C#.NET 大型通用信息化系统集成快速开发平台 4.0 版本
    C#.NET 大型通用信息化系统集成快速开发平台 4.0 版本
    通用用户权限管理系统组件4.0 版本
  • 原文地址:https://www.cnblogs.com/tanll/p/15416997.html
Copyright © 2011-2022 走看看