grep 过滤输出
2026/10/14CLI 管理7.X.X大约 3 分钟
grep 过滤输出
get、show 和 diagnose 的输出可以通过管道传给 grep,按文本或正则表达式筛选。
参数说明
-i:匹配时忽略大小写。-n:在输出行前显示该行在原始输出中的行号。-v:反向匹配,只显示不符合过滤表达式的行。-f:保留匹配内容所属的 Fortinet 配置层级。-c:只显示匹配行的数量,不显示匹配行内容。-A <NUM>:显示匹配行及其后面的NUM行。-B <NUM>:显示匹配行及其前面的NUM行。-C <NUM>:显示匹配行及其前后各NUM行。
使用示例
查看接口 MAC 地址所在的行。
# get hardware nic port1 | grep Current_HWaddr Current_HWaddr 02:00:00:00:01:01筛选会话列表中的 TCP 行,并显示行号。
# get system session list | grep -n tcp 2:tcp 3550 192.168.100.177:59265 10.10.12.2:59265 203.0.113.10:443 - 3:tcp 3582 192.168.100.177:59729 10.10.12.2:59729 203.0.113.11:443 - 4:tcp 3583 192.168.100.177:59063 10.10.12.2:59063 203.0.113.12:80 - ...忽略大小写,用大写
URL筛选 HTTP 替换消息中的url。# show system replacemsg http | grep -i URL config system replacemsg http "url-block" config system replacemsg http "urlfilter-err"反向匹配,排除接口配置中包含
set snmp-index的行。# show system interface | grep -v 'set snmp-index' config system interface edit "dmz" set vdom "root" set allowaccess ping https http set type physical set role dmz next edit "mgmt" ... end只统计包含
set allowaccess且后面出现ssh的配置行数量。# show system interface | grep -c 'set allowaccess.*ssh' 3相关信息
-c统计的是匹配行数,不是某个关键词在文本中出现的总次数。同一行出现多次匹配内容,也只计为一行。使用
-A 2显示匹配行及其后面的 2 行。# show system interface lan | grep -A 2 'edit "lan"' edit "lan" set vdom "root" set ip 192.168.100.99 255.255.255.0使用
-B 2显示匹配行及其前面的 2 行。# show system interface lan | grep -B 2 'set ip 192.168.100.99' edit "lan" set vdom "root" set ip 192.168.100.99 255.255.255.0使用
-C 2显示匹配行及其前后各 2 行。# show system interface lan | grep -C 2 'set ip 192.168.100.99' edit "lan" set vdom "root" set ip 192.168.100.99 255.255.255.0 set allowaccess ping https ssh http fgfm fabric ftm set type hard-switch使用
-f保留匹配配置的所属层级,便于识别对象及其引用。相关信息
以下为设备已有用户组的查询输出。普通过滤只显示匹配行,
-f可以保留config、edit、next和end等层级信息,并用<---标出匹配行。# show user group | grep Guest-group edit "Guest-group" # show user group | grep -f Guest-group config user group edit "Guest-group" <--- ... set member "guest" next end
多条件过滤
多个过滤条件可以组合在同一个 grep 表达式中,按需要匹配任一条件,或要求同一行同时包含两个条件。
满足任一条件(OR):用
\|分隔条件,并用单引号包围整个表达式。下面筛选包含https或ssh的行。# show system interface | grep 'https\|ssh' set allowaccess ping https http set allowaccess ping https ssh set allowaccess ping https ssh http ftm set ip6-allowaccess ping ssh set allowaccess ping https http set allowaccess ping https ssh http fgfm fabric ftm同时满足两个条件(AND):筛选同一行中同时包含默认路由网段
0.0.0.0/0和出接口wan1的路由记录。# get router info routing-table all | grep '0\.0\.0\.0/0.*wan1' S* 0.0.0.0/0 [1/0] via 10.10.12.1, wan1, 4d23h55m, [1/0]相关信息
.*匹配网段与接口名之间的内容,本例中是[1/0] via 10.10.12.1,。- IP 地址中的
.写为\.,表示匹配点号本身。 - 两个条件只针对同一行判断。多下一跳路由可能分行显示,此表达式不会跨行匹配。
过滤配置时,可以结合
-f保留所属层级。下面筛选包含set allowaccess且后面出现ssh的配置行,并保留对应接口配置。# show system interface | grep -f 'set allowaccess.*ssh' config system interface ... edit "lan" set vdom "root" set ip 192.168.100.99 255.255.255.0 set allowaccess ping https ssh http fgfm fabric ftm <--- set type hard-switch set explicit-web-proxy enable set explicit-ftp-proxy enable set device-identification enable set snmp-index 33 config ipv6 set ip6-allowaccess ping end next end