第四周-命令,shell脚本

1、统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来
[root@centos7 ~]#grep "/sbin/nologin" /etc/passwd |wc -l 
40
[root@centos7 ~]#grep "/sbin/nologin" /etc/passwd |cut -d: -f1
bin
daemon
adm
等
2、查出用户UID最大值的用户名、UID及shell类型
[root@centos7 ~]#cat /etc/passwd |sort -t: -k3 -nr|head -1|cut -d: -f1,3,7
nfsnobody:65534:/sbin/nologin

3、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
[root@centos7 ~]#netstat -nt|tr -s ' '|cut -d' ' -f5|egrep -o '[0-9.]{7,15}' |uniq -c|sort -nr
      1 192.168.37.1
4、编写脚本 createuser.sh,实现如下功能:使用一个用户名做为参数,如果 指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等 信息
[root@centos7 script]#vim createuser.sh 

#!/bin/bash
#
#********************************************************************
#Author:                iamalu
#QQ:                    2425942026
#Date:                  2020-04-03
#FileName:             createuser.sh
#URL:                   http://www.iamalu.com
#Description:          The test script
#Copyright (C):         2020 All rights reserved
#********************************************************************
[ $# -eq 0 ]&& { echo "Usage:`basename $0` USERNAME";exit 10; }
id $1 &>/dev/null && { echo "user $1 is exist!";exit 20; }
useradd $1 &>/dev/null && { echo "user $1 is created";echo 123456 |passwd --stdin $1 &>/dev/null; }|| { echo "error!";exit 30; }   

[root@centos7 script]#bash createuser.sh alice
user alice is created
[root@centos7 script]#bash createuser.sh root
user root is exist!
[root@centos7 script]#bash createuser.sh
Usage:createuser.sh USERNAME
5、编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等
[root@centos7 ~]#vim ~/.vimrc

set ignorecase                                                                              
set cursorline
set autoindent
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
        if expand("%:e") == 'sh'
        call setline(1,"#!/bin/bash")
        call setline(2,"#")
        call setline(3,"#********************************************************************") 
        call setline(4,"#Author:                iamalu")
	        call setline(5,"#QQ:                2425942026")
        call setline(6,"#Date:                  ".strftime("%Y-%m-%d"))
        call setline(7,"#FileName:             ".expand("%"))
        call setline(8,"#URL:                   http://www.iamalu.com")
        call setline(9,"#Description:          The test script")
        call setline(10,"#Copyright (C):        ".strftime("%Y")." All rights reserved")
        call setline(11,"#********************************************************************")
        call setline(12,"")
        endif
endfunc
autocmd BufNewFile * normal G
1、查找/etc目录下大于1M且类型为普通文件的所有文件
[root@centos7 ~]#find /etc \( -size +1M -a -type f \) -ls
1556601 8144 -r--r--r--   1 root     root      8339001 Mar 17 00:28 /etc/udev/hwdb.bin
202918288 1384 -rw-r--r--   1 root     root      1415647 Mar 17 00:23 /etc/selinux/targeted/contexts/files/file_contexts.bin
135123457 3784 -rw-r--r--   1 root     root      3874070 Mar 17 00:23 /etc/selinux/targeted/policy/policy.31
70321685 3784 -rw-------   1 root     root      3874070 Mar 17 00:23 /etc/selinux/targeted/active/policy.kern
70321691 3784 -rw-------   1 root     root      3874070 Mar 17 00:23 /etc/selinux/targeted/active/policy.linked
202396823 1336 -rw-r--r--   1 root     root      1367395 Apr 11  2018 /etc/brltty/zh-tw.ctb
2、打包/etc/目录下面所有conf结尾的文件,压缩包名称为当天的时间,并拷贝到/usr/local/src目录备份。
[root@centos7 ~]#find /etc -name "*.conf" |xargs tar -zcvf /usr/local/src/`date +%F`.tar.gz 
tar: Removing leading `/' from member names
/etc/resolv.conf
/etc/fonts/conf.d/65-0-lohit-bengali.conf
/etc/fonts/conf.d/61-urw-fallback-backwards.conf
/etc/fonts/conf.d/66-ucs-miscfixed.conf
/etc/fonts/conf.d/31-cantarell.conf
/etc/fonts/conf.d/57-paratype-pt-sans.conf
/etc/fonts/conf.d/59-liberation-sans.conf
/etc/fonts/conf.d/65-0-khmeros-base.conf
/etc/fonts/conf.d/20-unhint-small-dejavu-sans.conf

[root@centos7 ~]#ll /usr/local/src
total 204
-rw-r--r-- 1 root root 207436 Apr  4 18:51 2020-04-04.tar.gz
1、显示统计占用系统内存最多的进程,并排序。
以下几种写法都可以:
ps aux --sort=-%mem 
ps aux --sort=-rss
ps aux --sort=-rssize
ps aux --sort=-rsz
ps aux | sort -k4nr
[root@centos7 scripts]# ps aux --sort -rss |head -5
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
wang       2538  0.7 10.1 3504552 188596 ?      Sl   03:02   0:18 /usr/bin/gnome-shell
wang       2852  0.2  5.1 1187164 96608 ?       Sl   03:02   0:04 /usr/libexec/gnome-initial-setup --existing-user
wang       2886  0.1  3.3 1363360 63148 ?       Sl   03:02   0:04 /usr/bin/gnome-software --gapplication-service
root       1345  0.1  3.2 357172 61028 tty1     Ssl+ 02:56   0:04 /usr/bin/X :0 -background none -noreset -audit 4 -verbose -auth /run/gdm/auth-for-gdm-bVEpPH/database -seat seat0 -nolisten tcp vt1
2、编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"
[root@centos7 scripts]# vim for_scanip.sh

#!/bin/bash
NETID=192.168.0.
for HOSTID in {1..254};do
{
        if /bin/ping -c1 -W1 $NETID$HOSTID >/dev/null ;then
                echo "$NETID$HOSTID is success!"
        else
                echo "$NETID$HOSTID is fail!"
        fi
} &
done
wait
[root@centos7 scripts]# bash for_scanip.sh 
192.168.0.1 is fail!
192.168.0.3 is fail!
192.168.0.6 is fail!
192.168.0.7 is fail!
192.168.0.2 is fail!
.....
192.168.0.248 is fail!
192.168.0.241 is fail!
192.168.0.200 is fail!
[root@centos7 scripts]# echo 192.168.0.{1..254} |tr -s " " "\n" > ip.txt
[root@centos7 scripts]# vim while_scanip.sh

#!/bin/bash
while read ip;do
{
        /bin/ping -c1 -W1 $ip >/dev/null
        if [ $? -eq "0" ];then
                echo "$ip is success!"
        else
                echo "$ip is fail!"
        fi
} &
done </data/scripts/ip.txt
wait
[root@centos7 scripts]# bash while_scanip.sh 
192.168.0.1 is fail!
192.168.0.4 is fail!
192.168.0.5 is fail!
192.168.0.7 is fail!
.....
192.168.0.248 is fail!
192.168.0.200 is fail!
3、每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间
[root@centos7 script]#vim backup.sh 

#!/bin/bash
[ -d /backup ] || mkdir /backup
rpm -q xz >/dev/null
[ $? -eq 0 ] || yum -y install xz >/dev/null
DATEFORMAT=`date -d yesterday "+%F-%H"`
tar -cf /backup/etcbak-$DATEFORMAT.tar /etc > /dev/null && xz -z /backup/etcbak-$DATEFORMAT.tar
[root@centos7 scripts]# bash backup.sh
tar: Removing leading `/' from member names
[root@centos7 /]# ls /backup/
etcbak-2020-04-26-03.tar.xz
4、工作日时间,每10分钟执行一次磁盘空间检查,一旦发现任何分区利用率高 于80%,就发送邮件报警
[root@centos7 scripts]#vim disk_check2.sh

#!/bin/bash
#Usage_Rate=`df |sed -nr "/\/dev\/sda/s#.* +([0-9]+)% .*#\1#p" |sort -nr |head -1`
Usage_Rate=`df |awk -F '[ %]+' 'BEGIN{max = 0}{if ($5+0 > max) max=$5} END {pring max}'`
Partition_Name=`df |grep "$Usage_Rate%" |cut -d" " -f1`
if [[ $Usage_Rate -gt 80 ]];then
        echo -e "Warning!!! \n $Partition_Name's avaliable space is less than 20%" |mail -s Warning root
fi
[root@centos7 scripts]#bash disk_check2.sh 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值