第三周(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)
AleenAleen
上一篇 2016-08-08
下一篇 2016-08-08

相关推荐

  • MYSQL数据库基础教程

    一、数据库介绍 1.数据库管理系统的产生背景 (1)数据时代的到来要求对数据进行有效和安全的管理 涉及的数据量大 数据不随程序的结束而消失 数据被多个应用程序共享 大数据时代的到来 (2)传统文件系统管理上的缺陷要求改变数据管理方式 编写应用程序不方便 数据冗余不可避免 应用程序依赖性 不支持对文件的并发访问 数据间联系弱 难以按用户视图表示数据 无安全控制…

    Linux干货 2017-10-05
  • 用户权限及正则表达式

    ln –s软连接原文件可以写绝对路径或相对于软连接文件的相对路径 ln 硬链接的原文件可以写绝对路径,相对于硬链接文件的相对路径或相对于当前路径的路径(因为硬链接主要是看节点号) etc/passwd格式: username:x:uid:gid:home:shell etc/shadow格式 username:password:password age:mi…

    Linux干货 2016-08-08
  • 关于shell脚本编程基础第三篇

                          关于shell脚本编程基础第三篇   本章主要内容:循环的特殊用法;while;for;select 循环与菜单       &nbsp…

    系统运维 2016-08-21
  • 文本处理工具

    文件 查看命令:cat, tac,revcat [OPTION]… [FILE]…-E: 显示行结束符$-n: 对显示出的每一行进行 编号-A :显示所有控制符-b: : 非空行编号-s :压缩连续的空行成一行 分页查看文件内容more: 分页查看文件more [OPTIONS…] FILE…-d: 显示翻页及…

    Linux干货 2017-03-17
  • rsync3.0 (初稿 逐步完善)

    实验环境为 centos 7.2 系统   CentOS Linux release 7.2.1511 (Core)   主机  ip               10.1.250.19 客…

    Linux干货 2016-06-03
  • CentOS6下的网络信息配置

    简介     我们都知道在我们互联网中,所有的通信都通过网络来完成的,有了网络当然也要有对应的地址,MAC地址是固定不变的,所以能变的只有IP的地址,那么今天来带给大家如何在CentOS6中如何配置网卡。当然一下的所有命令都是在CentOS6中进行的。 一、网卡的配置文件    1.操作网卡的命令 …

    Linux干货 2017-03-20