三三世界-百宝箱

文章 分类 标签
130 9 247

公告

暂无站点公告

Linux CentOS7的IP和grep命令使用实例

三三 2022-04-07 1426 0条评论 Linux使用 grep命令IP命令

首页 / 正文

去掉无用ip的所有ip:

ip a | grep inet | grep -v inet6 | grep -v 127 | sed 's/^[ t]*//g' | cut -d ' ' -f2
ip a | grep -E "inet\>"  | cut -d " " -f 10        >>>>>  \>是直接显示inet,不扩展  
ip a | grep -E "inet "  | cut -d " " -f 10         >>>>>  这里在inet后面添加了空格,相当于直接搜索inet空格 ,也可以时先精确查找  

去掉无用ip的所有ip的数量:

ip a | grep inet | grep -v inet6 | grep -v 127 | sed 's/^[ t]*//g' | cut -d ' ' -f2 | wc -l

grep的基本用法和格式

grep root passwd 显示包含root的行
grep ^root passwd 显示以root开头的行
grep root$ passwd 显示以root结尾的行
grep -i root passwd 忽略大小写
grep -v root passwd 显示不包含匹配文本的所有行
grep -E "^root|ROOT$" passwd -E表示扩展的正则表达式,以root开头或者以ROOT结尾的行

注意:正规的 grep 不支持扩展的正则表达式子 , 竖线是用于表示”或”的扩展正则表达式元字符 , 正规的 grep 无法识别,egrep 命令等同于‘grep -E
grep 'r..t' test 表示匹配r开头,t结尾的,中间含有两个任意字符的行,有几个.就匹配多少个
grep -E 'r*t' test 表示显示以r开始,t结尾,中间含有0到任意个r的行
grep -E 'rot' test 表示显示以ro开始,t结尾,中间含有0到任意个o的行,这里表示匹配0到任意多个*前面的字符
grep -E 'ro?t' test 表示显示以ro开头,t结尾的,中间含有0或者1个字符的行
grep -E 'ro{1,}t' test 表示显示以ro开头, t结尾,中间含有1到多个任意字符的行
grep -E 'ro{1,3}t' test 表示显示以ro开头,t结尾,中间含有1到3个任意字符的行
grep -E 'ro{,3}' test 表示显示以ro开头,t结尾,中间含有0到3个任意字符的行
grep -E 'ro{3,}t' test 表示显示以ro开头,t结尾的,中间含有3到无穷任意字符的行
grep -E 'ro+t' test 表示显示以ro开头,t结尾的,中间含有1到无穷多个任意字符的行
这与grep -E 'ro{1,}t' test 的效果一样
grep -E '(root){1,}' test 表示匹配以root开头的,含有1到无穷多个root的行

这里root被括起来,表示一个整体!!!

grep -E 'r.*t' test 表示以r开始t结尾中间含有0到任意个任意字符的行

r.t匹配的是. rt匹配的是r

grep -E "r..>" test 表示显示含有r(不一定是r开头),后面只匹配两个任意字符的行,>表示防止向后扩展,有几个..就匹配几个任意字符
grep -E "<..t" test 表示显示前面两个是任意字符,后面以t结尾的行,<表示防止向前扩张,有几个.前面就匹配几个任意字符

Ip 相关命令

删除所有IP:ip addr flush eth0

增加

格式:ip addr add CIDR dev 网卡名 label 网卡名:num
例子:ip addr add 3.3.3.3/24 dev eth1 label eth1:2

删除

格式:ip addr del CIDR dev 网卡名
指定删除例子:ip addr del 3.3.3.3/24 dev eth0

禁用ens39这个网卡接口

[root@33f ~]# ip link set ens39 down

激活ens39这个网卡接口

[root@33f ~]# ip link set ens39 up

路由相关命令

查看系统的路由信息

[root@33f ~]# ip route show

通过add 动作来增加一条路由规则:

[root@33f ~]# ip route add 192.168.2.0/24 via 192.168.146.254
[root@33f ~]# ip route show

只设定设备转发

[root@33f ~]# ip route add 192.168.146.5 dev ens39
[root@33f ~]# ip route show

删除手工配置的路由规则

ip route del 192.168.146.5 dev ens39
ip route del 192.168.2.0/24 via 192.168.146.254
ip route show

[root@33f ~]# ip route show
default via 192.168.200.2 dev ens32 proto static metric 100 
192.168.146.0/24 dev ens39 proto kernel scope link src 192.168.146.129 metric 100 
192.168.200.0/24 dev ens32 proto kernel scope link src 192.168.200.128 metric 100 
[root@33f ~]# ip route add default via 192.168.200.6
[root@33f ~]# ip route  show
default via 192.168.200.6 dev ens32 
default via 192.168.200.2 dev ens32 proto static metric 100 
192.168.146.0/24 dev ens39 proto kernel scope link src 192.168.146.129 metric 100 
192.168.200.0/24 dev ens32 proto kernel scope link src 192.168.200.128 metric 100 
[root@33f ~]# ip route del default via 192.168.200.6

查看本地服务器的ARP表

ip neigh show

查看接口状态

ip link show [设备名]

# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: em1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
    link/ether f0:4d:a2:75:dc:f6 brd ff:ff:ff:ff:ff:ff
3: eth1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN mode DEFAULT group default qlen 1000
    link/ether f0:4d:a2:75:dc:f7 brd ff:ff:ff:ff:ff:ff

评论(0)

当前没有评论,还不快来留下第一个脚印吧


Copyright 2021 三三世界-百宝箱. All Rights Reserved.

最新评论

  • 三三

    @Doug Shume it's ok for me , you can post here.

  • refugiaguenther

    Saved as a favorite, I like your website!

  • josefa

    If some one wishes to be updated with hottest technologies after that he must be visit this site and be up to date daily.

  • Penzu

    Heello would you mind sharing which blog platform you're using? I'm planning to start my own blog in the near future but I'm having a tough time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your layout seems different then moost blogs and I'm looking for something completely unique. P.S Apologies forr being off-topic butt I had to ask!

  • Yupoo Fendi

    Thanks to my father who shared with me regarding this webpage, this website is genuinely amazing.

  • Doug Shume

    Hi, I have an overflow of customers that I'd like to send to you but I want to make sure you can handle more leads, let me know if you'd like me to send you more info.

  • SuperWind

    zh.us.to 有效
    kms.03k.org 有效
    kms.chinancce.com
    kms.shuax.com 有效
    kms.dwhd.org 有效
    kms.luody.info 有效
    kms.digiboy.ir 有效
    kms.lotro.cc 有效
    www.zgbs.cc 有效
    cy2617.jios.org 有效

  • 三三

    @         权限问题,试试sudo 再加命令。

  •         

    你好提示Permission denied 怎么办啊

日历

2024年05月

   1234
567891011
12131415161718
19202122232425
262728293031 

文章目录

上一张 下一张