Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I just want to check whether following code will work or not. It is like a validation

su -oracle "ssh  oracle@$MY_IP -o 'BatchMode=yes' -o 'ConnectionAttempts=1'"
returnCode=$?

echo "$returnCode"
 if [ $returnCode != 0 ]
 then
  echo "Configuration is not valid"
  return 1;
 else
  echo "Configuration is  valid"
  return 0;
 fi

When I run my script, it just gives me empty prompt. I can not enter to if statement. it seems script is trying to connect MY_IP.

How can I validate my statement without changing user and connecting to IP.

share|improve this question
1  
So you are trying to ensure you can still login without logging in? –  Mike Pennington Jun 17 '12 at 22:54
    
I am not sure if it is really possible but yeah. –  mibzer Jun 17 '12 at 22:55
    
What are the most probably events you are guarding against? Examples: Network failures, pam failures, sshd failures? –  Mike Pennington Jun 17 '12 at 22:57
    
I am just trying to make a health check procedure, it will run everyday and report if ssh is not working... –  mibzer Jun 17 '12 at 22:59
    
It works for me in Linux (are you using Solaris?). For my test I did: export MY_IP="192.168.246.1" ; su mvaldez -c "ssh mvaldez@$MY_IP -o 'BatchMode=yes' -o 'ConnectionAttempts=1'" ; echo $? –  MV. Jun 17 '12 at 23:27

1 Answer 1

up vote 3 down vote accepted

The only test to ensure that you can reach the other machine and log in there is to log in. Run a command that does nothing.

su -c "ssh  oracle@$MY_IP -o 'BatchMode=yes' -o 'ConnectionAttempts=1' true" oracle
share|improve this answer
    
thank you it worked :) –  mibzer Jun 17 '12 at 23:44
    
I'm not sure I see the need to su here... your ssh command is already using the oracle user –  Mike Pennington Jun 17 '12 at 23:51
1  
@MikePennington I left that in place. Presumably only the local oracle user has a private key that's authorized to log in as the remote oracle user, or there's some other authentication mechanism that requires oracle to run ssh. –  Gilles Jun 18 '12 at 0:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.