#!/bin/sh
clear
printf "+============================================================================+\n";
printf "|                                   ***                                      |\n"; 
printf "| You should run this script as root !!!!                                    |\n";
printf "| Script will work only if you have .ssh directory on your HOME directory    |\n";
printf "|    				    ***	                                     |\n";
printf "| Script is designed to udatesoftware at cluster nodes and .   		     |\n"; 
printf "| It is not designed to do any damage in the network But you have 	     |\n";	
printf "| to know what you are doing...	     					     |\n";
printf "|                                   ***                                      |\n"; 
printf "+============================================================================+ \n";
printf "Checking your network...\n";

int=`route | grep default | awk '{print $NF}'`;
net_address=`/sbin/ifconfig  $int | grep Bcast | awk -F : '{print $2}'| awk '{print $1}'`;

printf "Your IP address is : $net_address \n";
printf "Checking who you are...\n";

check_user=`whoami`;

printf "You are $check_user \n";
printf "==============================================================================\n";
printf "Enter your target subnet in following format 0.0.0.* :"; read target_subnet;

if [ $check_user = "root" ];
        then
                {

			for i in `nmap -sP $target_subnet|grep Host |awk '{print $2}'`;
			do
				echo ""|telnet $i 22|grep "Connected"|awk '{print $3}'|cut -d "." -f 1,2,3,4 >> ~/temp_ip_file;
			done

			printf "\n This script will update software for these hosts \n"; 
			printf "=================================================\n"
			cat ~/temp_ip_file; 
			printf "=================================================\n"	
			printf " of your choice and, do you want to proceed ? :";  read choice;

                        if [ $choice = "y" ];
                        then
			{

				for each in `cat ~/temp_ip_file`; do
				{
					ssh $each "apt-get update && apt-get dist-upgrade -y &apt-get clean" 
					ssh $each "apt-get update && apt-get dist-upgrade -y &apt-get clean" >> ~/debug`date +%d_%b_%Y`
				}
				done
				rm -f ~/temp_ip_file;
			}
			else
				{
	                        if [ -e ~/temp_ip_file ];
        		        then
                        	        rm -f ~/temp_ip_file;
	                        fi
                        	exit 1;

				}
			fi	
		}
	else
		{
			printf "\n You are not root !!! \n"	
			if [ -e ~/temp_ip_file;];
			then
				rm -f ~/temp_ip_file;
			fi
			exit 1;
		}

fi

