# 982 Router CGNAT Migration Script - SAFE VERSION
# This version adds the new configuration alongside the old one
# Allows gradual migration without service interruption
#
# Migration: 100.64.32.0/22 -> 100.64.48.0/20
# Matches management subnet pattern (10.10.48.0/20)

# PREPARATION PHASE - Run these first
{
    # Add new CGNAT pool with expanded range
    /ip pool add name=cgnat-new ranges=100.64.48.1-100.64.63.199 comment="New /20 CGNAT pool"
    
    # Add new IP address (keep old one for now)
    /ip address add address=100.64.63.254/20 interface=982 comment="New CGNAT gateway /20"
    
    # Create temporary PPP profile for migration
    /ppp profile add name=982-migrate local-address=100.64.63.253 remote-address=cgnat-new comment="Migration profile"
    
    # Print current state
    :put "New CGNAT configuration added. Current state:"
    /ip pool print where name~"cgnat"
    /ip address print where interface=982
    /ppp profile print where name~"982"
}

# TESTING PHASE - Test with one client
{
    # Change one PPPoE client to test
    # Replace 'testclient' with actual username
    # /ppp secret set [find name="testclient"] profile=982-migrate
    
    # Have client reconnect and verify they get IP from new range
    # Check with: /ppp active print where name="testclient"
}

# MIGRATION PHASE - Run during maintenance window
{
    # Update PPPoE server to use new profile for new connections
    /interface pppoe-server server set [find name=982] default-profile=982-migrate
    
    # Force all clients to reconnect (will cause brief outage)
    # /ppp active remove [find]
    
    # Or disconnect clients gradually:
    # :foreach i in=[/ppp active find] do={
    #     /ppp active remove $i
    #     :delay 1s
    # }
}

# VERIFICATION COMMANDS
{
    # Check active connections
    :put "Active PPPoE connections by IP range:"
    :put "Old range (100.64.32.x): $([:len [/ppp active find where address~"100.64.32"]])"
    :put "Old range (100.64.33.x): $([:len [/ppp active find where address~"100.64.33"]])"
    :put "Old range (100.64.34.x): $([:len [/ppp active find where address~"100.64.34"]])"
    :put "Old range (100.64.35.x): $([:len [/ppp active find where address~"100.64.35"]])"
    :put "New range (100.64.48-63.x): $([:len [/ppp active find where address~"100.64.[45][0-9]"]])"
}

# CLEANUP PHASE - Only run after ALL clients migrated
{
    # Remove old configuration
    # /ip address remove [find address="100.64.35.254/22"]
    # /ip pool remove [find name=cgnat]
    # /ppp profile remove [find name=982]
    
    # Rename new items to standard names
    # /ip pool set [find name=cgnat-new] name=cgnat comment="982 CGNAT pool /20"
    # /ppp profile set [find name=982-migrate] name=982 comment=""
    # /ip address set [find address="100.64.63.254/20"] comment="982 CGNAT gateway"
}

# ROLLBACK COMMANDS - If something goes wrong
{
    # Revert PPPoE server to old profile
    # /interface pppoe-server server set [find name=982] default-profile=982
    
    # Remove new configuration
    # /ip address remove [find address="100.64.63.254/20"]
    # /ip pool remove [find name=cgnat-new]
    # /ppp profile remove [find name=982-migrate]
}