第三周(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.www58058.com/30621

(0)
AleenAleen
上一篇 2016-08-08
下一篇 2016-08-08

相關推薦

  • keepalived雙主模型的實現

    一、簡介  keepalived 簡介:是服務器高可用的一個重要軟件,它的核心組件有vrrp ,stack, checker ,ipvs, warpper, watch dog  它是vrrp協議的實現,原生設計目的為高可用ipvs服務;keepalived能夠通過配置文件中定義生成ipvs規則 并能夠對RS的健康狀態進行檢測;vrrp_…

    Linux干貨 2016-08-30
  • 馬哥教育網絡班20期第2周課程練習

    一、Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關  示例演示。 linux上文件管理命令包括: ls,cat,pwd,cp,rm,cd,head,tail,more,less,cut,which,whereis,find,mkdir,mv; 1、ls 命令;顯示文件/文件夾清單   例如:如下顯示列出根目錄下的文件及目錄…

    Linux干貨 2016-06-23
  • 馬哥教育網絡班20期+第6周課程練習

    1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#; cp /etc/rc.d/rc.sysinit /tmp sed 's/^[[:space:]]/#&/' /tmp/rc.sysinit 2、…

    Linux干貨 2016-08-15
  • 第6天預習xargs命令的用法

    Xargs用法詳解 1. 簡介   之所以能用到這個命令,關鍵是由于很多命令不支持|管道來傳遞參數,而日常工作中有有這個必要,所以就有了xargs命令,例如: find /sbin -perm +700 |ls -l     &n…

    Linux干貨 2016-08-05
  • 計算機組成及Linux入門知識

    計算機的基本組成: 存儲器:     實現記憶功能的部件用來存放計算程序及參與運算的各種數據 運算器:     負責數據的算術運算和邏輯運算即數據的加工處理 控制器:     負責對程序規定的控制信息進行分析,控制并協調輸入,輸出操作或內存訪問 輸入設備:    實現計算程序和原始數據的輸入 輸出設備:    實現計算結果輸出 組成的聯系: 圖一 圖二 計算…

    Linux干貨 2016-09-16
  • 簡述一些基礎指令

    tree命令 tree -L 1 -d /用來查看目錄結構 -L 指定層數 -d 只查看目錄 /boot 跟內核有關的文件 grub 內核和BootLoader ├── bin 存放用戶使用的基本命令(可執行程序,二進制文件) 不能單獨分區的 ├── boot 跟內核有關的文件 grub 內核和BootLoader&n…

    Linux干貨 2017-04-03
欧美性久久久久