Shell script help to execute ssh remote commands

Hi,

The below command is not giving me the count , Can somebody help me in re-writing this pls .

Proc_Exist=`ssh -q -o "BatchMode=yes" -o "PasswordAuthentication=no" $OAUSER@${Primary_Node} ps -ef |
 grep -v grep | grep "${ICM_Proc}" |wc -l `

Also the same problem with below command as well .

ssh -q -o "BatchMode=yes" -o "PasswordAuthentication=no" $OAUSER@${Primary_Node} 
""$ADMIN_SCIRPTS_HOME/adcmctl.sh stop apps/${AppsPass}" 
|| "$COMMON_TOP/admin/scripts/${CONTEXT_NAME}/adcmctl.sh stop apps/${AppsPass}""

Regards
Bala

From the manual pages on RHEL 6.3 (you don't say what you have, so yours may be different:-

Might you be suppressing your output?

Robin

You must put the remote command in quotes, so the local shell sees a string.
E.g. in double-quotes so "$var" is substituted in the string (on the local host).

Proc_Exist=`ssh -q -o "BatchMode=yes" -o "PasswordAuthentication=no" $OAUSER@${Primary_Node} "ps -ef 
| grep -v grep | grep '${ICM_Proc}' |wc -l" `
ssh -q -o "BatchMode=yes" -o "PasswordAuthentication=no" $OAUSER@${Primary_Node} 
"$ADMIN_SCIRPTS_HOME/adcmctl.sh stop apps/${AppsPass} 
|| $COMMON_TOP/admin/scripts/${CONTEXT_NAME}/adcmctl.sh stop apps/${AppsPass}"

Hi,

I'd like to execute .profile as well as i need this variable ${AppsPass} value to be substituted variables dynamically but the below code is not executing it properly.

ssh -q -o BatchMode=yes -o PasswordAuthentication=no XXXXXXXX@XXXXXXX '. ~/.profile 
2>/dev/null;${ADMIN_SCIRPTS_HOME}/adcmctl.sh stop apps/${AppsPass} 
||${COMMON_TOP}/admin/scripts/${CONTEXT_NAME}/adcmctl.sh stop apps/${AppsPass}'

You might be better to have a script on the target server that:-

  1. Calls .profile
  2. Executes the command

That way, your ssh is kept neater. Is this an option, or will there be a wide variety of calls, of which this is a test case, and you might want to repeat it for many other target servers.

It's all in the quoting to make sure that the right shell evaluates the variables.

What errors are you getting back?
Can you simplify the ssh call to just display what would be executed so you can see what is getting passed where, and as what value?

Perhaps something more like:-

ssh username@target '. ./.profile ; print "ADMIN_SCIRPTS_HOME=$ADMIN_SCIRPTS_HOME
  AppsPass=$AppsPass
  COMMON_TOP=$COMMON_TOP
  CONTEXT_NAME=CONTEXT_NAME"

Of course, you might just have a spelling mistake in ADMIN_SCIRPTS_HOME and that would be an easy fix.

Robin

ADMIN_SCIRPTS_HOME should be ADMIN_SCRIPTS_HOME, or did you misspell this everywhere in your application ?

Hi Robin,

Yes,I'm calling .profile and executing ssh command in one line but as per my experience i've to include .profile in single quotes only then it works but otherway around in dobule quotes variables are not getting expanded because of single quotes.

I've tried even simpler. As i said above problem with single quotes and double quotes :frowning:

stop=`$COMMON_TOP/admin/scripts/$CONTEXT_NAME/adcmctl.sh stop apps/$AppsPass`
ssh -q -o BatchMode=yes -o PasswordAuthentication=no XXXX@XXXXXX '. ~/.profile 2>/dev/null; "${stop}"'

./test.sh: line 2: /admin/scripts/XXXXXXXX/adcmctl.sh: No such file or directory

I've fixed Admin_scripts_home spelling already sorry that was a typo above.

Regards
Bala

So, have you tried my suggestion to display what is or not getting set, and are you getting any output that would help?

Did you consider the spelling of your variable?

Robin

The ~ expansion only works in bash and ksh.
Because the first shell on the remote system starts in the user's home directory, one can safely do

. .profile

NB: .profile is made for interactive logins, so bares a risk of malfunction.

Hi,

I've tried the below code this .profile is being executed but its not expanding this variable value ${AppsPass}.

ssh -q -o BatchMode=yes -o PasswordAuthentication=no XXXXX@XXXXXX '. ./.profile 2>/dev/null;"sh $ADMIN_SCRIPTS_HOME/adcmctl.sh stop apps/${AppsPass}"||"sh $COMMON_TOP/admin/scripts/$CONTEXT_NAME/adcmctl.sh stop apps/${AppsPass}"'

Regards
Bala

Please, for each variables on the remote system, say if they are to be defined from the calling system or by the .profile on the remote system!

Hi All,

I've solved the problem somehow .. thanks for all your efforts last but not least.

I'd like to limit my search of below text only to last 3 lines if its finds it should print if not it should exist .. Can anybody help that.

grep "Shutting down concurrent managers" adcmctl.txt | tail -1

Change the order, so the grep is the last component in the pipe, and determines the exit status that if can use

if tail -3 adcmctl.txt | grep "Shutting down concurrent managers"
then
  : # nothing to add
else
  exit # not found
fi
1 Like

Thanks Germany .. this idea didn't cross my rusty mind. thank you so much.