A robust script to switch between network sites by replacing one subnet with another in:
- Network Manager configuration (using nmcli)
- /etc/hosts entries
- Optionally Pacemaker cluster configuration
Imagine that your first site is on 192.168.something and the other location is on 172.23.something. You want to keep the local addressing (the "something" part) and just change the site by modifying Network Manager configuration, /etc/hosts and optionally also the Pacemaker.
- Automatically modifies NetworkManager connections using nmcli commands
- Updates IP addresses, gateways, DNS servers, and routes while preserving host portions
- Updates /etc/hosts entries matching the source subnet
- Optional Pacemaker configuration updates
- Comprehensive dry-run mode with mock data for safe testing
- Creates secure backups of modified files and configurations
- Structured Logging: Multi-level logging (DEBUG, INFO, SUCCESS, WARNING, ERROR) with timestamps
- Robust Error Handling: Comprehensive error handling with proper exit codes and cleanup
- Security Enhancements: Secure temporary file creation with proper permissions (600)
- Signal Handling: Graceful shutdown and cleanup on interruption
- Input Validation: Enhanced subnet validation with comprehensive IPv4 checking
- Modular Design: Well-structured code with focused functions for maintainability
- Designed to work on RHEL 8, RHEL 9, and RHEL 10
- Compatible with bash 4.4+ (RHEL 8) through bash 5.2+ (RHEL 10)
- Uses only features available across all target RHEL versions
- Color-coded output for better readability (auto-detects terminal support)
- Bash shell (version 4.4 or higher)
- Root privileges (when not using dry-run mode)
- NetworkManager with nmcli command
- Pacemaker cluster software (optional, only needed if using the Pacemaker update feature)
- Clone or download this repository
- Make the script executable:
chmod +x change-site.sh
./change-site.sh [options] <from_subnet> <to_subnet><from_subnet>- Source subnet (e.g., 192.168)<to_subnet>- Destination subnet (e.g., 172.23)
-h, --help- Show help message with examples-v, --version- Show version information-p, --pacemaker- Also update Pacemaker configuration-n, --dry-run- Show changes without applying them (safe testing mode)-b, --backup- Create backups of modified files--verbose- Enable verbose logging for debugging--config FILE- Use configuration file (future feature)
Preview changes (recommended first step):
./change-site.sh -n 192.168 172.23Switch from 192.168.x.x to 172.23.x.x with backups:
sudo ./change-site.sh -b 192.168 172.23Update all configurations including Pacemaker with verbose output:
sudo ./change-site.sh -p -b --verbose 192.168 172.23Test with verbose dry-run mode:
./change-site.sh --verbose --dry-run 192.168 172.23The script performs the following steps:
- Argument Parsing: Validates command-line arguments and options
- Input Validation: Comprehensive subnet format validation and range checking
- Dependency Checking: Verifies required tools and services (skipped in dry-run mode)
- Backup Creation: Creates secure backups of files and NetworkManager connections
- NetworkManager Updates:
- Finds connections with matching subnet addresses
- Updates IPv4 addresses, gateways, DNS servers, and routes
- Preserves the last two octets of IP addresses
- Restarts NetworkManager and re-applies connections
- Hosts File Updates: Updates /etc/hosts entries matching the source subnet
- Pacemaker Updates: Optionally updates Pacemaker cluster configuration
- Cleanup: Removes temporary files and performs graceful shutdown
The script uses standardized exit codes for better integration with automation tools:
0- Success1- Invalid arguments2- Permission denied3- Missing dependencies4- Operation failed5- Validation failed
The script provides comprehensive logging with multiple levels:
- DEBUG: Detailed execution information (use
--verbose) - INFO: General information about operations
- SUCCESS: Confirmation of successful operations
- WARNING: Non-fatal issues that should be noted
- ERROR: Fatal errors that prevent operation
Logs include timestamps and are written to /var/log/change-site.log when running with appropriate permissions.
- Always use the dry-run mode first to verify changes before applying them
- Create backups with the
-boption before making changes - Review the verbose output to understand what changes will be made
- Test in a non-production environment first
- Requires root privileges only for actual modifications (not dry-run)
- Creates temporary files with secure permissions (600)
- Validates all user inputs to prevent injection attacks
- Implements proper cleanup on exit or interruption
- Logs all operations for audit trails
- NetworkManager connections are exported before modification
- Files are backed up with timestamps in
/var/backups/change-site/ - Backup files have secure permissions (600)
- Failed operations preserve original configurations
The project includes comprehensive testing infrastructure organized in the tests/ directory:
# Run all test suites
./tests/run-tests.sh
# Run specific test suites
./tests/run-tests.sh --basic # Basic functionality tests
./tests/run-tests.sh --integration # Integration tests
./tests/run-tests.sh --enhanced # Enhanced tests with mock environments
# Run individual test files
./tests/test-change-site.sh # Basic functionality
./tests/test-integration.sh # Integration testing
./tests/test-change-site-enhanced.sh # Enhanced testing
# Test specific functionality with dry-run
./change-site.sh --verbose --dry-run 192.168 172.23- Basic Tests: Core functionality, syntax validation, argument parsing
- Integration Tests: Configuration files, rollback functionality, performance
- Enhanced Tests: Mock environments, multi-connection scenarios, memory testing
- Configuration file support and profile loading
- Rollback functionality with operation tracking
- Syntax validation and argument parsing
- Subnet format validation and error handling
- Dry-run functionality with mock data
- Multi-connection NetworkManager scenarios
- Performance benchmarking and memory usage
- RHEL 8-10 compatibility testing
- Security practices and file permissions
See tests/README.md for detailed testing documentation.
Complete project documentation is available in the docs/ directory:
- Project Status: Current project overview, features, and metrics
- Staging Testing Guide: Comprehensive staging environment testing procedures
- Release Notes: Detailed v1.0.0 release documentation
- Development Plans: Implementation plans and development documentation
For testing documentation, see tests/README.md.
Permission Denied Errors:
- Ensure you're running with
sudofor actual changes - Use dry-run mode (
-n) to test without requiring root
NetworkManager Not Found:
- Install NetworkManager:
sudo dnf install NetworkManager - Ensure NetworkManager service is running:
sudo systemctl start NetworkManager
Backup Directory Issues:
- Check disk space in
/var/backups/ - Verify write permissions to backup directory
Verbose Debugging:
# Enable verbose output for detailed debugging
./change-site.sh --verbose --dry-run 192.168 172.23The refactored script follows best practices with:
- Modular function design (all functions under 50 lines)
- Comprehensive error handling and cleanup
- Secure temporary file handling
- Proper signal handling
- No problematic global variables
- Extensive input validation
- Test changes with the comprehensive test suite
- Ensure RHEL 8-10 compatibility
- Follow the established coding patterns
- Add appropriate logging and error handling
- Update documentation for new features
- Enhanced monitoring and logging with structured JSON format
- Performance tracking and health monitoring capabilities
- Alert system with email notifications
- Interactive monitoring dashboard
- Comprehensive staging testing guide
- Complete project status documentation
- Organized documentation structure in docs/ directory
- Complete refactoring with modular design
- Enhanced security and error handling
- Comprehensive dry-run mode with mock data
- Structured logging system
- RHEL 8-10 compatibility improvements
- Comprehensive test suite
- Signal handling and cleanup
- Configuration file support and rollback functionality
- Original monolithic script
- Basic functionality only
- Limited error handling
This project is open source and available under the MIT License.
For issues, questions, or contributions:
- Check the troubleshooting section above
- Run with
--verboseflag for detailed debugging - Test with dry-run mode first
- Review the comprehensive test suite for examples