• shell编程练习(三): 笔试21-30


    笔试练习(三):

    21、编写shell程序,实现自动删除30个账号的功能。

    账号名为std01至std30。

    [root@VM_0_5_centos test]# vi 21.sh 
    [root@VM_0_5_centos test]# cat 21.sh 
    #!/bin/bash
    #编写shell程序,实现自动删除30个账号的功能。账号名为std01至stud30
    #要有root权限
    
    for i in {9901..9930}; do
        xx=`echo $i | sed 's/99//g'`
            userdel -r std$xx
    done

    22、用户清理,清除本机除了当前登陆用户以外的所有用户

    [root@VM_0_5_centos test]# vi 22.sh
    [root@VM_0_5_centos test]# cat 22.sh 
    #!/bin/bash
    a=`echo $0 | sed 's/..(...).*/1/'`
    for i in `w|awk -v b=$a 'NR>2{if($NF !~ b) print $2}'` do echo $i fuser -k /dev/$i done [root@VM_0_5_centos test]# sh 22.sh pts/0 /dev/pts/0: 978 3473 Connection closed by foreign host. Disconnected from remote host(tencentYun) at 15:31:30. Type `help' to learn how to use Xshell prompt.

    23、设计一个shell程序,在每月第一天备份并压缩/etc目录的所有内容,

    存放在/root/bak目录里,且文件名,为如下形式yymmdd_etc,yy为年,mm为月,dd为日。Shell程序fileback存放在/usr/bin目录下。

    [root@VM_0_5_centos test]# vi 23.sh
    [root@VM_0_5_centos test]# cat 23.sh 
    #!/bin/bash
    #需要有root权限
    
    filename=`date +%y%m%d`_etc.tar.gz
    #cd /etc/
    tar -zcvf $filename *
    mv $filename /root/bak/
    
    
    # vim /etc/crontab 加入
    # * * 1 * * root ./23.sh &
    # 加入定时管理模块

    24、对于一个用户日志文件,每行记录了一个用户查询串,长度为1-255字节,共几千万行,请列出查询最多的前100条。

    日志可以自己构造。 (提示:awk sort uniq head)

    [root@VM_0_5_centos test]# mkdir -p 24
    [root@VM_0_5_centos test]# r -y
    -bash: r: command not found
    [root@VM_0_5_centos test]# rz -E
    rz waiting to receive.
    [root@VM_0_5_centos test]# ll
    total 24
    -rw-r--r-- 1 root root  212 Aug  6 15:28 21.sh
    -rw-r--r-- 1 root root  148 Aug  6 15:27 22.sh
    -rw-r--r-- 1 root root  125 Aug  6 15:38 23.sh
    drwxr-xr-x 2 root root 4096 Aug  6 15:39 24
    -rw-r--r-- 1 root root  109 May 20  2015 24.sh
    -rw-r--r-- 1 root root 2090 May 20  2015 testdata.txt
    [root@VM_0_5_centos test]# mv 24.sh testdata.txt 24
    [root@VM_0_5_centos test]# cd 24
    [root@VM_0_5_centos 24]# ll
    total 8
    -rw-r--r-- 1 root root  109 May 20  2015 24.sh
    -rw-r--r-- 1 root root 2090 May 20  2015 testdata.txt
    
    
    [root@VM_0_5_centos 24]# cat 24.sh 
    #! /bin/bash
    
    # show top 10
    
    file=$1
    awk '{print $1}' testdata.txt | sort | uniq -c | sort -k1nr | head -n10
    
    
    [root@VM_0_5_centos 24]# sh 24.sh 
         13 廖铭杰
         10 徐磊
          3 刘刚
          3 宋世达
          3 熊鹏飞
          1 丁俊龙
          1 于仲
          1 任凯
          1 冯夏
          1 冯宇鹏

    25、编写自己的ubuntu环境安装脚本

    26、编写服务器守护进程管理脚本。

    27、查看TCP连接状态

    netstat -nat |awk ‘{print $6}’|sort|uniq -c|sort -rn
    
    netstat -n | awk ‘/^tcp/ {++S[$NF]};END {for(a in S) print a, S[a]}’ 或
    netstat -n | awk ‘/^tcp/ {++state[$NF]}; END {for(key in state) print key,"	",state[key]}’
    netstat -n | awk ‘/^tcp/ {++arr[$NF]};END {for(k in arr) print k,"t",arr[k]}’
    
    netstat -n |awk ‘/^tcp/ {print $NF}’|sort|uniq -c|sort -rn
    
    netstat -ant | awk ‘{print $NF}’ | grep -v ‘[a-z]‘ | sort | uniq -c

    28、查找请求数前20个IP(常用于查找攻来源):

    [root@VM_0_5_centos 24]# vi 28.sh
    [root@VM_0_5_centos 24]# cat 28.sh 
    echo "出现次数 IP地址"
    echo "第一种方法:"
    netstat -anlp|grep 80|grep tcp|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -n20
    echo "第二种方法:"
    netstat -ant |awk '/:80/{split($5,ip,":");++A[ip[1]]}END{for(i in A) print A[i],i}' |sort -rn|head -n20
    
    [root@VM_0_5_centos 24]# sh 28.sh 
    出现次数 IP地址
    第一种方法:
          3 0.0.0.0
          1 122.96.40.191
    第二种方法:
          3 0.0.0.0
          1 122.96.40.191 

    29、用tcpdump嗅探80端口的访问看看谁最高

    [root@VM_0_5_centos 24]# vi 29.sh 
    [root@VM_0_5_centos 24]# cat 29.sh 
    tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr |head -20
    
    
    [root@VM_0_5_centos 24]# sh 29.sh 
    tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
    listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes

    30、查找较多time_wait连接

    [root@VM_0_5_centos 24]# vi 30.sh
    [root@VM_0_5_centos 24]# cat 30.sh 
    netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20
    [root@VM_0_5_centos 24]# sh 30.sh 
          1 193.201.224.232:8462
          1 193.201.224.232:39595 

    获取脚本

    注:所有脚本均可通过关注右侧公众号,后台回复"shell编程练习"获取百度网盘链接。

  • 相关阅读:
    20210718力扣第249场周赛(五)
    统计学习方法第三章 k近邻法
    统计学习方法第二章
    启发式搜索
    洛谷P2015 二叉苹果树
    Codeforces Round #736 (Div. 2). D
    牛客多校第五场 K King of Range
    RMQ/ST表
    AcWing100 IncDec Sequence
    AcWing 368. 银河
  • 原文地址:https://www.cnblogs.com/mmzs/p/9430335.html
Copyright © 2020-2023  润新知