Linux 第七天: (08月05日) 練習和作業
1 找出ifconfig命令結果中本機的所有IPv4地址
ifconfig | tr -cs '[0-9].''\n' | sort -ut '.' -k3 -n 或
ifconfig | head -2 | tail -1 | cut -d: -f2 | cut -d " " -f1
2 查出分區空間使用率的最大百分比
df -h |tr -s ' ' ':'|cut -d: -f5 |tr -d % | sort -n
3 查出用戶UID最大值得用戶名,UID及shell類型
getent passwd | sort -t: -k3 -nr|cut -d: -f1,3,7|tail -1
4 查出/tmp權限, 以數字顯示
stat /tmp |head -4 |tail -1 |tr " " "\n" |head -2 |tail -1|tr -dc '[:digit:]'
5 統計當前連接本機的每個遠程主機IP連接數, 從大到小排序
netstat -nt|tr -s ' ' '|' |cut -d '|' -f5 |cut -d: -f1 |uniq -c|sort -n
統計/etc/init.d/functiongs每個單詞出現次數
cat /etc/init.d/functiongs | tr -cs '[:alpha:]''\n' | sort | uniq -c | sort -n
掃描網段顯示主機IP
nmap -sP -v 10.1.252.210/24 | grep -B1 up | grep -v up
1 顯示/proc/meminfo 文件中以大小s開頭的行 (兩種方法)
grep '^[sS].*' /proc/meminfo 或
grep -i '^s.*' /proc/meminfo 或
grep -e '^s.*' -e '^S.*' /proc/meminfo
2 顯示/etc/paswd 文件中不以/bin/bash結尾的行
grep -v /bin/bash& /etc/passwd
3 顯示用戶rpc默認的shell程序
grep '^rpc\>' /etc/passwd | cut -d : -f 7
4 找出/etc/passwd 文件中的兩位或三位數
grep "\<[1-9][0-9][0-9]\?\>" /etc/passwd 或
grep "\<[1-9][0-9]\{1,2\}\>" /etc/passwd 或
grep -w "[1-9][0-9][0-9]\?" /etc/passwd
5 顯示/etc/grup2.cfg文件中至少一個空白字符開頭的且后面有非空白字符的行
grep "^[[:space]]\+[^[:space:]].*" /etc/grub2.cfg
6 找出"netstat -tan"命令結果中以'LISTEN'后跟任意空白字符結尾的行
netstat -tan | grep 'LISTEN[[:space]]]*$'
7 添加用戶bash,testbash,basher,nologin(shell為/sbin/nologin),
找出/etc/passwd文件中用戶名同shell名的行
useradd bash
useradd testbash
useradd basher
useradd nologin -s /sbin/nologin
grep '^\<\(.*\)\>.*/\1$' /etc/passwd 或
grep '^\<\(.*\)\>.*\<\1\>$' /etc/passwd
1 顯示root,mage,wang,的UID和默認shell
grep -E "^\<root\>|^\<mage\>|^\<wang\>" /etc/passwd|cut -d: -f3,7 或
grep -E "^(root|wang|mage)\>" /etc/passwd|cut -d: -f3,7 或
grep -E "^(root|wang|mage):" /etc/passwd|cut -d: -f3,7
2 找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)后面跟一個小括號的行
egrep "^[[:almum:]_]+\(\)" /etc/tc.d/init.d/functions|
3 使用egrep取出/etc/rcd/initd/functions中基名
echo "/etc/rc.d/init.d/functions" | egrep -o"[^/]+/?$"
4 使用egrep取出/etc/rcd/initd/functions目錄名
echo "/etc/rc.d/init.d/functions" | egrep -o"(/.*/)"
5 統計以root身份登錄的每個遠程主機IP地址的登錄次數
last | grep -E -o "^root\>.*([[:digit:]]\.){3}[[:digit:]]" |tr -s ' ' |cut -d' ' -f3 |sort |uniq -c
6 利用擴展正則表達式分別表示0-9,10-99,100-199,200-249,250-255
([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]
7 顯示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])' 或
ifconfig | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'
正則表達式表示身份證號
^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$
正則表達式表示手機號
^[1][3|5|7|8][0-9]{9}$
正則表達式表示郵箱
^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$
正則表達式表示QQ號
[1-9][0-9]{5,11}
原創文章,作者:dengjian,如若轉載,請注明出處:http://www.www58058.com/30894