Shell script to connect to a servers

Hi Team,

I have an requirement on connecting to different linux servers using a shell script.
I have server1 and Server2.

I need to connect to server1 using ssh , check if a file exists if exists then connect to server2 and perform some action say load data.

Is it possible using  a shell script. Any help is really appreciated
sam_2012Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sam_2012Author Commented:
#!/usr/bin/ksh

#connect to server 1
ssh root@server1.com << EOF
passwd

if ( -e $filename );
then
0
sam_2012Author Commented:
#!/usr/bin/ksh

#connect to server 1
ssh root@server1.com << EOF
passwd

if ( -e $filename );
then
ssh root@server2.com << EOF1
passwd
loaddata
EOF1
EOF

the above code gives me an error.
0
nociSoftware EngineerCommented:
First setup public private key logins (that makes password obsolete).
then  execute all from the commandline....

ssh user@server1    ksh   -c " if  [ -e filename ] ; then ; ssh root@server2 loadddata; fi"
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

murugesandinsShell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NTCommented:
Sample script (tested only using localhost, 127.0.0.1 and local inet address)
Also change related path for
/bin/ping
/usr/bin/ssh
/usr/bin/ssh-keygen
/usr/bin/scp => Not used by this script, you can use if required.
/bin/chmod
/bin/ls
/usr/bin/egrep
/sbin/ip
/usr/bin/awk
/bin/sed

Open in new window

#!/bin/ksh
#Write related comment at required places.
#Change SERVER1 to related server having fully qualified domain name
ERRFOUND=""
for RequiredFiles in /bin/ping \
/usr/bin/ssh \
/usr/bin/ssh-keygen \
/usr/bin/scp \
/bin/chmod \
/bin/ls \
/usr/bin/egrep \
/sbin/ip \
/usr/bin/awk \
/bin/sed
do
	if [[ ! -f $RequiredFiles ]]
	then
		echo "$RequiredFiles No such file"
		ERRFOUND=1
	fi
done
if [[ ! -s "$ERRFOUND" ]]
then
	if [[ ! -d ~/.ssh ]]
	then
		/bin/mkdir ~/.ssh
	fi
	if [[ ! -f ~/.ssh/id_rsa ]]
	then
		/usr/bin/ssh-keygen -t rsa -b 1024 -N ""
	fi
	if [[ ! -f ~/.ssh/authorized_keys ]]
	then
		/bin/cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
	fi
	/bin/ls -ld  ~/.ssh | /usr/bin/grep -E "^drwx\-\-\-\-\-\-" >/dev/null 2>&1
	Chk700Permission=$?
	if [[ 0 -ne $Chk700Permission ]]
	then
		echo /bin/chmod 700 ~/.ssh
		/bin/chmod 700 ~/.ssh
	fi
	/bin/ls -ld  ~/.ssh/id_rsa | /usr/bin/grep -E "^\-rw\-\-\-\-\-\-\-" >/dev/null 2>&1
	Chk600Permission=$?
	if [[ 0 -ne $Chk600Permission ]]
	then
		echo /bin/chmod 600 ~/.ssh/id_rsa
		/bin/chmod 600 ~/.ssh/id_rsa
	fi
	if [[ ! -f ~/.ssh/authorized_keys ]]
	then
		echo /bin/cp -ip ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
		/bin/cp -ip ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
	fi
	/bin/ls -ld  ~/.ssh/authorized_keys | /usr/bin/grep -E "^\-rw\-r\-\-r\-\-" >/dev/null 2>&1
	Chk600Permission=$?
	if [[ 0 -ne $Chk600Permission ]]
	then
		echo /bin/chmod 600 ~/.ssh/authorized_keys
		/bin/chmod 600 ~/.ssh/authorized_keys
	fi
	ETH_DEV_NAME=`/sbin/ip -f inet addr | /usr/bin/awk -F: '{ printf( "%s\n", $2);}' | /bin/egrep -v "^$| lo" | /bin/sed "s/ //;"`
	IP=`/sbin/ip -f inet addr show dev $ETH_DEV_NAME | /usr/bin/grep -w inet | /usr/bin/awk '{ print $2}' | /bin/sed "s/\/.*//;"`
	/sbin/ip -f inet addr | /usr/bin/grep -w "$SERVER1" >/dev/null 2>&1
	SERVER1=127.0.0.1	#tested localhost 127.0.0.1
	#SERVER1=localhost	#tested localhost
	#SERVER1=$IP		#tested $IP	  localhost
	SERVER_IS_LOCAL=$?
	/bin/ping -c 1 $SERVER1 >/dev/null
	PingRet=$?
	if [[ 0 -eq $PingRet ]]
	then
		if [[ 0 -ne $SERVER_IS_LOCAL ]]
		then
			#Handle RequiredFiles at $SERVER1
			/usr/bin/ssh $SERVER1 "if [[ ! -d ~/.ssh ]];then echo /bin/mkdir ~/.ssh;/bin/mkdir ~/.ssh;echo /bin/chmod 700 ~/.ssh;/bin/chmod 700 ~/.ssh;fi;if [[ ! -f ~/.ssh/id_rsa || ! -f ~/.ssh/id_rsa.pub ]];then echo y | /usr/bin/ssh-keygen -t rsa -b 2048 -N '' -f ~/.ssh/id_rsa;fi;if [[ ! -f ~/.ssh/authorized_keys ]];then /bin/cp -p ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys;/bin/chmod 600 ~/.ssh/id_rsa ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys;fi;if [[ -f ~/.kshrc ]];then echo \"~/.kshrc    file is  present at $SERVER1\";else ~/.kshrc not found;fi;if [[ -f ~/.notfound ]];then echo \"~/.notfound is  present\";else echo \"~/.notfound is not present   at $SERVER1\";fi"
			SSHRet=$?
			if [[ 0 -ne $SSHRet ]]
			then
				echo "/usr/bin/ssh $SERVER1 failed"
			else
				echo "/usr/bin/ssh Action passed   at $SERVER1"
			fi
		else
			/usr/bin/ssh $SERVER1 "if [[ -f ~/.kshrc ]];then echo \"~/.kshrc    file is  present at $SERVER1\";else ~/.kshrc not found;fi;if [[ -f ~/.notfound ]];then echo \"~/.notfound is  present\";else echo \"~/.notfound is not present   at $SERVER1\";fi"
			SSHRet=$?
			if [[ 0 -ne $SSHRet ]]
			then
				echo "/usr/bin/ssh $SERVER1 failed"
			else
				echo "/usr/bin/ssh Action passed   at $SERVER1"
			fi
		fi
	else
		echo "/bin/ping -c 1 $SERVER1 failed"
	fi
fi

Open in new window

Sample result
./29088730_ssh_to_other_Server.sh
Generating public/private rsa key pair.
Enter file in which to save the key (/home/murugesandins/.ssh/id_rsa):
Your identification has been saved in /home/murugesandins/.ssh/id_rsa.
Your public key has been saved in /home/murugesandins/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:bEe6vc1+Og6Ky/omwSMm4x/sTvgnRuQo3ulWntp0Gio murugesandins@Enterprise
The key's randomart image is:
+---[RSA 1024]----+
|                 |
|                 |
|          .      |
|   .   . o       |
|  +.    S .      |
|+.=o+. . +       |
|+=o=+=... o      |
| E+**==. . =. .  |
|  BO=B=.. .o*+   |
+----[SHA256]-----+
/bin/chmod 700 /home/murugesandins/.ssh
The authenticity of host '11.128.220.64 (11.128.220.64)' can't be established.
ECDSA key fingerprint is SHA256:ODeaQmZm2pvpJkXMDDuiQEhFIXPeEy/ejs1jnsaVmG4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '11.128.220.64' (ECDSA) to the list of known hosts.
~/.kshrc    file is  present at 11.128.220.64
~/.notfound is not present   at 11.128.220.64
/usr/bin/ssh Action passed   at 11.128.220.64

Open in new window

Given script using no password => -N ""
you can change based on requirement.
0
sam_2012Author Commented:
Hi Sir,
I understand that you are trying to generate the rsa key and then logging into the server and checking the files. But I need some explanation on the below codes. It would really help me

Can you explain why these files required?

for RequiredFiles in /bin/ping \
/usr/bin/ssh \
/usr/bin/ssh-keygen \
/usr/bin/scp \
/bin/chmod \
/bin/ls \
/usr/bin/egrep \
/sbin/ip \
/usr/bin/awk \
/bin/sed
do
      if [[ ! -f $RequiredFiles ]]
      then
            echo "$RequiredFiles No such file"
            ERRFOUND=1
      fi
done


--- Below lines what are we trying to achieve

-- If no error then
if [[ ! -s "$ERRFOUND" ]]
then
   # if .ssh directory does not exists then create it . Why do we need to create it.
      if [[ ! -d ~/.ssh ]]
      then
            /bin/mkdir ~/.ssh
      fi

       #if there is no rsa then generate it

      if [[ ! -f ~/.ssh/id_rsa ]]
      then
            /usr/bin/ssh-keygen -t rsa -b 1024 -N ""
      fi

       # if no authorization keys then copy from the above step
      if [[ ! -f ~/.ssh/authorized_keys ]]
      then
            /bin/cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
      fi

      # what does this command do? Why do i need to check the directory permission

      /bin/ls -ld  ~/.ssh | /usr/bin/grep -E "^drwx\-\-\-\-\-\-" >/dev/null 2>&1

      Chk700Permission=$?

      if [[ 0 -ne $Chk700Permission ]]
      then
            echo /bin/chmod 700 ~/.ssh
            /bin/chmod 700 ~/.ssh
      fi

      /bin/ls -ld  ~/.ssh/id_rsa | /usr/bin/grep -E "^\-rw\-\-\-\-\-\-\-" >/dev/null 2>&1

      Chk600Permission=$?

      if [[ 0 -ne $Chk600Permission ]]
      then
            echo /bin/chmod 600 ~/.ssh/id_rsa
            /bin/chmod 600 ~/.ssh/id_rsa
      fi

      if [[ ! -f ~/.ssh/authorized_keys ]]
      then
            echo /bin/cp -ip ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
            /bin/cp -ip ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
      fi

      /bin/ls -ld  ~/.ssh/authorized_keys | /usr/bin/grep -E "^\-rw\-r\-\-r\-\-" >/dev/null 2>&1
      Chk600Permission=$?

      if [[ 0 -ne $Chk600Permission ]]
      then
            echo /bin/chmod 600 ~/.ssh/authorized_keys
            /bin/chmod 600 ~/.ssh/authorized_keys
      fi
0
sam_2012Author Commented:
Hi Noci,

Can you help why do I need to create a subshell to check for a file?

ssh user@server1    ksh   -c " if  [ -e filename ] ; then ; ssh root@server2 loadddata; fi"
0
murugesandinsShell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NTCommented:
1. Can you explain why these files required?
Verify that the full path of executable are present in current host. Since they are used inside the script (excluding scp).
Written scp as a sample, which you can add for further actions.

2. Below lines what are we trying to achieve
            Script initially validate existence of required executable.
            SERVER1 can be localhost also (handling that exception too)
            if localhost not having ~/.ssh directory create that directory
            if ~/.ssh/id_rsa file is not present create that using /usr/bin/ssh-keygen mentioning -N "" no password (man ssh-keygen)
            /usr/bin/ssh-keygen creates following files:
                     ~/.ssh/id_rsa
                     ~/.ssh/id_rsa.pub
            change the permission 700 for ~/.ssh directory
            change the permission 600 for ~/.ssh/id_rsa and ~/.ssh/authorized_keys files
            create ~/.ssh/authorized_keys using ~/.ssh/id_rsa.pub file => can be changed using /bin/cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
            if SERVER1 is localhost no need to generate id_rsa files.
            Given sample to validate existence of files at SERVER1

2. -- If no error then
if no error, /usr/bin/ssh SERVER1
3. #if there is no rsa then generate it
Yes
4. # if no authorization keys then copy from the above step
   >> can be changed using /bin/cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
   this is required if when using /usr/bin/ssh to more servers
5. what does this command do? Why do i need to check the directory permission
    Required to handle hackers.

If those files are not found:
handle exceptions => example: /bin/ping or /usr/bin/ping => /usr/bin/which ping
/bin/ping
/usr/bin/ssh
/usr/bin/ssh-keygen
/usr/bin/scp
/bin/chmod
/bin/ls
/usr/bin/egrep or /bin/
/sbin/ip
/usr/bin/awk
/bin/sed
Updated code for egrep exception and removed scp
#!/bin/ksh
#Write related comment at required places.
#Change SERVER1 to related server having fully qualified domain name
ERRFOUND=""
for RequiredFiles in /bin/ping \
/usr/bin/ssh \
/usr/bin/ssh-keygen \
/bin/chmod \
/usr/bin/grep \
/bin/ls \
/sbin/ip \
/usr/bin/awk \
/bin/sed
do
	if [[ ! -f $RequiredFiles ]]
	then
		echo "$RequiredFiles No such file"
		ERRFOUND=1
	fi
done
if [[ ! -s "$ERRFOUND" ]]
then
	if [[ ! -d ~/.ssh ]]
	then
		/bin/mkdir ~/.ssh
	fi
	if [[ ! -f ~/.ssh/id_rsa ]]
	then
		/usr/bin/ssh-keygen -t rsa -b 1024 -N ""
	fi
	if [[ ! -f ~/.ssh/authorized_keys ]]
	then
		/bin/cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
	fi
	/bin/ls -ld  ~/.ssh | /usr/bin/grep -E "^drwx\-\-\-\-\-\-" >/dev/null 2>&1
	Chk700Permission=$?
	if [[ 0 -ne $Chk700Permission ]]
	then
		echo /bin/chmod 700 ~/.ssh
		/bin/chmod 700 ~/.ssh
	fi
	/bin/ls -ld  ~/.ssh/id_rsa | /usr/bin/grep -E "^\-rw\-\-\-\-\-\-\-" >/dev/null 2>&1
	Chk600Permission=$?
	if [[ 0 -ne $Chk600Permission ]]
	then
		echo /bin/chmod 600 ~/.ssh/id_rsa
		/bin/chmod 600 ~/.ssh/id_rsa
	fi
	if [[ ! -f ~/.ssh/authorized_keys ]]
	then
		echo /bin/cp -ip ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
		/bin/cp -ip ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
	fi
	/bin/ls -ld  ~/.ssh/authorized_keys | /usr/bin/grep -E "^\-rw\-r\-\-r\-\-" >/dev/null 2>&1
	Chk600Permission=$?
	if [[ 0 -ne $Chk600Permission ]]
	then
		echo /bin/chmod 600 ~/.ssh/authorized_keys
		/bin/chmod 600 ~/.ssh/authorized_keys
	fi
	ETH_DEV_NAME=`/sbin/ip -f inet addr | /usr/bin/awk -F: '{ printf( "%s\n", $2);}' | /usr/bin/grep -E -v "^$| lo" | /bin/sed "s/ //;"`
	IP=`/sbin/ip -f inet addr show dev $ETH_DEV_NAME | /usr/bin/grep -w inet | /usr/bin/awk '{ print $2}' | /bin/sed "s/\/.*//;"`
	/sbin/ip -f inet addr | /usr/bin/grep -w "$SERVER1" >/dev/null 2>&1
	#SERVER1=127.0.0.1	#tested localhost 127.0.0.1
	#SERVER1=localhost	#tested localhost
	SERVER1=$IP		#tested $IP	  localhost
	SERVER_IS_LOCAL=$?
	/bin/ping -c 1 $SERVER1 >/dev/null
	PingRet=$?
	if [[ 0 -eq $PingRet ]]
	then
		if [[ 0 -ne $SERVER_IS_LOCAL ]]
		then
			#Handle RequiredFiles at $SERVER1
			/usr/bin/ssh $SERVER1 "if [[ ! -d ~/.ssh ]];then echo /bin/mkdir ~/.ssh;/bin/mkdir ~/.ssh;echo /bin/chmod 700 ~/.ssh;/bin/chmod 700 ~/.ssh;fi;if [[ ! -f ~/.ssh/id_rsa || ! -f ~/.ssh/id_rsa.pub ]];then echo y | /usr/bin/ssh-keygen -t rsa -b 2048 -N '' -f ~/.ssh/id_rsa;fi;if [[ ! -f ~/.ssh/authorized_keys ]];then /bin/cp -p ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys;/bin/chmod 600 ~/.ssh/id_rsa ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys;fi;if [[ -f ~/.kshrc ]];then echo \"~/.kshrc    file is  present at $SERVER1\";else ~/.kshrc not found;fi;if [[ -f ~/.notfound ]];then echo \"~/.notfound is  present\";else echo \"~/.notfound is not present   at $SERVER1\";fi"
			SSHRet=$?
			if [[ 0 -ne $SSHRet ]]
			then
				echo "/usr/bin/ssh $SERVER1 failed"
			else
				echo "/usr/bin/ssh Action passed   at $SERVER1"
			fi
		else
			/usr/bin/ssh $SERVER1 "if [[ -f ~/.kshrc ]];then echo \"~/.kshrc    file is  present at $SERVER1\";else ~/.kshrc not found;fi;if [[ -f ~/.notfound ]];then echo \"~/.notfound is  present\";else echo \"~/.notfound is not present   at $SERVER1\";fi"
			SSHRet=$?
			if [[ 0 -ne $SSHRet ]]
			then
				echo "/usr/bin/ssh $SERVER1 failed"
			else
				echo "/usr/bin/ssh Action passed   at $SERVER1"
			fi
		fi
	else
		echo "/bin/ping -c 1 $SERVER1 failed"
	fi
fi

Open in new window

0
sam_2012Author Commented:
Hi Sir,

Why do i need to check again for the id_rsa while logging into the server1 using ssh

usr/bin/ssh $SERVER1 "if [[ ! -d ~/.ssh ]];then echo /bin/mkdir ~/.ssh;/bin/mkdir ~/.ssh;echo /bin/chmod 700 ~/.ssh;/bin/chmod 700 ~/.ssh;fi;if [[ ! -f ~/.ssh/id_rsa || ! -f ~/.ssh/id_rsa.pub ]];then echo y | /usr/bin/ssh-keygen -t rsa -b 2048 -N '' -f ~/.ssh/id_rsa;fi;if [[ ! -f ~/.ssh/authorized_keys ]];then /bin/cp -p ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys;/bin/chmod 600 ~/.ssh/id_rsa ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys;fi;if [[ -f ~/.kshrc ]];then echo \"~/.kshrc    file is  present at $SERVER1\";else ~/.kshrc not found;fi;if [[ -f ~/.notfound ]];then echo \"~/.notfound is  present\";else echo \"~/.notfound is not present   at $SERVER1\";fi"
                  SSHRet=$?
                  if [[ 0 -ne $SSHRet ]]
                  then
                        echo "/usr/bin/ssh $SERVER1 failed"
                  else
                        echo "/usr/bin/ssh Action passed   at $SERVER1"
                  fi
0
sam_2012Author Commented:
My requirement is

Need to log into the server 1 , check for the file name if exists then log into server2 run the script to loaddata and come out.
0
murugesandinsShell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NTCommented:
Update sample code executing /bin/pwd at SERVER2
#!/bin/ksh
#Write related comment at required places.
#Change SERVER1 to related server having fully qualified domain name
#Change SERVER2 to related server having fully qualified domain name
ERRFOUND=""
for RequiredFiles in /bin/ping \
/usr/bin/ssh \
/usr/bin/ssh-keygen \
/bin/chmod \
/usr/bin/grep \
/bin/ls \
/sbin/ip \
/usr/bin/awk \
/bin/sed
do
        if [[ ! -f $RequiredFiles ]]
        then
                echo "$RequiredFiles No such file"
                ERRFOUND=1
        fi
done
if [[ ! -s "$ERRFOUND" ]]
then
        if [[ ! -d ~/.ssh ]]
        then
                /bin/mkdir ~/.ssh
        fi
        if [[ ! -f ~/.ssh/id_rsa ]]
        then
                /usr/bin/ssh-keygen -t rsa -b 1024 -N ""
        fi
        if [[ ! -f ~/.ssh/authorized_keys ]]
        then
                /bin/cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
        fi
        /bin/ls -ld  ~/.ssh | /usr/bin/grep -E "^drwx\-\-\-\-\-\-" >/dev/null 2>&1
        Chk700Permission=$?
        if [[ 0 -ne $Chk700Permission ]]
        then
                echo /bin/chmod 700 ~/.ssh
                /bin/chmod 700 ~/.ssh
        fi
        /bin/ls -ld  ~/.ssh/id_rsa | /usr/bin/grep -E "^\-rw\-\-\-\-\-\-\-" >/dev/null 2>&1
        Chk600Permission=$?
        if [[ 0 -ne $Chk600Permission ]]
        then
                echo /bin/chmod 600 ~/.ssh/id_rsa
                /bin/chmod 600 ~/.ssh/id_rsa
        fi
        if [[ ! -f ~/.ssh/authorized_keys ]]
        then
                echo /bin/cp -ip ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
                /bin/cp -ip ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
        fi
        /bin/ls -ld  ~/.ssh/authorized_keys | /usr/bin/grep -E "^\-rw\-r\-\-r\-\-" >/dev/null 2>&1
        Chk600Permission=$?
        if [[ 0 -ne $Chk600Permission ]]
        then
                echo /bin/chmod 600 ~/.ssh/authorized_keys
                /bin/chmod 600 ~/.ssh/authorized_keys
        fi
        ETH_DEV_NAME=`/sbin/ip -f inet addr | /usr/bin/awk -F: '{ printf( "%s\n", $2);}' | /usr/bin/grep -E -v "^$| lo" | /bin/sed "s/ //;"`
        IP=`/sbin/ip -f inet addr show dev $ETH_DEV_NAME | /usr/bin/grep -w inet | /usr/bin/awk '{ print $2}' | /bin/sed "s/\/.*//;"`
        /sbin/ip -f inet addr | /usr/bin/grep -w "$SERVER1" >/dev/null 2>&1
        #SERVER1=127.0.0.1      #tested localhost 127.0.0.1
        #SERVER1=localhost      #tested localhost
        SERVER1=$IP             #tested $IP       localhost
        SERVER2=$IP
        SERVER_IS_LOCAL=$?
        SERVER_IS_LOCAL=1
        echo SERVER1 /bin/ping -c 1 $SERVER1
        /bin/ping -c 1 $SERVER1 >/dev/null
        PingRet=$?
        if [[ 0 -eq $PingRet ]]
        then
                echo "/bin/ping at SERVER1 [ $SERVER1 ] PASS"
                if [[ 0 -ne $SERVER_IS_LOCAL ]]
                then
                        #Handle RequiredFiles at $SERVER1
                        /usr/bin/ssh $SERVER1 "if [[ ! -d ~/.ssh ]];then echo /bin/mkdir ~/.ssh;/bin/mkdir ~/.ssh;echo /bin/chmod 700 ~/.ssh;/bin/chmod 700 ~/.ssh;fi;if [[ ! -f ~/.ssh/id_rsa || ! -f ~/.ssh/id_rsa.pub ]];then echo y | /usr/bin/ssh-keygen -t rsa -b 2048 -N '' -f ~/.ssh/id_rsa;fi;if [[ ! -f ~/.ssh/authorized_keys ]];then /bin/cp -p ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys;/bin/chmod 600 ~/.ssh/id_rsa ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys;fi;if [[ -f ~/loaddata ]];then echo \"~/loaddata    file is  present at SERVER1 [ $SERVER1 ]\";echo SERVER2 /bin/ping -c 1 $SERVER2;/bin/ping -c 1 $SERVER2 >/dev/null;PingRet=\$?;if [[ 0 -eq \$PingRet ]];then echo /bin/ping at SERVER2 [ $SERVER2 ] PASS;/usr/bin/ssh $SERVER2 \"if [[ ! -d ~/.ssh ]];then echo /bin/mkdir ~/.ssh;/bin/mkdir ~/.ssh;echo /bin/chmod 700 ~/.ssh;/bin/chmod 700 ~/.ssh;fi;if [[ ! -f ~/.ssh/id_rsa || ! -f ~/.ssh/id_rsa.pub ]];then echo y | /usr/bin/ssh-keygen -t rsa -b 2048 -N '' -f ~/.ssh/id_rsa;fi;if [[ ! -f ~/.ssh/authorized_keys ]];then /bin/cp -p ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys;/bin/chmod 600 ~/.ssh/id_rsa ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys;fi;echo /bin/pwd at $SERVER2 \$PWD;\";else /bin/ping $SERVER2 FAIL;fi;else echo ~/loaddata not found at \$SERVER2;fi;"
                        SSHRet=$?
                        if [[ 0 -ne $SSHRet ]]
                        then
                                echo "/usr/bin/ssh $SERVER1 failed"
                        else
                                echo "/usr/bin/ssh Action passed   at $SERVER1"
                        fi
                else
                        /usr/bin/ssh $SERVER1 "if [[ -f ~/loaddata ]];then echo \"~/loaddata    file is  present at $SERVER1\";else ~/loaddata not found;fi;if [[ -f ~/.notfound ]];then echo \"~/.notfound is  present\";else echo \"~/.notfound is not present   at $SERVER1\";fi"
                        SSHRet=$?
                        if [[ 0 -ne $SSHRet ]]
                        then
                                echo "/usr/bin/ssh $SERVER1 failed"
                        else
                                echo "/usr/bin/ssh Action passed   at $SERVER1"
                        fi
                fi
        else
                echo "/bin/ping -c 1 $SERVER1 failed"
        fi
fi

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sam_2012Author Commented:
awesome . Thanks a lot.
1
murugesandinsShell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NTCommented:
In previous code, SERVER_IS_LOCAL=1 was hard coded since it was used for testing.
Reason: Remote system is not available here.

Hence
Replace:
SERVER1=$IP             #tested $IP
SERVER2=$IP
SERVER_IS_LOCAL=$?
SERVER_IS_LOCAL=1
echo SERVER1 /bin/ping -c 1 $SERVER1
With:
SERVER1=$IP             #tested $IP
SERVER2=$IP
SERVER_IS_LOCAL=$?
# SERVER_IS_LOCAL=1 # used for testing
echo SERVER1 /bin/ping -c 1 $SERVER1

Also use related IP for SERVER2 and SERVER1
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.