Extracting Interface Names from show interface

🏷️ Regular Expressions (Regex) / Practical Engineering Examples

🎯 Context Introduction

When working with network devices, one of the most common tasks is parsing the output of show interface commands. Engineers often need to extract just the interface names from a large block of text to build inventories, check statuses, or feed into automation scripts. Regular expressions provide a clean and efficient way to pull out exactly what you need without manually scanning through pages of output.


⚙️ The Challenge

A typical show interface output contains many lines, but the interface name always appears at the beginning of its section, followed by status and statistics. The goal is to isolate only the lines that contain interface names like GigabitEthernet0/1, FastEthernet0/0, Serial1/0, or Loopback0.

Key observations about interface name lines: - They always start at the beginning of a line - They contain a combination of letters, numbers, and slashes - They are followed by either is up or is administratively down - They never contain leading spaces or indentation


🕵️ Building the Regex Pattern

To match interface names, we need a pattern that captures the following structure:

  • Start of line – Use the caret symbol ^
  • Interface type – A word like GigabitEthernet, FastEthernet, Serial, Loopback, Vlan, or Tunnel
  • Interface number – Digits, possibly with a slash separator like 0/1 or 1/0/0
  • Followed by space and "is" – This confirms we are on the correct line

The resulting pattern looks like this: ^(\w+\d+/\d+|\w+\d+)\s+is

Breaking this down: - ^ – Anchors the match to the beginning of the line - \w+ – Matches one or more word characters (letters and digits) for the interface type - \d+/\d+ – Matches a number, slash, and another number (e.g., 0/1) - | – The OR operator to handle interfaces without a slash (e.g., Loopback0) - \w+\d+ – Matches a word followed by digits (e.g., Loopback0) - \s+is – Ensures the line continues with spaces and the word is


📊 Comparison: With vs. Without Regex

Approach Effort Accuracy Flexibility
Manual scanning High – must read every line Low – prone to human error None – must redo for each device
String splitting Medium – requires multiple steps Medium – misses edge cases Low – breaks on different formats
Regex pattern matching Low – one pattern does all High – captures all variations High – works across vendors

🛠️ Practical Python Example

Here is how you would use Python with the re module to extract interface names from a show interface output:

  1. Import the re module – This gives you access to regex functions
  2. Store the show interface output – As a multi-line string variable
  3. Compile the pattern – Using re.compile() for better performance
  4. Find all matches – Using findall() to return a list of interface names
  5. Print or store the results – For further processing

The script would: - Read the show interface output from a file or variable - Apply the regex pattern ^(\w+\d+/\d+|\w+\d+)\s+is - Return a clean list like: ['GigabitEthernet0/0', 'GigabitEthernet0/1', 'Serial1/0', 'Loopback0']


📋 Expected Output

After running the extraction, you would see a list similar to this:

  • GigabitEthernet0/0
  • GigabitEthernet0/1
  • Serial1/0
  • Loopback0
  • Vlan1
  • Tunnel0

Each entry represents a single interface name extracted from the original output, ready for use in further automation or inventory tasks.


🔄 Handling Different Interface Formats

Network devices from different vendors may use slightly different naming conventions. Here are variations your pattern should handle:

  • Cisco styleGigabitEthernet0/1, FastEthernet0/0
  • Juniper stylege-0/0/0, fxp0
  • Generic styleeth0, ens192, bond0

To accommodate these, you can adjust the pattern to: ^[a-zA-Z][a-zA-Z0-9-]+\d+(?:/\d+)*\s+is

This version: - Starts with a letter - Allows hyphens in the interface type name - Matches one or more digits - Optionally matches multiple slash-separated numbers


✅ Key Takeaways

  • Regex provides a precise way to extract interface names from large text outputs
  • The anchor ^ ensures you only match lines that start with an interface name
  • Using \w+ and \d+ captures both the type and number portions
  • The | operator handles interfaces with and without slash separators
  • Python's re.findall() returns a clean list ready for automation
  • Always test your pattern against actual device output to catch edge cases

📚 Next Steps

Once you have extracted interface names, you can: - Loop through each name to check its status - Build a configuration template for each interface - Compare interface lists across multiple devices - Generate reports showing which interfaces are up or down

Regular expressions are a powerful tool in any engineer's toolkit, and mastering this simple extraction pattern opens the door to much more advanced text parsing and automation workflows.


This topic shows how to use regular expressions to extract interface names from Cisco "show interface" command output.


🔧 Example 1: Finding a single interface name

This example finds the first interface name in a simple string.

import re

text = "GigabitEthernet0/1 is up"
pattern = r"GigabitEthernet\d+/\d+"
match = re.search(pattern, text)
print(match.group())

📤 Output: GigabitEthernet0/1


🔧 Example 2: Extracting multiple interface names

This example finds all interface names from a multi-line show interface output.

import re

show_int_output = """
GigabitEthernet0/1 is up
GigabitEthernet0/2 is down
GigabitEthernet0/3 is up
"""
pattern = r"GigabitEthernet\d+/\d+"
interfaces = re.findall(pattern, show_int_output)
print(interfaces)

📤 Output: ['GigabitEthernet0/1', 'GigabitEthernet0/2', 'GigabitEthernet0/3']


🔧 Example 3: Handling different interface types

This example extracts interface names that include FastEthernet and GigabitEthernet types.

import re

show_int_output = """
FastEthernet0/1 is up
GigabitEthernet0/2 is down
FastEthernet0/3 is up
"""
pattern = r"(FastEthernet|GigabitEthernet)\d+/\d+"
interfaces = re.findall(pattern, show_int_output)
print(interfaces)

📤 Output: ['FastEthernet', 'GigabitEthernet', 'FastEthernet']


🔧 Example 4: Extracting full interface names with status

This example captures both the interface name and its operational status.

import re

show_int_output = """
GigabitEthernet0/1 is up
GigabitEthernet0/2 is down
GigabitEthernet0/3 is up
"""
pattern = r"(GigabitEthernet\d+/\d+) is (up|down)"
matches = re.findall(pattern, show_int_output)
for interface, status in matches:
    print(f"{interface}: {status}")

📤 Output: GigabitEthernet0/1: up
GigabitEthernet0/2: down
GigabitEthernet0/3: up


🔧 Example 5: Extracting only up interfaces

This example filters and returns only interfaces that are in "up" status.

import re

show_int_output = """
GigabitEthernet0/1 is up
GigabitEthernet0/2 is down
GigabitEthernet0/3 is up
FastEthernet0/1 is up
"""
pattern = r"(GigabitEthernet\d+/\d+|FastEthernet\d+/\d+) is up"
up_interfaces = re.findall(pattern, show_int_output)
print(up_interfaces)

📤 Output: ['GigabitEthernet0/1', 'GigabitEthernet0/3', 'FastEthernet0/1']


📊 Comparison Table

Example Pattern Used What It Extracts
Example 1 GigabitEthernet\d+/\d+ Single interface name
Example 2 GigabitEthernet\d+/\d+ All interface names
Example 3 (FastEthernet\|GigabitEthernet)\d+/\d+ Interface type only
Example 4 (GigabitEthernet\d+/\d+) is (up\|down) Interface name + status
Example 5 (GigabitEthernet\d+/\d+\|FastEthernet\d+/\d+) is up Only up interfaces

🎯 Context Introduction

When working with network devices, one of the most common tasks is parsing the output of show interface commands. Engineers often need to extract just the interface names from a large block of text to build inventories, check statuses, or feed into automation scripts. Regular expressions provide a clean and efficient way to pull out exactly what you need without manually scanning through pages of output.


⚙️ The Challenge

A typical show interface output contains many lines, but the interface name always appears at the beginning of its section, followed by status and statistics. The goal is to isolate only the lines that contain interface names like GigabitEthernet0/1, FastEthernet0/0, Serial1/0, or Loopback0.

Key observations about interface name lines: - They always start at the beginning of a line - They contain a combination of letters, numbers, and slashes - They are followed by either is up or is administratively down - They never contain leading spaces or indentation


🕵️ Building the Regex Pattern

To match interface names, we need a pattern that captures the following structure:

  • Start of line – Use the caret symbol ^
  • Interface type – A word like GigabitEthernet, FastEthernet, Serial, Loopback, Vlan, or Tunnel
  • Interface number – Digits, possibly with a slash separator like 0/1 or 1/0/0
  • Followed by space and "is" – This confirms we are on the correct line

The resulting pattern looks like this: ^(\w+\d+/\d+|\w+\d+)\s+is

Breaking this down: - ^ – Anchors the match to the beginning of the line - \w+ – Matches one or more word characters (letters and digits) for the interface type - \d+/\d+ – Matches a number, slash, and another number (e.g., 0/1) - | – The OR operator to handle interfaces without a slash (e.g., Loopback0) - \w+\d+ – Matches a word followed by digits (e.g., Loopback0) - \s+is – Ensures the line continues with spaces and the word is


📊 Comparison: With vs. Without Regex

Approach Effort Accuracy Flexibility
Manual scanning High – must read every line Low – prone to human error None – must redo for each device
String splitting Medium – requires multiple steps Medium – misses edge cases Low – breaks on different formats
Regex pattern matching Low – one pattern does all High – captures all variations High – works across vendors

🛠️ Practical Python Example

Here is how you would use Python with the re module to extract interface names from a show interface output:

  1. Import the re module – This gives you access to regex functions
  2. Store the show interface output – As a multi-line string variable
  3. Compile the pattern – Using re.compile() for better performance
  4. Find all matches – Using findall() to return a list of interface names
  5. Print or store the results – For further processing

The script would: - Read the show interface output from a file or variable - Apply the regex pattern ^(\w+\d+/\d+|\w+\d+)\s+is - Return a clean list like: ['GigabitEthernet0/0', 'GigabitEthernet0/1', 'Serial1/0', 'Loopback0']


📋 Expected Output

After running the extraction, you would see a list similar to this:

  • GigabitEthernet0/0
  • GigabitEthernet0/1
  • Serial1/0
  • Loopback0
  • Vlan1
  • Tunnel0

Each entry represents a single interface name extracted from the original output, ready for use in further automation or inventory tasks.


🔄 Handling Different Interface Formats

Network devices from different vendors may use slightly different naming conventions. Here are variations your pattern should handle:

  • Cisco styleGigabitEthernet0/1, FastEthernet0/0
  • Juniper stylege-0/0/0, fxp0
  • Generic styleeth0, ens192, bond0

To accommodate these, you can adjust the pattern to: ^[a-zA-Z][a-zA-Z0-9-]+\d+(?:/\d+)*\s+is

This version: - Starts with a letter - Allows hyphens in the interface type name - Matches one or more digits - Optionally matches multiple slash-separated numbers


✅ Key Takeaways

  • Regex provides a precise way to extract interface names from large text outputs
  • The anchor ^ ensures you only match lines that start with an interface name
  • Using \w+ and \d+ captures both the type and number portions
  • The | operator handles interfaces with and without slash separators
  • Python's re.findall() returns a clean list ready for automation
  • Always test your pattern against actual device output to catch edge cases

📚 Next Steps

Once you have extracted interface names, you can: - Loop through each name to check its status - Build a configuration template for each interface - Compare interface lists across multiple devices - Generate reports showing which interfaces are up or down

Regular expressions are a powerful tool in any engineer's toolkit, and mastering this simple extraction pattern opens the door to much more advanced text parsing and automation workflows.

Interactive Views

You are currently in 📚 All-in-One mode. Use the tabs at the top to switch to 📖 Theory Only or 💻 Code Only views.

This topic shows how to use regular expressions to extract interface names from Cisco "show interface" command output.


🔧 Example 1: Finding a single interface name

This example finds the first interface name in a simple string.

import re

text = "GigabitEthernet0/1 is up"
pattern = r"GigabitEthernet\d+/\d+"
match = re.search(pattern, text)
print(match.group())

📤 Output: GigabitEthernet0/1


🔧 Example 2: Extracting multiple interface names

This example finds all interface names from a multi-line show interface output.

import re

show_int_output = """
GigabitEthernet0/1 is up
GigabitEthernet0/2 is down
GigabitEthernet0/3 is up
"""
pattern = r"GigabitEthernet\d+/\d+"
interfaces = re.findall(pattern, show_int_output)
print(interfaces)

📤 Output: ['GigabitEthernet0/1', 'GigabitEthernet0/2', 'GigabitEthernet0/3']


🔧 Example 3: Handling different interface types

This example extracts interface names that include FastEthernet and GigabitEthernet types.

import re

show_int_output = """
FastEthernet0/1 is up
GigabitEthernet0/2 is down
FastEthernet0/3 is up
"""
pattern = r"(FastEthernet|GigabitEthernet)\d+/\d+"
interfaces = re.findall(pattern, show_int_output)
print(interfaces)

📤 Output: ['FastEthernet', 'GigabitEthernet', 'FastEthernet']


🔧 Example 4: Extracting full interface names with status

This example captures both the interface name and its operational status.

import re

show_int_output = """
GigabitEthernet0/1 is up
GigabitEthernet0/2 is down
GigabitEthernet0/3 is up
"""
pattern = r"(GigabitEthernet\d+/\d+) is (up|down)"
matches = re.findall(pattern, show_int_output)
for interface, status in matches:
    print(f"{interface}: {status}")

📤 Output: GigabitEthernet0/1: up
GigabitEthernet0/2: down
GigabitEthernet0/3: up


🔧 Example 5: Extracting only up interfaces

This example filters and returns only interfaces that are in "up" status.

import re

show_int_output = """
GigabitEthernet0/1 is up
GigabitEthernet0/2 is down
GigabitEthernet0/3 is up
FastEthernet0/1 is up
"""
pattern = r"(GigabitEthernet\d+/\d+|FastEthernet\d+/\d+) is up"
up_interfaces = re.findall(pattern, show_int_output)
print(up_interfaces)

📤 Output: ['GigabitEthernet0/1', 'GigabitEthernet0/3', 'FastEthernet0/1']


📊 Comparison Table

Example Pattern Used What It Extracts
Example 1 GigabitEthernet\d+/\d+ Single interface name
Example 2 GigabitEthernet\d+/\d+ All interface names
Example 3 (FastEthernet\|GigabitEthernet)\d+/\d+ Interface type only
Example 4 (GigabitEthernet\d+/\d+) is (up\|down) Interface name + status
Example 5 (GigabitEthernet\d+/\d+\|FastEthernet\d+/\d+) is up Only up interfaces