Professional SNMP Configuration Comparison: How to Diff Two snmpd.conf Files Effectively While Ignoring Comments and Whitespace


3 views

When troubleshooting SNMP configurations, we often need to compare a working snmpd.conf with a broken one. The challenge? These files are typically cluttered with comments, whitespace, and boilerplate text that obscure the actual configuration differences.

Here's my go-to method using standard Unix tools to filter out the noise before comparison:

diff <(grep -vE '^#|^$' working.conf | sort) <(grep -vE '^#|^$' broken.conf | sort)

Breaking this down:

1. grep -vE '^#|^$' removes comments (lines starting with #) and empty lines

2. sort normalizes the line order for better comparison

3. Process substitution (<(...)) lets us pipe pre-processed files to diff

When you need more sophisticated filtering, this Python script provides greater control:

import difflib

def clean_config(lines):
    return [line.strip() for line in lines 
            if line.strip() and not line.startswith('#')]

with open('working.conf') as f1, open('broken.conf') as f2:
    diff = difflib.unified_diff(
        clean_config(f1.readlines()),
        clean_config(f2.readlines()),
        fromfile='working.conf',
        tofile='broken.conf'
    )
    print('\n'.join(diff))

For GUI lovers, these tools offer excellent filtered diff capabilities:

  • Meld: Use its text filter feature (Right-click → Filters)
  • VS Code: The built-in diff viewer with extensions like "Diff"
  • Araxis Merge: Powerful professional tool with regex filtering

Sometimes you need to normalize equivalent configurations before comparing. This sed command helps standardize SNMP community strings:

sed -E 's/^(community)\s+(\S+)\s+(.*)/\1 \2 \3/' config_file.conf

If your configs are in Git, use this powerful command to compare while ignoring whitespace:

git diff --ignore-all-space --no-index working.conf broken.conf

When troubleshooting SNMP configurations, you often need to compare a working snmpd.conf with a non-functional one. The challenge lies in filtering out noise like comments and whitespace while preserving meaningful configuration differences.

# Using grep and diff together:
diff <(grep -vE '^#|^$' working.conf) <(grep -vE '^#|^$' broken.conf)

# This approach:
# 1. grep -vE removes lines matching patterns (^# for comments, ^$ for empty lines)
# 2. Process substitution (<(...)) feeds the filtered output to diff

For more sophisticated comparisons, consider this Python script:

import difflib
import re

def clean_config(lines):
    return [line.strip() for line in lines 
            if not re.match(r'^\s*(#|$)', line)]

with open('working.conf') as f1, open('broken.conf') as f2:
    diff = difflib.unified_diff(
        clean_config(f1.readlines()),
        clean_config(f2.readlines()),
        fromfile='working.conf',
        tofile='broken.conf'
    )
    print(''.join(diff))

The ndiff utility from python3-snmp package offers SNMP-aware comparison:

ndiff --ignore-comments --ignore-blanks working.conf broken.conf

For GUI lovers, configure meld/kdiff3 to ignore comments:

# Meld preferences:
# Edit → Preferences → Text Filters → Add pattern: ^\s*#.*$

# kdiff3 settings:
# Settings → Configure kdiff3 → Diff → Ignore regexp: ^[[:space:]]*#.*$

Some SNMP configurations span multiple lines. This AWK script handles them better:

awk '
BEGIN { RS=""; ORS="\n\n"; FS="\n" } 
!/^#/ && !/^$/ { 
    for(i=1; i<=NF; i++) 
        if($i !~ /^[[:space:]]*#/) 
            print $i 
}' snmpd.conf | diff -u - working_clean.conf

When using git, create a custom diff driver in .git/config:

[diff "snmp"]
    textconv = "grep -vE '^#|^$'"
    cachetextconv = true