美文网首页
shell_script 练习题

shell_script 练习题

作者: 心疼你萌萌哒 | 来源:发表于2018-05-09 11:34 被阅读0次

    shell_script练习题

    1. 获取随机生成的8位字符串:
        # echo $RANDOM | md5sum | cut -c 1-8
        # openssl rand -base64 4
        # cat /proc/sys/kernel/random/uuid | cut -c 1-8
    
    2. 获取随机的8位数字:
        # echo $RANDOM |cksum |cut -c 1-8
        # openssl rand -base64 4 | cut -c 1-8
        # date +%N |cut -c 1-8
    
    3. 检查软件包是否安装:
        #!/usr/bin/env bash
        if rpm -qa sysstat &>/dev/null; then
            echo "sysstat is already installed."
        else
            echo "sysstat is not installed!"
        fi
    
    4. 监控系统中的重要地方:
    cpu:
    # cat cpu_vmstat.sh
    #!/bin/bash
    DATE=$(date +%F" "%H:%M)
    IP=`ifconfig | awk -F' ' 'NR==2{print $2}'`
    MAIL="liuchao07a@163.com"
    if ! which vmstat &>/dev/null; then
        echo "vmstat command no found, Please install procps package."
        exit 1
    fi
    US=$(vmstat |awk 'NR==3{print $13}')
    SY=$(vmstat |awk 'NR==3{print $14}')
    IDLE=$(vmstat |awk 'NR==3{print $15}')
    WAIT=$(vmstat |awk 'NR==3{print $16}')
    USE=$(($US+$SY))
    if [ $USE -ge 50 ]; then
        echo "
        Date: $DATE
        Host: $IP
        Problem: CPU utilization $USE
        " | mail -s "CPU Monitor" $MAIL
    
    memory:
    
    #!/bin/bash
    DATE=$(date +%F" "%H:%M)
    IP=$(ifconfig eth0 |awk -F '[ :]+' '/inet addr/{print $4}')
    MAIL="example@mail.com"
    TOTAL=$(free -m |awk '/Mem/{print $2}')
    USE=$(free -m |awk '/Mem/{print $3-$6-$7}')
    FREE=$(($TOTAL-$USE))
    # 内存小于1G发送报警邮件
    if [ $FREE -lt 1024 ]; then
        echo "
        Date: $DATE
        Host: $IP
        Problem: Total=$TOTAL,Use=$USE,Free=$FREE
        " | mail -s "Memory Monitor" $MAIL
    
    disk:
    
    #!/bin/bash
    DATE=$(date +%F" "%H:%M)
    IP=$(ifconfig eth0 |awk -F '[ :]+' '/inet addr/{print $4}')
    MAIL="example@mail.com"
    TOTAL=$(fdisk -l |awk -F'[: ]+' 'BEGIN{OFS="="}/^Disk \/dev/{printf "%s=%sG,",$2,$3}')
    PART_USE=$(df -h |awk 'BEGIN{OFS="="}/^\/dev/{print $1,int($5),$6}')
    for i in $PART_USE; do
        PART=$(echo $i |cut -d"=" -f1)
        USE=$(echo $i |cut -d"=" -f2)
        MOUNT=$(echo $i |cut -d"=" -f3)
        if [ $USE -gt 80 ]; then
            echo "
            Date: $DATE
            Host: $IP
            Total: $TOTAL
            Problem: $PART=$USE($MOUNT)
            " | mail -s "Disk Monitor" $MAIL
        fi
    
    5. 系统初始化:
    
    #!/usr/bin/env bash
    #
    # Author: liuchao
    # Email: liuchao07a@icloud.com
    # Version: v1.0
    # BeiJingQianFengHuLianKeJiYouXianGongSi.
    
    # This script is used to set your server to initialize, and it can be used to install the necessary software,
    # set your yum source, but also configure some security settings, you can also add the appropriate functionality
    # to meet your needs
    
    
    
    # Get os version.
    OSRELEASE=$(rpm -q --qf "%{Version}" $(rpm -q --whatprovides redhat-release))
    
    # Configure yum source, config is BeiJingQFcloud.
    sed -ri s/^#baseurl/baseurl/g /etc/yum.repos.d/CentOS-Base.repo
    sed -ri s/^mirrorlist/#mirrorlist/g /etc/yum.repos.d/CentOS.Base.repo
    
    yum clean all
    yum makecache
    wait
    
    yum -y install epel-release &>/dev/null
    
    sed -ri s/^#baseurl/baseurl/g /etc/yum.repos.d/epel.repo
    sed -ri s/^mirrorlist/#mirrorlist/g /etc/yum.repos.d/epel.repo
    
    yum makecache
    wait
    
    # Install software.
    yum -y install nc vim iftop iotop dstat tcpdump
    yum -y install ipmitool bind-libs bind-utils
    yum -y install libselinux-python ntpdate
    yum -y install net-tools lftp
    
    # disable unuse service.
    systemctl stop auditd && systemctl disable auditd
    systemctl stop firewalld && systemctl disable firewalld
    
    # disable ipv6.
    cd /etc/modprobe.d/ && touch ipv6.conf
    >/etc/modprobe.d/ipv6.conf
    cat >> /etc/modprobe.d/ipv6.conf <<EOF
    alias net-pf-10 off
    alias ipv6 off
    EOF
    
    # set route forward.
    >/etc/sysctl.conf
    cat >> /etc/sysctl.conf <<EOF
    net.ipv4.ip_forward = 1
    net.ipv4.conf.default.rp_filter = 1
    net.ipv4.conf.default.accept_source_route = 0
    kernel.sysrq = 0
    kernel.core_uses_pid = 1
    net.ipv4.tcp_syncookies = 1
    kernel.msgmnb = 65536
    kernel.msgmax = 65536
    kernel.shmmax = 68719476736
    kernel.shmall = 4294967296
    EOF
    
    # disable SELINUX.
    setenforce 0
    sed -ri s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config
    
    # set history display datetime.
    echo "HISTFILESIZE=2000" >> /etc/bashrc && echo "HISTSIZE=2000" >> /etc/bashrc && echo 'HISTTIMEFORMAT="%Y%m%d %T "'>> /etc/bashrc && export HISTTIMEFORMAT
    
    
    
    6. 搭建内网yum源服务器,并定期同步公网数据
    
    #!/bin/bash
    #
    # BeiJing QFcloud office made.
    # author:liuchao
    # 2017/8/12.
    
    if [ -f /var/log/yum_server.pid ];then
        rm -rf /var/log/yum_server.pid
    else
        echo "Ok. Continue rsync yum server." &> /dev/null
    fi
    
    YUM_SITE="rsync://mirrors.tuna.tsinghua.edu.cn/centos/"
    #存放路径,我这里直接用的是httpd默认根目录
    LOCAL_PATH="/var/www/html/centos/"
    #需要同步哪些目录
    LOCAL_VER='6 6* 7 7*'
    BW_limit=2048
    LOCK_FILE="/var/log/yum_server.pid"
    RSYNC_PATH=""
    
    # check update yum server pid
    MY_PID=$$
    if [ -f $LOCK_FILE ]; then
    get_pid=`/bin/cat $LOCK_FILE`
    get_system_pid=`/bin/ps -ef|grep -v grep|grep $get_pid|wc -l`
    if [ $get_system_pid -eq 0 ] ; then
    echo $MY_PID>$LOCK_FILE
    else
    echo "Have update yum server now!"
    exit 1
    fi
    else
    echo $MY_PID>$LOCK_FILE
    fi
    
    # check rsync tool
    if [ -z $RSYNC_PATH ]; then
    RSYNC_PATH=`/usr/bin/whereis rsync|awk ' ''{print $2}'`
    if [ -z $RSYNC_PATH ]; then
    echo 'Not find rsync tool.'
    echo 'use comm: yum install -y rsync'
    fi
    fi
    
    # sync yum source
    for VER in $LOCAL_VER;
    do
    # Check whether there are local directory
    if [ ! -d "$LOCAL_PATH$VER" ] ; then
    echo "Create dir $LOCAL_PATH$VER"
    `/bin/mkdir -p $LOCAL_PATH$VER`
    fi
    # sync yum source
    echo "Start sync $LOCAL_PATH$VER"
    $RSYNC_PATH -avrtH --delete --bwlimit=$BW_limit --exclude "isos" $YUM_SITE$VER $LOCAL_PATH
    done
    
    # clean lock file
    `/bin/rm -f $LOCK_FILE`
    
    echo "rsync end $(date +%Y-%m-%d_%k:%M:%S)" >> /var/www/html/centos/centos_rsync_is_end.txt
    exit 1
    
    1.使用shell脚本,判断10.18.41.0/24网络里,当前在线的IP有哪些。并对在线的主机批量传送公钥
    2.使用shell脚本,获取本机的详细信息(当前用户,IP,内核版本,主机名称,CPU版本信息……)
    3.请按照这样的日期格式(xxxx-xx-xx)每日生成一个文件,例如今天生成的文件为)2017-07-05.log, 并且把磁盘的使用情况
    写到到这个文件中,(不用考虑cron,仅仅写脚本即可)
    5.使用shell脚本进行判断磁盘,内存使用情况,当磁盘,内存使用量超过20%,就发警报进行报警(或者发报警邮件给自己)
    工具分享:
    top 查看进程活动状态以及一些系统状况
    vmstat 查看系统状态、硬件和系统信息等
    iostat 查看CPU 负载,硬盘状况
    sar 综合工具,查看系统状况
    mpstat 查看多处理器状况
    netstat 查看网络状况
    iptraf 实时网络状况监测
    tcpdump 抓取网络数据包,详细分析
    tcptrace 数据包分析工具
    netperf 网络带宽工具
    
    

    相关文章

      网友评论

          本文标题:shell_script 练习题

          本文链接:https://www.haomeiwen.com/subject/bysnrftx.html