第三周
Posted amy_itx_aps
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第三周相关的知识,希望对你有一定的参考价值。
第三周
1、统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来
方法1
[root@centos79 ~]# grep -iEv \'/sbin/nologin\' passwd|cut -d\':\' -f1,7|sort |uniq -c
1 gonghairong:/bin/bash
1 halt:/sbin/halt
1 root:/bin/bash
1 shutdown:/sbin/shutdown
1 sync:/bin/sync
1 user1:/bin/bash
1 user2:/bin/bash
1 user3:/bin/bash
方法2
[root@centos79 ~]# grep -ivE "/sbin/nologin$" passwd |awk -F ":" \'{print $1,$NF}\'|sort |uniq -c
1 gonghairong /bin/bash
1 halt /sbin/halt
1 root /bin/bash
1 shutdown /sbin/shutdown
1 sync /bin/sync
1 user1 /bin/bash
1 user2 /bin/bash
1 user3 /bin/bash
2、查出用户UID最大值的用户名、UID及shell类型
方法1
[root@centos79 ~]# sort -t\':\' -k 3 -nr passwd |cut -d\':\' -f1,3,7|head -n 1
nfsnobody:65534:/sbin/nologin
方法2
[root@centos79 ~]# awk -F ":" \'{print $1,$3,$7}\' passwd | tail -n 1
nfsnobody 65534 /sbin/nologin
3、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
[root@centos79 ~]# ss -nt|grep -vi "state"|grep -E "([0-9]{1,3}\\.){3}[0-9]{1,3}:[0-9]+[[:space:]]*$"|grep -Eo "([0-9]{1,3}\\.){3}[0-9]{1,3}"|sort -nr|uniq -c
9 192.168.80.1
9 192.168.80.3
4、编写脚本disk.sh,显示当前硬盘分区中空间利用率最大的值
#[root@centos79 data]# df -h|grep -iE "^/dev/" | grep -Eo "[0-9]+%"|grep -Eo "[0-9]+"|sort -rn |head -n 1
100
#df -h |tr -s \' \'| cut -d" " -f5| grep -Eo "[0-9]+" |sort -rn |head -n 1
100
[root@centos79 ~]# vim disk.sh
#!/bin/bash
#=====================================================================================================
#File Name: disk.sh
#Date: 2021-09-02 20-56-07
#Author: Create by gong hai rong
#Description: This script function is
#Shell Version: GNU bash version 4.1.2(2)-release x86_64-redhat-linux-gnu
#Copyright (C): 2021 All rights reserved
#=====================================================================================================
warning=85
RED_COLOR=\'\\E[1;31m\'
GREEN_COLOR=\'\\E[1;32m\'
YELOW_COLOR=\'\\E[1;33m\'
BLUE_COLOR=\'\\E[1;34m\'
PINK=\'\\E[1;35m\'
RES=\'\\E[0m\'
c00=\'\\033[40;32m\'
c01=\'\\033[40;31m\'
rq=$(date +%Y-%m-%d\'-\'%H-%M-%S)
export TIME_STYLE=\'+%Y-%m-%d %H:%M:%S\'
export TMOUT=864000
export LANG=en_US.UTF-8
#df -h|grep -iE "^/dev/" | grep -Eo "[0-9]+%"|grep -Eo "[0-9]+"|sort -rn |head -n 1
diskused_max=$(df -h |tr -s \' \'| cut -d" " -f5| grep -Eo "[0-9]+" |sort -rn |head -n 1)
diskused_list=$(df -h |tr -s \' \'| cut -d" " -f5| grep -Eo "[0-9]+" |sort -rn )
echo
echo -e "$RED_COLOR$rq disk used max: $diskused_max%$RES"
echo
#echo -e "disk used list : $diskused_list"
#disk_used_list=$(df -h |tr -s \' \'|grep -i "^/dev*"|cut -d " " -f1,5,6)
echo -e "${GREEN_COLOR}disk used list\\n`df -h |tr -s \' \'|grep -i "^/dev"|cut -d " " -f1,5,6|tee u.log` $RES"
echo
#df -h |tr -s \' \'|grep -i "^/dev"|cut -d " " -f1,5,6>u.log
while read i
do
{
dev=$(echo "$i"|cut -d\' \' -f1)
num=$(echo "$i"|cut -d\' \' -f2|cut -d\'%\' -f1)
mp=$(echo "$i"|cut -d\' \' -f3)
if [ "$num" -ge "$warning" ];then
#echo -e "${RED_COLOR} 磁盘空间严重告警 快快处理 老板要扣钱了 $RES "
echo -e "${RED_COLOR}$rq $dev $pm used: $num 磁盘空间严重告警 快快处理 老板要扣钱了 $RES"
fi
}&
done<u.log
wait
#echo ok
5、编写脚本 systeminfo.sh,显示当前主机系统信息,包括:主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小
[root@centos79 data]#vim systeminfo.sh
#!/bin/bash
#=====================================================================================================
#File Name: systeminfo.sh
#Date: 2021-09-02 20-53-09
#Author: Create by gong hai rong
#Description: This script function is
#Shell Version: GNU bash version 4.1.2(2)-release x86_64-redhat-linux-gnu
#Copyright (C): 2021 All rights reserved
#=====================================================================================================
ED_COLOR=\'\\E[1;31m\'
GREEN_COLOR=\'\\E[1;32m\'
YELOW_COLOR=\'\\E[1;33m\'
BLUE_COLOR=\'\\E[1;34m\'
PINK=\'\\E[1;35m\'
RES=\'\\E[0m\'
c00=\'\\033[40;32m\'
c01=\'\\033[40;31m\'
#hostname
host_name0=$(hostname)
host_name1=$HOSTNAME
host_name2=`uname -m`
host_name3=$(sed -n \'p\' /etc/hostname )
hostname_name4=$(cat /etc/hostname)
hostname5=$(hostnamectl status | grep -i "Static hostname" |tr -s \'\'| cut -d":" -f2 |grep -o "\\<.*\\>")
## ipaddress
ipaddress0=$(ifconfig ens32 |grep -i "^.*inet\\>.*\\>"|grep -io "[0-9]\\{1,3\\}[.][0-9]\\{1,3\\}[.][0-9]\\{1,3\\}[.][0-9]\\{1,3\\}" |head -n 1)
ipaddress1=$(ifconfig ens32 |grep -i "^.*inet\\>.*\\>"|grep -io "[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}" |head -n 1)
ipaddress2=$(ifconfig ens32| grep ".*inet\\>"|grep -io "\\([0-9]\\{1,3\\}[.]\\)\\{3\\}[0-9]\\{1,3\\}"|head -n 1)
ipaddress3=$(ifconfig ens32|grep -i ".*inet\\>"|grep -iEo "[0-9]+.[0-9]+.[0-9]+.[0-9]+" |head -n 1)
ipaddress4=$(ifconfig ens32 |grep -i ".*inet\\>"|grep -iEo "([0-9]+[.]){1,3}[0-9]+"|head -n 1)
osversion=$(cat /etc/redhat-release )
kernelversion=$(uname -r)
cpu=$(lscpu |sed -n \'/Model name/Ip\'|tr -s \' \'|cut -d\':\' -f2)
mem=$(free -h|tr -s \' \' |grep -iEo "[0-9]+m.*"|cut -d\' \' -f1)
disk=$(lsblk |grep \'^sd\' |tr -s \' \' |cut -d " " -f1,4)
echo -e "$GREEN_COLOR=======================================echo $(date +%Y-%m-%d\'-\'%H-%M-%S) System Info ===================================================================$RES\\t\\n"
echo -e "IPADDRESS: $ipaddress0"
echo -e "OSVERSION: $osversion"
echo -e "KERNEL: $kernelversion"
echo -e "CPU: $cpu"
echo -e "MEMORY: $mem\\t"
echo -e "DISK:\\n$disk"
echo -e "$GREEN_COLOR=======================================echo $(date +%Y-%m-%d\'-\'%H-%M-%S) System Info ===================================================================$RES\\t\\n"
printf "IPADDRESS:%-10s$ipaddress0\\n"
printf "OSVERSION:%-10s$osversion\\n"
printf "KERNEL:%-10s $kernelversion\\n"
printf "10sCPU:%-10s $cpu\\n"
printf "MEMORY:%-10s $mem\\n"
printf "DISK:%-10s\\n$disk\\n"
echo -e "$GREEN_COLOR=======================================printf $(date +%Y-%m-%d\'-\'%H-%M-%S) System Info ===================================================================$RES\\t\\n"
6、20分钟内通关vimtutor(可参考https://yyqing.me/post/2017/2017-02-22-vimtutor-chinese-summary)
以上是关于第三周的主要内容,如果未能解决你的问题,请参考以下文章