Supakorn Visutthicho

การใช้ Pipe (|) เมื่อเรา show command ต่างๆ เพื่อ filter ในส่วนที่เราสนใจบน Cisco IOS

ใบบทความนี้ผมจะมานำเสนอวิธีการใช้ Pipe (|) เมื่อเรา show command ต่างๆ เพื่อ filter ในส่วนที่เราสนใจกันครับ
ปกติแล้วเวลาที่เราใช้ show command เพื่อดูค่าต่างๆบน Cisco IOS มันก็จะแสดงค่ามาทั้งหมด

อย่างเช่น เวลาที่เราใช้คำสั่ง "show running-config"
R4#show running-config
Building configuration...
Current configuration : 2849 bytes
!
version 15.4
service timestamps debug datetime msec
service timestamps log datetime msec
no service password-encryption
!
hostname R4
!
boot-start-marker
boot-end-marker
!
!
no aaa new-model
mmi polling-interval 60
no mmi auto-configure
no mmi pvc
mmi snmp-timeout 180
!
ip dhcp excluded-address 10.2.1.1 10.2.1.3
ip dhcp excluded-address 10.2.1.254
!
ip dhcp pool VLAN_10_CLIENTS
network 10.2.1.0 255.255.255.0
default-router 10.2.1.254
!
!
no ip domain lookup
ip cef
ipv6 unicast-routing
no ipv6 cef
!
multilink bundle-name authenticated
!
!
redundancy
!
!
interface Loopback4
description Test I/F for OSPFv2 & OSPFv3 Routing
ip address 4.4.4.4 255.255.255.255
ipv6 address FEC0::4:4/122
ipv6 ospf 6 area 34
!
interface Loopback14
description Test I/F for RIPng Routing
no ip address
ipv6 address FEC0::14:4/122
ipv6 rip RIP_ZONE enable
!
interface Loopback21
description Test I/F for EIGRPv4 Routing & IP(v4) Helper Loopback I/F
ip address 10.1.21.129 255.255.255.224
!
interface Tunnel34
no ip address
ipv6 address 2026::34:2/122
ipv6 ospf 6 area 34
tunnel source Loopback4
tunnel mode ipv6ip
tunnel destination 3.3.3.3
!
interface Ethernet0/0
description L3 Link to DSW1 fa0/1
ip address 10.1.4.5 255.255.255.252
ipv6 address 2026::2:1/122
ipv6 rip RIP_ZONE enable
!
interface Ethernet0/1
description L3 Link to DSW2 fa0/14
ip address 10.1.4.9 255.255.255.252
!
interface Ethernet0/2
no ip address
shutdown
<output omitted>

ถ้าเราต้องการจะดู config เฉพาะส่วน เช่น ต้องการดูแค่ routing , static route หรือ dhcp เวลาที่เราใช้คำสั่ง "show running-config" กว่าเราจะหาเจอว่าอยู่ส่วนไหน ก็ทำให้เกิดความยากลำบาก บางที config เยอะๆ ก็ทำให้ตาลายได้อีก

Empty space, drag to resize

การใช้ Pipe (|) จะช่วย filter ส่วนที่เราสนใจได้ โดยจะมีวิธีการ filter ได้หลายแบบ ดังนี้

มาลองดูตัวอย่างการใช้งานกันครับ

ตัวอย่างที่ 1 : ต้องการดูการตั้งค่า static route บน Router
R4#show running-config | include ip route
ip route 0.0.0.0 0.0.0.0 202.100.2.1
ip route 10.1.4.4 255.255.255.255 10.2.4.3
ip route 10.2.1.0 255.255.255.0 10.2.4.3
R4#
ผมอยากดูการตั้งค่า static route บน Router ถ้าผมใช้ show running-config โดยไม่ใช้ Pipe (|) ถ้า config เยอะๆ กว่าผมจะหาเจอก็ต้องใช้เวลา บางทีก็ตาลายด้วย ผมเลยใช้ "show running-config | include ip route" เพื่อให้แสดงเฉพาะบรรทัดที่มีคำว่า "ip route" (ซึ่งเป็นคำสั่งทีใช้สร้าง static route) มันก็จะทำการ filter บรรทัดที่มีเฉพาะคำว่า "ip route" มาให้เรา ทำให้เราดูง่ายขึ้นเลยเห็นไหมครับ

ตัวอย่างที่ 2 : ต้องการดูการตั้งค่า Routing OSPF บน Router
R4#show running-config | section router ospf
router ospf 1
 router-id 4.4.4.4
 area 34 nssa
 redistribute eigrp 10 metric-type 1 subnets
 network 4.4.4.4 0.0.0.0 area 34
 network 10.1.1.8 0.0.0.3 area 34
ผมอยากดูการตั้งค่า Routing OSPF บน Router ผมจึงใช้ "show running-config | section router ospf" เพื่อดูเฉพาะ section หรือคำสั่ง (sub-command) ภายใต้คำว่า "router ospf" มันก็จะทำการแสดงคำว่า "router ospf" และ sub-command ที่อยู่ภายใต้คำสั่ง "router ospf" มาให้นั่นเอง

คำถามชวนคิด : ถ้าตัวอย่างที่ 2 นี้ ผมใช้ Pipe แบบ include ได้ไหม ? มาลองดูผลกันครับ
R4#show running-config | include router ospf
router ospf 1
เห็นไหมครับ มันแสดงเฉพาะบรรทัดที่มีคำว่า "router ospf" ไม่ได้แสดงเป็น section คือ ไม่แสดงคำสั่ง (sub-command) ที่อยู่ภายใต้ "router ospf" มาให้ด้วย ฉะนั้น เราต้องลองเลือกดูว่าจะเลือกใช้แบบไหนในการ filter

ตัวอย่างที่ 3 : ต้องการดูการตั้งค่า IP address บน Interface ด้วยคำสั่ง "show ip interface brief"

ลองดูตัวอย่างคำสั่ง "show ip interface brief" แบบปกติ
แล้วมาลองดูตัวอย่างการใช้ Pipe แบบ "exclude" กันครับ
ผมใช้คำสั่ง "show running-config | exclude unassigned" เพื่อไม่ต้องการให้แสดงบรรทัดที่มีคำว่า "unassigned" ทำให้เราดูง่ายขึ้นว่า มี interface ไหนที่มี IP adddress อยู่บ้างนั่นเอง

ผมก็หวังว่าบทความนี้น่าจะเป็นประโยชน์นะครับ ซึ่งเราสามารถนำ Pipe ไปประยุกต์ใช้งานได้หลายๆแบบเลย แล้วแต่ว่าเราอยากจะ filter ให้แสดงแบบไหน
แล้วเจอกันในบทความต่อๆไปครับ
Created with