第三周(3):课堂练习与作业

课堂练习:

1、找出ifconfig命令结果中本机的所有IPv4地址

[root@centos6 ~]# ifconfig | tr -cs '[0-9].' '\n'|sort -ut. -k3n

127.0.0.1
10.1.255.255
10.1.255.76

2、查出分区空间使用率的最大百分比值

[root@centos6 ~]# df | tr -s ' ' ':' |cut -d : -f5|tr -d % |sort -rn |head -n1
22

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

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

4、查出/tmp的权限,以数字方式显示

[root@centos6 ~]# stat /tmp | cut -d: -f2|head -4|tail -1|tr -cd '0-9\n'
1777

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

[root@centos6 ~]# netstat -nt | tr -cs '[0-9].' '\n'|sort -ut. -k3nr
10.1.255.76
10.1.250.48

6、显示/proc/meminfo文件中以大小s开头的行;(要求:使用两种方式)

[root@centos6 ~]# grep '^[S|s]' /proc/meminfo
[root@centos6 ~]# grep -i '^s' /proc/meminfo

7、显示/etc/passwd文件中不以/bin/bash结尾的行

[root@centos6 ~]# grep -v "/bin/bash$" /etc/passwd

8、显示用户rpc默认的shell程序

[root@centos6 ~]# grep "^rpc\b" /etc/passwd | cut -d: -f7

9、找出/etc/passwd中的两位或三位数

grep -o "[[:digit:]]\{2,3\}" /etc/passwd

10、显示/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面存非空白字符的行

grep '^[[:space:]]\+[^[:space:]]' /etc/grub.conf   #centos6已"grub.conf"文件为参考

11、找出"netstat -tan"命令的结果中以'LISTEN'后跟0、1或多个空白字符结尾的行

[root@centos6 ~]# netstat -tan |grep 'LISTEN[[:space:]]*$'

12、添加用户bash、testbash、basher以及nologin(其shell为/sbin/nologin),而后找出/etc/passwd文件中用户名同shell名的行

[root@centos6 ~]# useradd bash
[root@centos6 ~]# useradd testbash
[root@centos6 ~]# useradd basher
[root@centos6 ~]# useradd -s /sbin/nologin nologin
[root@centos6 ~]# grep "^\([[:alnum:]]\+\>\).*\1$" /etc/passwd

13、显示当前系统root、mage或wang用户的UID和默认shell

[root@centos6 ~]# egrep '^(root|mage|wang)\b' /etc/passwd

14、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行

[root@centos6 ~]# egrep '^[[:alpha:]_]+\(\)' /etc/rc.d/init.d/functions

15、使用egrep取出/etc/rc.d/init.d/functions中其基名

[root@centos6 ~]# echo "/etc/rc.d/init.d/functions" | egrep -o "[^/]+/?$"

16、使用egrep取出上面路径的目录名

[root@centos6 ~]# echo "/etc/rc.d/init.d/functions" | egrep -o "^/.*/+\<"

17、统计以root身份登录的每个远程主机IP地址的登录次数

[root@centos6 ~]# last | grep -E -o '^root\>.*([[:digit:]]\.){3}[[:digit:]]' | tr -s '' |cut -d ' ' -f3 |sort|uniq -c

18、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255

[root@centos6 ~]# seq 0 255 |egrep '[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]' | tr -cs '0-9' '\t'

19、显示ifconfig命令结果中所有IPv4地址  

ifconfig |grep -E -o  '(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'

作业:

1、取本机ip地址

ifconfig |grep -E -o  '(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])' |head -1

2、取各分区利用率的数值

[root@centos6 ~]# df | tr -s ' ' '|' |cut -d '|' -f5

3、统计/etc/init.d/functions 文件中每个单词出现的次数,并按频率从高到低显示

[root@centos6 ~]# cat /etc/init.d/functions | tr -cs '[:alpha:]' '\n' | sort | uniq -c | sort -nr

4、/etc/rc.d/init.d/functions或/etc/rc.d/init.d/functions/"  取目录名

echo "/etc/rc.d/init.d/functions" | egrep -o '^/.*/\<'

5、正则表达式表示身份证号(以第二代身份证为参考)

blob.png

grep -E -o -i '[1-6][0-7][0-9]{2}[0-4][0-9]((1[89][0-9]{2})|(200[0-9]|201[0-5]))(((0[13578]|1[02])([0-2][0-9]|3[01]))|((0[469]|1[02])([0-2][0-9]|30))|02[012][0-9])[0-9]{3}[0-9x]'

6、正则表达式表示手机号

blob.png

[root@centos6 ~]# grep -E -o '1(3|4|5|7|8|9)[0-9]{9}'

7、正则表达式表示邮箱(已163邮箱为例)

blob.png

[root@centos6 ~]# grep -E -o '[[:alnum:]]([[:alnum:]]|[[:punct:]]){5,17}@163\.com'

8、正则表达式表示QQ号(数字登陆)

[root@centos6 ~]# grep -E -o '[1-9][0-9]{4,10}'

原创文章,作者:Aleen,如若转载,请注明出处:http://www.178linux.com/30621

(0)
上一篇 2016-08-08 20:10
下一篇 2016-08-08 20:22

相关推荐

  • 基于Python和MoviePy库实现数据的动态展示

    基于Python和MoviePy库实现数据的动态展示 (翻译:以马内利)  原文链接:Data Animations With Python and MoviePy   Python拥有很多实现数据可视化的库,但是很少可以展示GIFs的动态视图。 这篇博客主要介绍怎样使用MoviePy库作为一个其他可视化库的通用插件。 Movi…

    2015-03-26
  • 博客

       这是我的第一篇博客!   平时学习中,总会遇到或大或小的问题,以及一些新的感悟,在当时能够十分清晰记得,但过一些时日,便发现又忘记了。以前十分明白的东西,又变得有些模糊,甚至完全忘记。因此特别需要记录下来,方便日后的查阅或分享给其他遇到类似问题的朋友。    如果以后忘记的某些知识,这时候,翻开以往博客…

    Linux干货 2017-07-11
  • RAID+LVM详解

                                        一.RAID篇 一.什么是RAID   磁盘阵列全名是『Redundant Arrays…

    Linux干货 2016-09-07
  • 用户管理、组管理、权限管理、文本处理工具应用示例

    用户管理、组管理、权限管理、文本处理工具应用示例 1.复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其他用户均没有任何访问权限 [root@localhost ~]# cp -a /etc/skel/ /home/tuser1/ [root@localhost ~]# chmod -R go= /hom…

    Linux干货 2017-07-23
  • 第二十周作业

    1、用Keepalived实现nginx与lvs的高可用集群; lvs+keepalived: 1)后端两台rs上安装web服务并创建探测页面 ~]# yum install nginx -y ~]# systemctl start nginx.service ~]# vim /usr/share/nginx/html/index.html <h1&g…

    2017-07-03
  • VRRP协议

    VRRP协议 Virtual Router Redundancy Protocol 虚拟路由器冗余协议。 Vrrp协议在linux上的实现(软件实现)是keepalived。 VRRP简单的讲,就是心跳信息。 主节点不断的向备用节点广播信息,信息包含自己的心跳和优先级。 不光如此,还可以在主节点上安装一个监控,来监控其他资源,如果这些资源运行正常,就可以给其…

    2016-11-02