edit /etc/hosts entry on mulitipule machines

Today I would like to share a bash program that I wrote to make it very quick to easily change the print client on a bunch of unix print clients.  How it works is that all the print clients are told to print to prtserv and then in the /etc/hosts file you uncomment the print server that will be seeing the print clients today.

There are three different options, where you have all print clients using server1, all print clients using server2, which is good for doing maintenance on one of the print servers.  Then there is the normal state where the production servers are using one print server and the development print clients are using the other print server.

The program is used in the fowling format.

Program_name server1

To tell all print clients to use the server1 print server.

#!/bin/bash
#
# Written By:Bryon
# Purpose: to change print server for print clients
#only allow one running instance, else quit
if [ -e  $PWD/running-change_print_client_server ]; then
        echo "Danger, Will Robinson! Danger! change_print_client_server script is currently running  if this is not true then delete running-change_print_cli
ent_server file"
        exit
else
        echo "this is the only instance that is running"
fi
touch  $PWD/running-change_print_client_server


server1() {
echo "setting all print clinets to print to server1"
LIST[++i]="$(cat print_clients_server1)"
LIST[++i]="$(cat print_clients_server2)"
echo ${PRINTCLIENTS[@]}
for PRINTCLIENTS in ${LIST[@]};
do

RETURN=`ssh lpmgr@$PRINTCLIENTS   "egrep '292.99.26.191 server1.bigco.com server1 prtserv|292.99.34.191 server2.bigco.com server2 prtserv'   /etc/hosts|wc -l|xargs"`  2>  /dev/null
if [ "$RETURN" = "2" ]; then
     echo "starting to modify $PRINTCLIENTS to use server1"
     ssh -n lpmgr@$PRINTCLIENTS sudo "perl -pi -e 's/.*292.99.26.191 server1.bigco.com server1 prtserv/292.99.26.191 server1.bigco.com server1 prtserv/g' /etc/hosts"
     ssh -n lpmgr@$PRINTCLIENTS sudo "perl -pi -e 's/.*292.99.34.191 server2.bigco.com server2 prtserv/#292.99.34.191 server2.bigco.com server2 prtserv/g' /etc/hosts"
     echo "done modifing $PRINTCLIENTS to use server1"

else
      echo -e "\e[0;31m $PRINTCLIENTS has a error in it's /etc/hosts setup please fix before the change is made  \e[0m"

fi
done

}

server2() {
echo "setting all print clients to print to server2"
LIST[++i]="$(cat print_clients_server1)"
LIST[++i]="$(cat print_clients_server2)"
#echo ${PRINTCLIENTS[@]}
for PRINTCLIENTS in ${LIST[@]}
do

RETURN=`ssh lpmgr@$PRINTCLIENTS   "egrep '292.99.26.191 server1.bigco.com server1 prtserv|292.99.34.191 server2.bigco.com server2 prtserv'   /etc/hosts|wc -l|xargs"`  2>  /dev/null
if [ "$RETURN" = "2" ]; then
     echo "starting to modify $PRINTCLIENTS to use server2"
     ssh -n lpmgr@$PRINTCLIENTS sudo "perl -pi -e 's/.*292.99.26.191 server1.bigco.com server1 prtserv/#292.99.26.191 server1.bigco.com server1 prtserv/g' /etc/hosts"
     ssh -n lpmgr@$PRINTCLIENTS sudo "perl -pi -e 's/.*292.99.34.191 server2.bigco.com server2 prtserv/292.99.34.191 server2.bigco.com server2 prtserv/g' /etc/hosts"
     echo "done modifing $PRINTCLIENTS to use server2"

else
     echo -e "\e[0;31m $PRINTCLIENTS has a error in it's /etc/hosts setup please fix before the change is made  \e[0m"

fi
done

}

normal() {
echo "Setting 1295 print clients to use server2"
LIST2[++i]="$(cat print_clients_server2)"
for PRINTCLIENTS in ${LIST2[@]}
        do
RETURN=`ssh lpmgr@$PRINTCLIENTS   "egrep '292.99.26.191 server1.bigco.com server1 prtserv|292.99.34.191 server2.bigco.com server2 prtserv'   /etc/hosts|wc -l|xargs"`  2>  /dev/null
if [ "$RETURN" = "2" ]; then
     echo "starting to modify $PRINTCLIENTS to use server2"
        ssh -n lpmgr@$PRINTCLIENTS sudo "perl -pi -e 's/.*292.99.26.191 server1.bigco.com server1 prtserv/#292.99.26.191 server1.bigco.com server1 prtserv/g' /etc/hosts"
        ssh -n lpmgr@$PRINTCLIENTS sudo "perl -pi -e 's/.*292.99.34.191 server2.bigco.com server2 prtserv/292.99.34.191 server2.bigco.com server2 prtserv/g' /etc/hosts"

     echo "done modifing $PRINTCLIENTS to use server2"

else
     echo -e "\e[0;31m $PRINTCLIENTS has a error in it's /etc/hosts setup please fix before the change is made  \e[0m"

fi
done

echo "Setting 128 first street needham  print clients to use server1"
LIST1[++i]="$(cat print_clients_server1)"
for PRINTCLIENTS in ${LIST1[@]}
        do
RETURN=`ssh lpmgr@$PRINTCLIENTS   "egrep '292.99.26.191 server1.bigco.com server1 prtserv|292.99.34.191 server2.bigco.com server2 prtserv'   /etc/hosts|wc -l|xargs"`  2>  /dev/null
if [ "$RETURN" = "2" ]; then
     echo "starting to modify $PRINTCLIENTS to use server1"
        ssh -n lpmgr@$PRINTCLIENTS sudo "perl -pi -e 's/.*292.99.26.191 server1.bigco.com server1 prtserv/292.99.26.191 server1.bigco.com server1 prtserv/g' /etc/hosts"
        ssh -n lpmgr@$PRINTCLIENTS sudo "perl -pi -e 's/.*292.99.34.191 server2.bigco.com server2 prtserv/#292.99.34.191 server2.bigco.com server2 prtserv/g' /etc/hosts"

     echo "done modifing $PRINTCLIENTS to use server1"

else
     echo -e "\e[0;31m $PRINTCLIENTS has a error in it's /etc/hosts setup please fix before the change is made  \e[0m"

fi
done


}

#
case "$1" in
  server1)
        server1
        ;;
  server2)
        server2
        ;;
  normal)
       normal
        ;;
  *)
        echo "Where you you like to redirect the print jobs to?"
        echo "Example: change_print_client_server (server1|server2|normal)"
esac
        rm $PWD/running-change_print_client_server
exit