Linux文本处理工具实践

  1. 统计/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来
[root@oracle ~]# cat passwd | grep /sbin/nologin | wc -l
29
[root@oracle ~]# cat passwd | grep /sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
rtkit:x:499:499:RealtimeKit:/proc:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
saslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin

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

[root@oracle ~]# cat passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
rtkit:x:499:499:RealtimeKit:/proc:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
saslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
i2:x:500:500::/home/i2:/bin/bash
oracle:x:501:501::/home/oracle:/bin/bash
test3:x:502:503::/home/test3:/bin/bash
tom:x:503:504::/home/tom:/bin/bash
jack:x:504:505::/home/jack:/bin/bash
mageia:x:1100:1100::/home/linux:/bin/bash
slackware:x:2002:2019::/home/slackware:/bin/tcsh
[root@oracle ~]# TESTUID=`cat passwd | cut -d: -f3 | sort -g | tail -n 1`; cat passwd | grep "$TESTUID" | cut -d: -f1,3,7
nfsnobody:65534:/sbin/nologin

或者精简为

[root@oracle ~]# getent passwd  | sort -t: -k3 -n | tail -1 | cut -d: -f1,3,7
nfsnobody:65534:/sbin/nologin

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

[root@oracle ~]# netstat -nt | grep -v ffff | grep tcp | tr -s " " | cut -d" " -f5 | cut -d: -f1| sort | uniq -c
     14 127.0.0.1
     17 192.168.97.135
      5 192.168.97.185
      9 192.168.97.186
      1 192.168.97.255
      1 192.168.99.161

备注:因为同时存在ipv4和ipv6的ip:port的连接存在,不得已通过grep -v ffff过滤出ipv4, 单独计数

[root@oracle ~]# netstat -nt | grep ffff | grep tcp | tr -s " "| cut -d" " -f4 | cut -d: -f1-4 | uniq -c
      5 ::ffff:192.168.97.185

再合并

[root@oracle ~]# (netstat -nt | grep -v ffff | grep tcp | tr -s " " | cut -d" " -f5 | cut -d: -f1| sort | uniq -c) > ipv4; ( netstat -nt | grep ffff | grep tcp | tr -s " "| cut -d" " -f4 | cut -d: -f1-4 | uniq -c ) > ipv6 ; cat ipv4 ipv6 | sort -nr
     17 192.168.97.135
     15 127.0.0.1
      9 192.168.97.186
      5 ::ffff:192.168.97.185
      5 192.168.97.185
      1 192.168.99.161
      1 192.168.97.255

4、编写脚本 createuser.sh,实现如下功能:使用一个用户名做为参数,如果 指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等 信息

#!/bin/bash
#Release:       Version 1.0
#***************************************
#Author:        yanj
#Mail:          289433340@qq.com
#Date:          2020-05-10
#FileName:      test.sh
#Copyright (c): 2020 All rights reserved
#***************************************

read -p "Please input the username you want to create: "  NAME
echo "Your input is $NAME"
grep -q $NAME /etc/passwd  &&  ( echo Sorry, this username has already existed, please try a new name. ) || (  useradd $NAME ; id $NAMEpasswd $NAME )

简单验证

[root@oracle ~]# ./test.sh
Please input the username you want to create: a80
Your input is a80
uid=2044(a80) gid=2047(a80) groups=2047(a80)
Changing password for user a80.
New password: 
BAD PASSWORD: it is too simplistic/systematic
BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@oracle ~]# ./test.sh
Please input the username you want to create: a80
Your input is a80
Sorry, this username has already existed, please try a new name.

5、编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等

set tabstop=4
set softtabstop=4
set shiftwidth=4
set expandtab
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,"#Release:       Version 1.0")
        call setline(3,"#***************************************")
        call setline(4,"#Author:        yanj")
        call setline(5,"#Mail:          289433340@qq.com")
        call setline(6,"#Date:          ".strftime("%Y-%m-%d"))
        call setline(7,"#FileName:      ".expand("%"))
        call setline(8,"#Copyright (c): ".strftime("%Y")." All rights reserved")
        call setline(9,"#***************************************")
        call setline(10,"")
endif
endfunc
autocmd BufNewFile * normal G

[root@oracle ~]# vim test.sh

#!/bin/bash
#Release:       Version 1.0
#***************************************
#Author:        yanj
#Mail:          289433340@qq.com
#Date:          2020-05-10
#FileName:      test.sh
#Copyright (c): 2020 All rights reserved
#***************************************
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值