# Network Module Examples

The net module provides utilities for network connectivity testing. This collection demonstrates practical patterns for TCP port scanning, HTTP health checks, and comprehensive network diagnostics.

## Available Functions

### TCP Functions

- `net_tcp_check(host, port)` - Check if a TCP port is open (returns bool)
- `net_tcp_ping(host)` - Ping a host using ICMP (returns bool)

### HTTP Functions

- `net_http_check(url)` - Check if a URL is reachable (returns bool, 2xx status)
- `net_http_status(url)` - Get HTTP status code (returns i64, -1 on error)

## Examples

| File | Description | Use Case |
|------|-------------|----------|
| `01_tcp_check.rhai` | Basic TCP port connectivity checks | Quick port availability tests, service discovery |
| `02_http_check.rhai` | HTTP/HTTPS endpoint reachability and status codes | Website monitoring, health checks |
| `04_advanced_port_scanning.rhai` | Multi-category port scanning with analysis | Comprehensive port inventory, service mapping |
| `05_http_health_checks.rhai` | Multi-endpoint health monitoring with reporting | Service health dashboards, SLA monitoring |
| `06_network_diagnostics.rhai` | Integrated TCP, HTTP, and connectivity analysis | Complete network assessment, diagnostics |

## Example Details

### 01_tcp_check.rhai
Basic TCP connectivity testing. Demonstrates:
- Single port checks with timeout handling
- Batch port scanning for common services
- ICMP ping testing

### 02_http_check.rhai
HTTP endpoint testing with status interpretation. Demonstrates:
- URL reachability checks
- HTTP status code retrieval and interpretation
- Multiple endpoint batch checking
- Health check pattern implementation

### 04_advanced_port_scanning.rhai
Sophisticated port scanning patterns. Demonstrates:
- Organized port scanning by service category
- Port status aggregation
- Multi-host comparison
- Results analysis and summary reporting

### 05_http_health_checks.rhai
Production-grade health monitoring. Demonstrates:
- Multi-endpoint health assessment
- Status code interpretation
- Category-based results grouping
- Health percentage calculation
- Comprehensive health reporting

### 06_network_diagnostics.rhai
Complete network diagnostic suite. Demonstrates:
- Multi-phase diagnostic workflow (TCP, HTTP, connectivity)
- TCP connectivity analysis with service categorization
- HTTP/HTTPS endpoint testing with status interpretation
- Host reachability assessment
- Network health scoring (0-100% scale)
- Detailed diagnostic reporting with visual formatting

## Running Examples

```bash
# Run individual examples
herodo packages/os/rhaiexamples/net/01_tcp_check.rhai

# Run all network examples
for file in packages/os/rhaiexamples/net/*.rhai; do
    echo "Running $file..."
    herodo "$file"
done
```

## Configuration Notes

### TCP Checks
- Default timeout: 5 seconds
- Works with both IP addresses and hostnames
- ICMP ping requires appropriate OS permissions

### HTTP Checks
- Default timeout: 10-30 seconds (depending on operation)
- Follows HTTP status codes (2xx = success)
- Status code -1 indicates connection failure

## Best Practices

1. **Error Handling**: Network timeouts are expected; handle gracefully
2. **Batch Operations**: Use loops for multiple checks rather than sequential calls
3. **Status Interpretation**: Implement custom logic for your specific status codes
4. **Performance**: Long timeouts add up; use appropriate values for your use case

## Performance Considerations

- Each TCP check uses a 5-second timeout
- Each HTTP check uses a 10-30 second timeout
- Scanning multiple hosts/ports can be time-consuming
- Consider parallel execution strategies for large-scale scanning
