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])' 或


'[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' 或

 

(\<([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]\>))

 


正则表达式表示×××号

egrep "\<((1[1-5])|(2[1-3])|(3[1-7])|(4[1-6])|(5[0-4])|(6[1-5])|(71|81|82))([0-9]){4}(19|20)([0-9]){2}((0[1-9])|(1[0-2]))(0[1-9]|([0-9])|(2[0-9])|(3[0-1]))([0-9]){3}([0-9]|X)\>"

 


正则表达式表示手机号

grep -E -o "(\+86)?1[38][0-9]{9}|14[57][0-9]{8}|15[0-35-9][0-9]{8}|17[0678][0-9]{8}"
grep -E -o "^\<1[3578][0-9]{9}\>"

 


正则表达式表示邮箱

egrep "\<([[:alnum:]]+(-|_)*[[:alnum:]]*)\>@([[:alnum:]]+\.)+[[:alnum:]]+" filename
grep -E -o '[a-zA-Z0-9]+[[:alnum:]\.\_-]*@[a-zA-Z0-9]+[[:alnum:]\.\_-]*' filename

 


正则表达式表示QQ号

grep -E -o '\b[1-9][0-9]{4,12}\b' filename