#!/bin/csh -f # change_server_hostname_and_ip # ----------------------------------------------------------------------------- # Shell script to change the hostname and internal IP address of a remote # server. # Run locally by Fred to update a remote server after cloning it from # another server, typically via an Amazon Web Services AMI. # ----------------------------------------------------------------------------- # Usage: See Usage section below or run with -h or --help option to see usage. # Assumptions: # - Assumes it was run from a local directory that contains etc/... and # var/... trees that contain the config files for the remote server. # - Assumes that all remote config files already exist. Overwrites them, # but refuses to create a new file if nothing to overwrite. # - Assumes sudo rights on the remote server # - Assumes tripwire is installed with its files in the standard locations # on the remote server # - Assumes commands on the remote server's PATH: diffcprm, tripwirereview # - Assumes commands on the local PATH: push_protected_file_to_server, sloop, # loop # Effects: # - Edits the local config files, updating hostname and IP address # - Pushes the config files to their corresponding server locations # - Updates software like tripwire to use the new files. # - Restarts the server to take on the new host name. # Notes: # Implementation Notes: # Portability Issues: # - Not very portable. Performs a specific useful task in Fred's typical # environment. # Revision History: # $Log$ # ----------------------------------------------------------------------------- if ($#argv < 7 || "$1" == "-h" || "$1" == "--help") then echo "Usage: $0:t external_IP old_hostname new_hostname old_domain_name new_domain_name old_internal_IP new_internal_IP" echo "external_IP = External IP address of server." echo " Example: 174.129.10.250" echo "old_hostname = Current hostname of server." echo " Example: amazon1" echo "new_hostname = Desired hostname of server." echo " Example: www1" echo "old_domain = Current domain name of server." echo " Example: bristle.com" echo "new_domain = Desired domain name of server." echo " Example: clsi.org" echo "old_internal_IP = Current internal IP address of server." echo " Example: 10.123.27.212" echo "new_internal_IP = Desired internal IP address of server." echo " Example: 10.250.18.132" exit 1 endif set external_IP=$1 set old_hostname=$2 set new_hostname=$3 set old_domain=$4 set new_domain=$5 set old_internal_IP=$6 set new_internal_IP=$7 set backup=".tmp" set file="./etc/sysconfig/network" if (-f ${file} && -w ${file}) then echo "" echo "" echo "" echo "Editing hostname in ${file}..." sed -i ${backup} -e "s/${old_hostname}/${new_hostname}/g" ${file} diff ${file}${backup} ${file} rm -iv ${file}${backup} else beep "${file} is not a plain writable file" exit 1 endif set file="./etc/hosts" if (-f ${file} && -w ${file}) then echo "" echo "" echo "" echo "Editing hostname in ${file}..." sed -i ${backup} -e "s/${old_hostname}/${new_hostname}/g" ${file} diff ${file}${backup} ${file} rm -iv ${file}${backup} echo "" echo "" echo "" echo "Editing domain name in ${file}..." sed -i ${backup} -e "s/${old_domain}/${new_domain}/g" ${file} diff ${file}${backup} ${file} rm -iv ${file}${backup} echo "" echo "" echo "" echo "Editing IP address in ${file}..." sed -i ${backup} -e "s/${old_internal_IP}/${new_internal_IP}/g" ${file} diff ${file}${backup} ${file} rm -iv ${file}${backup} else beep "${file} is not a plain writable file" exit 1 endif set file="./etc/tripwire/twpol.txt" if (-f ${file} && -w ${file}) then echo "" echo "" echo "" echo "Editing hostname in ${file}..." sed -i ${backup} -e "s/${old_hostname}/${new_hostname}/g" ${file} diff ${file}${backup} ${file} rm -iv ${file}${backup} else beep "${file} is not a plain writable file" exit 1 endif echo "" echo "" echo "" echo "These are the current hostname and IP address:" ssh -t ${external_IP} "hostname;hostname -f; ifconfig" echo "" echo "" echo "" echo "Pushing edited files to server and showing differences..." push_protected_file_to_server ${external_IP} ./etc/sysconfig/network /etc/sysconfig/network push_protected_file_to_server ${external_IP} ./etc/hosts /etc/hosts push_protected_file_to_server ${external_IP} ./etc/tripwire/twpol.txt /etc/tripwire/twpol.txt echo "" echo "" echo "" echo "Setting hostname temporarily so tripwire creates the right filenames..." ssh -t ${external_IP} sudo hostname ${new_hostname} echo "" echo "" echo "" echo "Deleting old tripwire files..." ssh -t ${external_IP} sudo rm -iv /etc/tripwire/site.key /etc/tripwire/${old_hostname}-local.key /var/lib/tripwire/${old_hostname}.twd echo "" echo "" echo "" echo "tripwire-setup-keyfiles..." ssh -t ${external_IP} sudo tripwire-setup-keyfiles echo "" echo "" echo "" echo "tripwire --init..." ssh -t ${external_IP} sudo tripwire --init echo "" echo "" echo "" echo "tripwire --check shows new tripwire db file..." ssh -t ${external_IP} sudo tripwire --check echo "" echo "" echo "" echo "tripwirereview to accept tripwire db file..." ssh -t ${external_IP} tripwirereview -c echo "" echo "" echo "" echo "tripwirereview to accept backup of tripwire db file..." ssh -t ${external_IP} tripwirereview -c echo "" echo "" echo "" echo "Rebooting so new hostname is permanent..." ssh -t ${external_IP} sudo shutdown -r now echo "" echo "" echo "" echo "Waiting for reboot with new hostname. Hit Ctrl-C when done..." sloop ssh -t ${external_IP} "hostname;hostname -f; ifconfig" exit 0