2021-09-07 第三周

第三周作业练习

1、统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来

[root@r8-01 ~]# cat /etc/passwd | grep "nologin$" | cut -d":" -f 1
bin
daemon
adm
lp
mail
operator
games
ftp
nobody
dbus
systemd-coredump
systemd-resolve
tss
polkitd
unbound
sssd
sshd
postfix
[root@r8-01 ~]#

2、查出用户UID最大值的用户名、UID及shell类型

[root@r8-01 ~]# cat /etc/passwd | sort -t":" -nk 3 | tail -1 | cut -d":" -f 1,3,7
nobody:65534:/sbin/nologin
[root@r8-01 ~]#
[root@r8-01 ~]#

3、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序

[root@r8-01 ~]# ss -nt | tr -s " " | cut -d " " -f 5 | grep -Eo "([0-9]+\.){1,3}[0-9]+" | sort -r  | uniq -c
      2 10.48.4.7
      2 10.48.0.1
[root@r8-01 ~]#

4、编写脚本disk.sh,显示当前硬盘分区中空间利用率最大的值

[root@r8-01 ~]# cat disk.sh
#!/bin/bash

#------------------
#Filename:      disk.sh
#Author:        L
#DateTime:      2021-09-11
#--------------------------

CMD=$(df | sed -rn '/^\/dev/s/.*\s+([0-9]+)%.*/\1/p' | sort -nr | head -n1)

echo -e "Maximum utilization:  $CMD%"
[root@r8-01 ~]#
[root@r8-01 ~]# ./disk.sh
Maximum utilization:  61%
[root@r8-01 ~]#

5、编写脚本 systeminfo.sh,显示当前主机系统信息,包括:主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小

[root@r8-01 ~]# cat systeminfo.sh
#!/bin/bash
#---------------------------
#FileName:      systeminfo.sh
#Author:        L
#Date:          2021-09-11
#Description:   Display systeminfo
#---------------------------

#-----------Host systeminfo------------
#HOSTNAME:      `hostname`
#IPADDR:        `hostname -I`
#OSVERSION:     `cat /etc/os-release | sed -rn "/PRETTY/s/PRETTY.*=\"(.*)\"/\1/p"`
#KERNEL:        `uname -r`
#CPU:           `lscpu | sed -nr "/Model name:/s/.*:\s+(.*)/\1/p"`
#MEMORY:        `lsmem | sed -nr "/online memory/s/.*:\s+(.*)/\1/p"`
#DISK:          `lsmem | sed -nr "/online memory/s/.*:\s+(.*)/\1/p"`
#--------------------------------------


GREEN="echo -e \e[1;32m"
RED="\e[1;31m"
END="\e[0m"

declare -A CMD
CMD[HOSTNAME]=$(hostname)
CMD[IPADDR]=$(hostname -I)
CMD[OSVERSION]=$(cat /etc/os-release | sed -rn "/PRETTY/s/PRETTY.*=\"(.*)\"/\1/p")
CMD[KERNEL]=$(uname -r)
CMD[CPU]=$(lscpu | sed -nr "/Model name:/s/.*:\s+(.*)/\1/p")
CMD[MEMORY]=$(lsmem | sed -nr "/online memory/s/.*:\s+(.*)/\1/p")
CMD[DISK]=$(lsblk | grep '^nvme0' | tr -s ' ' | cut -d' ' -f4)

$GREEN----------------------Host Systeminfo------------------------------$END
echo -e "HOSTNAME:      ${RED}${CMD[HOSTNAME]}$END"
echo -e "IPADDR:        ${RED}${CMD[IPADDR]}$END"
echo -e "OSVERSION:     ${RED}${CMD[OSVERSION]}$END"
echo -e "KERNEL:        ${RED}${CMD[KERNEL]}$END"
echo -e "CPU:           ${RED}${CMD[CPU]}$END"
echo -e "MEMORY:        ${RED}${CMD[MEMORY]}$END"
echo -e "DISK:          ${RED}${CMD[DISK]}$END"
$GREEN-------------------------------------------------------------------$END

[root@r8-01 ~]#
[root@r8-01 ~]# ./systeminfo.sh
----------------------Host Systeminfo------------------------------
HOSTNAME:      r8-01
IPADDR:        10.48.4.8 192.168.1.123
OSVERSION:     Red Hat Enterprise Linux 8.1 (Ootpa)
KERNEL:        4.18.0-147.el8.x86_64
CPU:           Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
MEMORY:        2G
DISK:          4G
-------------------------------------------------------------------
[root@r8-01 ~]#

6、20分钟内通关vimtutor(可参考https://yyqing.me/post/2017/2017-02-22-vimtutor-chinese-summary)

上一篇:Proguard混淆之后行数LineNumber对不上


下一篇:沁恒CH573开发板上手