Skip to content

NetDevAutomate/aws_network_shell

Repository files navigation

AWS Network Shell

AWS Network Tools CLI - A hierarchical CLI for AWS networking

🌟 Overview

AWS Network Shell provides a comprehensive CLI for managing AWS networking resources with a familiar Cisco IOS-style interface. Navigate through contexts (VPC, Transit Gateway, Firewall, etc.) and execute commands with hierarchical command structures.

πŸš€ Quick Start

# Install
pip install -e .

# Launch shell
aws-net-shell

# Or with AWS profile
aws-net-shell -p production

# Run automated workflows
aws-net-runner "show vpcs" "set vpc 1" "show subnets"

# Debug mode for troubleshooting
aws-net-runner --debug "show vpns" "set vpn 1" "show tunnels"
# Logs to: /tmp/aws_net_runner_debug_<timestamp>.log

πŸ“Š Command Hierarchy

The CLI uses a hierarchical command structure with 10 contexts and 100+ commands.

Graph Commands

Explore and validate the command hierarchy:

# Show command tree
aws-net> show graph

# Show statistics
aws-net> show graph stats
  Total nodes: 103
  Total edges: 145
  Contexts: 10
  Command paths: 78
  Implemented: 100%

# Validate all handlers exist
aws-net> show graph validate
βœ“ Graph is valid - all handlers implemented

# Export Mermaid diagram
aws-net> show graph mermaid

# Find path to specific command
aws-net> show graph parent find-prefix
Paths to 'find-prefix':
βœ“ find-prefix
  Context: route-table
  Path: show route-tables β†’ set route-table 1 β†’ find-prefix

Available Graph Operations

  • show graph - Display command tree structure
  • show graph stats - Show command statistics
  • show graph validate - Verify all handlers implemented
  • show graph mermaid - Generate Mermaid diagram
  • show graph parent <command> - Show navigation path to command
  • validate-graph - Run full validation check
  • export-graph [filename] - Export to markdown file

πŸ”₯ Network Firewall Commands

Complete AWS Network Firewall inspection and management:

# List all firewalls
aws-net> show firewalls

# Enter firewall context
aws-net> set firewall 1
aws-net>fi:1>

# Show firewall overview with rule groups
aws-net>fi:1> show firewall
πŸ”₯ Network Firewall: prod-firewall-1
β”œβ”€β”€ ID: 12345678-1234-1234-1234-123456789abc
β”œβ”€β”€ Region: us-east-1
β”œβ”€β”€ VPC: vpc-0123456789abcdef0
└── Subnets: subnet-0a1b2c3d4e5f67890, subnet-0f9e8d7c6b5a43210

Rule Groups
┏━━━┳━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━┳━━━━━━━━━━┓
┃ # ┃ Name                 ┃ Type      ┃ Rules ┃ Capacity ┃
┑━━━╇━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━╇━━━━━━━━━━┩
β”‚ 1 β”‚ drop-remote-outbound β”‚ STATELESS β”‚     2 β”‚      2/2 β”‚
β”‚ 2 β”‚ allow-domains        β”‚ STATEFUL  β”‚     2 β”‚    2/100 β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

# Show policy with rule groups
aws-net>fi:1> show policy

# Show rule groups (with selection index)
aws-net>fi:1> show rule-groups

# Enter rule group context
aws-net>fi:1> set rule-group 1
aws-net>fi:1>ru:1>

# Show detailed rules (STATELESS)
aws-net>fi:1>ru:1> show rule-group
┏━━━┳━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ # ┃ Priority ┃ Actions  ┃ Sources   ┃ Destinations ┃ Protocols ┃ Source Ports ┃ Dest Ports ┃
┑━━━╇━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
β”‚ 1 β”‚        1 β”‚ aws:drop β”‚ 0.0.0.0/0 β”‚ 0.0.0.0/0    β”‚ 6         β”‚ 22           β”‚ 22         β”‚
β”‚ 2 β”‚        2 β”‚ aws:drop β”‚ 0.0.0.0/0 β”‚ 0.0.0.0/0    β”‚ 27        β”‚ Any          β”‚ Any        β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

# Show detailed rules (STATEFUL with Suricata format)
aws-net>fi:1> set rule-group 2
aws-net>fi:1>ru:2> show rule-group
  1. pass tcp any any <> $EXTERNAL_NET 443 (msg:"Allowing TCP in port 443"; flow:not_established; sid:892123; rev:1;)
  2. pass tls any any -> $EXTERNAL_NET 443 (tls.sni; dotprefix; content:".amazon.com"; endswith; msg:"Allowing .amazon.com HTTPS requests"; sid:892125; rev:1;)

Firewall Commands Summary

  • Contexts: firewall β†’ rule-group (2-level hierarchy)
  • Commands: show firewall, show rule-groups, show policy, set rule-group, show rule-group
  • Display: Complete rule details including ports, protocols, actions, and Suricata rules
  • Navigation: Index-based selection for easy context switching

πŸ”Œ VPN Commands

Complete Site-to-Site VPN tunnel inspection and management:

# List all VPN connections
aws-net> show vpns
                                 Site-to-Site VPN Connections
┏━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━━┓
┃ # ┃ Name           ┃ ID              ┃ State     ┃ Type    ┃ Region    ┃
┑━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━┩
β”‚ 1 β”‚ corporate-vpn  β”‚ vpn-0abcdef123… β”‚ available β”‚ ipsec.1 β”‚ us-east-1 β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

# Enter VPN context
aws-net> set vpn 1
aws-net>vp:1>

# Show VPN overview with tunnel summary
aws-net>vp:1> show detail
       VPN: corporate-vpn
┏━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Field ┃ Value                 ┃
┑━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━┩
β”‚ id    β”‚ vpn-0abcdef123456789a β”‚
β”‚ state β”‚ available             β”‚
β”‚ type  β”‚ ipsec.1               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Tunnels (2):
  UP 203.0.113.10
  UP 203.0.113.20

# Show detailed tunnel status
aws-net>vp:1> show tunnels
            VPN Tunnels: corporate-vpn
┏━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━┓
┃ Outside IP   ┃ Status ┃ Status Message ┃ Routes ┃
┑━━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━┩
β”‚ 203.0.113.10 β”‚ UP     β”‚ 3 BGP ROUTES   β”‚      3 β”‚
β”‚ 203.0.113.20 β”‚ UP     β”‚ 3 BGP ROUTES   β”‚      3 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜

VPN Commands Summary

  • Context: vpn (1-level hierarchy)
  • Commands: show detail, show tunnels
  • Display: IPSec tunnel status with outside IPs, BGP route counts, status messages
  • Status Colors: Green (UP), Red (DOWN) for immediate visual identification
  • Data Source: AWS VgwTelemetry with tunnel health and BGP session information

πŸ“ Repository Structure

aws_net_shell/
β”œβ”€β”€ src/
β”‚   └── aws_network_tools/
β”‚       β”œβ”€β”€ cli.py                    # CLI entry point
β”‚       β”œβ”€β”€ core/                     # Core functionality
β”‚       β”‚   β”œβ”€β”€ base.py              # Base shell class
β”‚       β”‚   β”œβ”€β”€ graph.py             # Command graph logic
β”‚       β”‚   β”œβ”€β”€ ip_resolver.py       # IP address resolution
β”‚       β”‚   β”œβ”€β”€ spinner.py           # Loading spinners
β”‚       β”‚   └── renderer.py          # Output rendering
β”‚       β”œβ”€β”€ models/                   # Data models
β”‚       β”œβ”€β”€ modules/                  # AWS service modules
β”‚       β”œβ”€β”€ shell/                   # Shell implementation
β”‚       β”‚   β”œβ”€β”€ base.py              # Base shell handlers
β”‚       β”‚   β”œβ”€β”€ graph.py             # Graph command implementation
β”‚       β”‚   β”œβ”€β”€ handlers/            # Command handlers by context
β”‚       β”‚   └── main.py              # Main shell entry
β”‚       └── traceroute/              # Traceroute functionality
β”œβ”€β”€ tests/                           # Test suite
β”‚   β”œβ”€β”€ conftest.py                 # Pytest fixtures
β”‚   β”œβ”€β”€ test_shell.py               # Shell tests
β”‚   └── test_*.py                   # Unit tests
β”œβ”€β”€ docs/                           # Documentation
β”‚   β”œβ”€β”€ command-hierarchy-split.md  # Split command diagrams
β”‚   β”œβ”€β”€ command-hierarchy-flow.md   # Context flow diagrams
β”‚   └── README.md                   # This file
β”œβ”€β”€ README.md                      # Main documentation
β”œβ”€β”€ pyproject.toml                 # Project configuration
└── quick_test.sh                  # Quick test script

πŸš€ Installation & Setup

# Clone repository
cd /Users/taylaand/code/personal/aws_net_shell

# Install dependencies
pip install -e .

# Run AWS network shell
aws-net-shell

# Or with specific profile
aws-net-shell -p production

πŸ§ͺ Testing

Run Tests

# Run all unit tests
pytest tests/

# Run with verbose output
pytest tests/ -v

# Quick test script
./quick_test.sh

Test Coverage

  • Root commands: 42 commands
  • Context commands: 35+ commands
  • Total coverage: 77+ commands
  • Contexts: 9 (global-network, vpc, transit-gateway, firewall, ec2-instance, elb, vpn, core-network, route-table)

πŸ“– Usage Examples

Basic Commands

aws-net> show vpcs
aws-net> show global-networks
aws-net> set vpc 1
aws-net> show detail

Context Navigation

# Enter VPC context
aws-net> set vpc 1
vpc> show subnets
vpc> show route-tables
vpc> exit

# Enter Transit Gateway context
aws-net> set transit-gateway 1
tgw> show attachments
tgw> show route-tables
tgw> exit

AWS Operations

# Trace between IPs
aws-net> trace 192.168.1.10 10.0.0.5

# Find IP address
aws-net> find_ip 10.1.32.100

# Find null routes
aws-net> find_null_routes

Cache Management

# Scenario: ELBs haven't finished provisioning yet
aws-net> show elbs
No load balancers found

# Wait for provisioning, then refresh cache
aws-net> refresh elb
Refreshed elb cache

# Now re-fetch will get latest data
aws-net> show elbs
┏━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━┓
┃ # ┃ Name      ┃ Type        ┃ State   ┃
┑━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━┩
β”‚ 1 β”‚ prod-alb  β”‚ application β”‚ active  β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

# Refresh current context data (when inside a context)
aws-net> set elb 1
elb:1> refresh
Refreshed elb cache

# Clear all caches
aws-net> refresh all
Cleared 5 cache entries

πŸ“Š Commands by Category

Cache Management (2)

  • clear_cache - Clear all cached data permanently
  • refresh [target|all] - Refresh cached data selectively
    • refresh - Refresh current context (e.g., in ELB context, clears ELB cache)
    • refresh elb - Clear and re-fetch ELB data on next show elbs
    • refresh vpcs - Clear and re-fetch VPC data
    • refresh all - Clear all caches globally

Show Commands (34)

  • Network Resources: vpcs, transit_gateways, firewalls, elbs, vpns
  • Compute: ec2-instances, enis
  • Connectivity: dx-connections, peering-connections
  • Security: security-groups, rule-groups, unused-sgs
  • DNS: resolver-endpoints, resolver-rules
  • Monitoring: network-alarms, alarms-critical
  • Global: global-networks, global-accelerators
  • System: config, cache, routing-cache

Set Commands (8 Contexts)

  • vpc, transit-gateway, global-network, core-network
  • firewall, ec2-instance, elb, vpn

Action Commands (9)

  • write <file>, trace <src> <dst>, find_ip <ip>
  • find_prefix <cidr>, find_null_routes
  • reachability, populate_cache, clear_cache
  • create-routing-cache, export_graph

Cache Commands Explained

populate_cache - General Topology Cache

Purpose: Pre-fetch ALL topology data across all modules for comprehensive analysis

What it caches:

  • VPCs, subnets, route tables, security groups, NACLs
  • Transit Gateways, attachments, peerings, route tables
  • Cloud WAN (global networks, core networks, segments, attachments)
  • EC2 instances, ENIs
  • ELBs, listeners, target groups
  • VPNs, Direct Connect connections, Firewalls
  • All discoverable resources across all regions

Use Case: Warm cache before comprehensive analysis or demos Duration: 30-60 seconds depending on account size

aws-net> populate_cache
Populating topology cache...
  β†’ Discovering VPCs...
  β†’ Discovering Transit Gateways...
  β†’ Discovering Cloud WAN...
  β†’ Discovering EC2 instances...
Cache populated

create_routing_cache - Specialized Routing Cache

Purpose: Build specialized cache of ONLY routing data for fast route lookups and analysis

What it caches:

  • VPC route table entries (all route tables across all VPCs)
  • Transit Gateway route table entries (all TGW route tables)
  • Cloud WAN route table entries (by core network, segment, and region)

Enables Commands:

  • find_prefix <cidr> - Find which route tables contain a prefix
  • find_null_routes - Find blackhole/null routes across all routing domains
  • show routing-cache <filter> - View cached routes with filtering

Use Case: Fast routing troubleshooting and analysis without fetching data Duration: 10-20 seconds

aws-net> create_routing_cache
Building routing cache...
  VPC routes: 373
  Transit Gateway routes: 50
  Cloud WAN routes: 300

View Cached Routes:

# Summary
aws-net> show routing-cache

# Detailed views
aws-net> show routing-cache vpc              # All VPC route table entries
aws-net> show routing-cache transit-gateway  # All Transit Gateway routes
aws-net> show routing-cache cloud-wan        # All Cloud WAN routes (by segment/region)
aws-net> show routing-cache all              # Everything (comprehensive view)

Comparison

Feature populate_cache create_routing_cache
Scope Everything Routes only
Purpose General topology Routing analysis
Speed Slower (30-60s) Faster (10-20s)
Data All resources Route tables only
Enables All show commands find_prefix, routing-cache commands
When to use Before exploration/demos Before routing troubleshooting

Recommendation:

  • Use populate_cache for general exploration and comprehensive analysis
  • Use create_routing_cache specifically for routing troubleshooting and prefix searches

πŸ”§ Configuration

Default configuration in pyproject.toml:

  • Timeout: 120 seconds per command
  • Regions: All enabled regions
  • Cache: Enabled by default
  • Output: Rich formatted tables

🎯 Development

Adding New Commands

  1. Add handler in src/aws_network_tools/shell/handlers/
  2. Update HIERARCHY dict in src/aws_network_tools/shell/base.py
  3. Run tests: pytest tests/

Testing New Features

# Run all tests
pytest tests/ -v

πŸ“¦ Dependencies

Core dependencies:

  • boto3: AWS SDK
  • rich: Terminal formatting
  • cmd2: Shell framework
  • pytest: Testing framework

See pyproject.toml for full dependency list.

πŸ“„ License

MIT License - see LICENSE file for details

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add comprehensive tests
  4. Submit a pull request

πŸ“ Changelog

2024-12-08

  • βœ… VPN tunnel inspection: show tunnels displays VgwTelemetry data with UP/DOWN status
  • βœ… VPN detail view: show detail includes tunnel summary with outside IPs and BGP routes
  • βœ… Debug logging: aws-net-runner --debug flag with comprehensive logging to /tmp/
  • βœ… Network Firewall enhancements: rule-group context with detailed rule inspection
  • βœ… Enhanced firewall commands: show firewall, show rule-groups with indexes
  • βœ… STATELESS rules: Complete display with ports, protocols, actions
  • βœ… STATEFUL rules: Suricata rule format display
  • βœ… Graph commands: stats, validate, mermaid, parent path lookup
  • βœ… Persistent routing cache with SQLite (save/load commands)
  • βœ… Enhanced routing cache display: vpc, transit-gateway, cloud-wan filters
  • βœ… Terminal width detection for proper Rich table rendering
  • βœ… aws-net-runner tool for programmatic shell execution

2024-12-05

  • βœ… ELB commands implementation (listeners, targets, health)
  • βœ… VPN context commands (detail, tunnels)
  • βœ… Firewall policy command
  • βœ… Core-network commands registration fix
  • βœ… Direct resource selection without show command
  • βœ… Automated issue resolution workflow
  • βœ… Consolidated CLI to aws-net-shell only
  • βœ… Multi-level context prompt fix
  • βœ… Comprehensive testing framework with pexpect integration
  • βœ… Graph-based command testing

2024-12-02

  • βœ… Comprehensive command graph with context navigation
  • βœ… Dynamic command discovery (78+ commands)
  • βœ… Command graph Mermaid diagrams
  • βœ… Test coverage: 77+ commands

About

aws_network_shell

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •