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

相关推荐

  • 不重启添加硬盘

    添加硬盘后 ls /dev/sd* 不重启添加硬盘 [root@localhost ~]# cd /sys/class/scsi_host/ [root@localhost scsi_host]# ls host0  host1  host2 [root@localhost scsi_host]# echo “- – -”…

    Linux干货 2017-10-21
  • LINUX的初探

    一般而言,计算机的组成遵循着“冯诺依曼体系结构”。这种结构的体现实质上是一种存储程序的原理,把程序本身当作数据来对待,程序和该程序处理的数据用同样的方式来存储。这种结构的要点是:计算机的数制采用二进制;计算机应该按照程序顺序执行。

    2018-03-04
  • 一起学DNS系列(十三)图文详说A、CNAME、MX和NS记录

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://jeffyyko.blog.51cto.com/28563/226194    前面用了12个小节对DNS的基础、以及Windows的DNS系统作了较详细的描述,下面的几节主要是说一些有关DNS应用方面…

    2015-03-18
  • 自制linux系统

    自制linux系统  需要为虚拟机新增一个硬盘,作为自制linux系统的载体。也可以用U盘、移动硬盘或其他设备作为载体实验步骤: (1)为虚拟机新增一个大小为20G的硬盘(2)给新增的磁盘分区,划分/boot、swap、/三个分区比如,将第一个分区当作boot,大小为500M;第二个分区当作swap分区,大小为2G;第三个分区用作/,大小为10G。…

    Linux干货 2016-09-13
  • 运维自动化之系统安装

    自动化安装系统,cobbler的安装使用

    Linux干货 2018-01-15
  • Linux中的文本处理工具—sed

    sed 处理文本工具 一、概述   Stream EDitor,行编辑器    sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文…

    Linux干货 2016-08-10