[root@oldboyedu-lnb ~]# netstat -lntp /查看网卡配置信息
[root@oldboyedu-lnb ~]# netstat -lntp|grep ‘sshd’ /过滤sshd的信息
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 7227/sshd
tcp6 0 0 :::22 ::😗 LISTEN 7227/sshd
[root@oldboyedu-lnb ~]# grep -n -A2 ‘Failed’ /var/log/secure /向下打印两行
[root@oldboyedu-lnb ~]# grep -n -B2 ‘Failed’ /var/log/secure /向上打印两行
[root@oldboyedu-lnb ~]# grep -n -C2 ‘Failed’ /var/log/secure / 上下各两行
例题1.过滤/etc/passwd以nologin结尾的,看有多少行
[root@oldboyedu-lnb ~]# grep 'nologin' /etc/passwd |wc -l
19
例题2.使用ifconfig获取当前的IP地址,使用sed.awk,grep获取ip地址
[root@oldboyedu-lnb ~]# ifconfig ens33|grep 'inet '
inet 10.0.0.200 netmask 255.255.255.0 broadcast 10.0.0.255
[root@oldboyedu-lnb ~]# ifconfig ens33|grep 'inet '|awk '{print $2}'
10.0.0.200
root@oldboyedu-lnb ~]# ifconfig ens33|awk '/inet /'
inet 10.0.0.200 netmask 255.255.255.0 broadcast 10.0.0.255
[root@oldboyedu-lnb ~]# ifconfig ens33|awk '/inet /'|awk '{print $2}'
10.0.0.200
[root@oldboyedu-lnb ~]# ifconfig ens33|awk '/inet / {print $2}'
10.0.0.200
例题3.将root和/bin/bash位置交换
[root@oldboyedu-lnb ~]# cat /etc/passwd
[root@oldboyedu-lnb ~]# ifconfig ens33 |awk ‘NR2’ #NR代表行号
inet 10.0.0.200 netmask 255.255.255.0 broadcast 10.0.0.255
[root@oldboyedu-lnb ~]# ifconfig ens33 |awk 'NR2 {print $2 }’
10.0.0.200
[root@oldboyedu-lnb ~]# ifconfig ens33 |sed -n ‘2p’|awk ‘{print $2}’
10.0.0.200
例题5.查找bytes
[root@oldboyedu-lnb ~]# ifconfig |awk 'NR==5 {print $4}'
byte
例题6.
[root@oldboyedu-lnb ~]# sed -r 's#^.*//(.*)/.*$#\1#g'
例题7.
[root@oldboyedu-lnb ~]# cat /etc/selinux/config |grep '^SELINUX='
SELINUX=diabled
[root@oldboyedu-lnb ~]# cat /etc/selinux/config |grep '^SELINUX='|sed 's#endorcing#disable#g'
SELINUX=disabled
[root@oldboyedu-lnb ~]# sed -i’s#^SELINUX=.*#SRLINUX=disabled#g’ /etc/selinux/config
永久修改
例题8.将root和/bin/bash位置互换
[root@oldboyedu-lnb ~]# cat /etc/passwd
过滤ip地址
方法一:
[root@oldboyedu-lnb ~]# ifconfig ens33 |sed -n ‘2p’
inet 10.0.0.200 netmask 255.255.255.0 broadcast 10.0.0.25
方法二:
[root@oldboyedu-lnb ~]# ifconfig ens33 |sed -n ‘2p’|sed -r ‘s#^.inet (.) netmask.KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲\1#g' …#\1#g’
10.0.0.200/24
[root@oldboyedu-lnb ~]# ip add|sed -n ‘9p’|sed -r 's#^.inet (.)/24 brd.KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲\1#g' …#\1#gp’
10.0.0.200
a、[root@oldboyedu-lnb ~]# ifconfig eth0|head -n2
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.200 netmask 255.255.255.0 broadcast 10.0.0.255
b、[root@oldboyedu-lnb ~]# ifconfig eth0|head -n2|tail -1
inet 10.0.0.200 netmask 255.255.255.0 broadcast 10.0.0.255
c、[root@oldboyedu-lnb ~]# ifconfig eth0|grep inet
inet 10.0.0.200 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::20c:29ff:fe7d:ce prefixlen 64 scopeid 0x20
d、[root@oldboyedu-lnb ~]# ifconfig eth0|grep ‘\binet\b’
inet 10.0.0.200 netmask 255.255.255.0 broadcast 10.0.0.255
方法一:[root@oldboyedu-lnb ~]# ifconfig eth0|grep '\binet\b'|egrep '[0-9.]+' -o //只过滤数字
10.0.0.200
255.255.255.0
10.0.0.255
方法二:[root@oldboyedu-lnb ~]# ifconfig eth0|grep '\binet\b'|egrep '[0-9]+.[0-9]+.[0-9]+.[0-9]+' -o
10.0.0.200
255.255.255.0
10.0.0.255
方法三:[root@oldboyedu-lnb ~]# ifconfig eth0|grep '\binet\b'|egrep '([0-9]{1,3}.){3}[0-9]+' -o
10.0.0.200
255.255.255.0
10.0.0.255