第三周(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

相关推荐

  • N25-第三周作业

      alias NAME='COMMAND'           定义命令的别名—对当前用户有效  un为解除别名 Which   查看命令别名 Whereis    查看程序的文档和源码路径(-b &nbs…

    Linux干货 2016-12-18
  • sed的模式匹配用法探讨

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://nolinux.blog.51cto.com/4824967/1405753 [root@sunsky Desktop]# cat sunsky a b c d e f [root@sunsk…

    Linux干货 2016-08-15
  • cp命令浅析

      cp主要作用    1拷贝文件到指定目录    2拷贝文件到指定文件    3拷贝多个文件到指定目录    4拷贝目录到指定目录    5拷贝多个目录到指定目录 先做些准备工作 &nbs…

    2017-04-04
  • linux 用户与组管理详解

    ##用户与组的分类 Linux系统对用户分配如下: -系统管理员:root -普通用户:普通用户分为以下两种           系统用户:系统用户通常是不可登陆的,执行某些服务及进程的帐号           登录用户:一般用户,    …

    Linux干货 2017-04-05
  • Tomcat公猫

    官方站点:http://tomcat.apache.org/ Tomcat是一个能够运行java页面(jsp)的java’web服务器程序;安装使用之前需要先安装jdk包 jdk是JAVA的开发工具包,已随base收录进来,安装jdk1.8版本:yum -y install java-1.8.0-openjdk-devel (其中jvm虚拟机的运行程序就叫j…

    2017-05-23