Shell script help to execute ssh remote commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script help to execute ssh remote commands
# 1  
Old 01-31-2014
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 .

Code:
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 .

Code:
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
# 2  
Old 01-31-2014
From the manual pages on RHEL 6.3 (you don't say what you have, so yours may be different:-
Quote:
-q Quiet mode. Causes most warning and diagnostic messages to be suppressed.
Might you be suppressing your output?



Robin
# 3  
Old 01-31-2014
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).
Code:
Proc_Exist=`ssh -q -o "BatchMode=yes" -o "PasswordAuthentication=no" $OAUSER@${Primary_Node} "ps -ef 
| grep -v grep | grep '${ICM_Proc}' |wc -l" `

Code:
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}"

# 4  
Old 02-03-2014
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.

Code:
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}'

# 5  
Old 02-03-2014
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:-
Code:
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
# 6  
Old 02-03-2014
Quote:
Originally Posted by Y.balakrishna

${ADMIN_SCIRPTS_HOME}

ADMIN_SCIRPTS_HOME should be ADMIN_SCRIPTS_HOME, or did you misspell this everywhere in your application ?
# 7  
Old 02-03-2014
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 Smilie

Code:
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

Last edited by Y.balakrishna; 02-03-2014 at 07:13 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to execute setDomainEnv.sh in wblogic via ssh on remote server in shell script?

How to execute setDomainEnv.sh in wblogic via ssh on remote server in shell script we execute setDomainEnv.sh manually as . ./setDomainEnv.sh from its location ie /opt/SP/users/domian/bin" My approach is not working. Please advise. #!/bin/bash set -x base="/opt/SP/users/d1/bin"... (5 Replies)
Discussion started by: abhaydas
5 Replies

2. Shell Programming and Scripting

How to execute commands on remote server using expect script?

I need to copy python script file to around 100 servers using expect script. 1. Copy script to my user home first(/home/myhome) on each remote server 2. change permissions on copied file to 766. 3. sudo to appuser1 account on remote server. copy script file from my user home to /usr/bin/... (1 Reply)
Discussion started by: kchinnam
1 Replies

3. Shell Programming and Scripting

How to execute application commands with shell script?

Hi Friends, We have a application "getstatus" which needs to run on application prompt. But it is not working with shell script. Example: Unix Root Prompt# execute "crp" to go to application prompt CRP>getstatus displays output of app health CRP>exit Unix Root Prompt# Script , which... (2 Replies)
Discussion started by: suresh3566
2 Replies

4. Shell Programming and Scripting

Execute shell script on remote machine

I want to execute a shell script(set of commands) on remote machine and that script takes input from text file(local machine). Please refer below: ssh user@hostname 'bash -s'< ./test.sh file.txt But i got the error file.txt doesn't exist. Can anyone help me on this. Content of test.sh: ... (2 Replies)
Discussion started by: manishtri88
2 Replies

5. Shell Programming and Scripting

bash script to execute a command remote servers using ssh

Hello, I am running into few issues, please suggest me what I am missing. I am running this script on a linux host. Main idea of this script is to, login to each host via ssh and get uid of user, service user that I trying to run this script, has already deployed ssh keys and provide sudo... (8 Replies)
Discussion started by: bobby320
8 Replies

6. Shell Programming and Scripting

Using ssh to execute a remote script in the background

Help please!! I want to use ssh to execute a remote exe and while it's running I want to query for the process ID of the exe (2 different ssh commands) 1. sshpass -p "<passwd>" ssh -f -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@<ipaddress> nohup /tmp/mmds_asyn & 2.... (0 Replies)
Discussion started by: rvompoluTMW
0 Replies

7. UNIX for Dummies Questions & Answers

how to stay in remote shell after executing commands in ssh?

the ssh calling convention: ssh <server> If I put commands in the section, ssh will execute them immediately after logging in and return to local shell. I want to stay in the remote shell after executing these commands. How can I achieve this? Thanks for all. (1 Reply)
Discussion started by: hplonlien
1 Replies

8. Shell Programming and Scripting

Execute ssh commands through bash script

Hi all! I am trying to write a script that will check if a certain directory is available at several different nodes and then do stuff in it ..... On the beginning of the script I give as a variable the directory and the number of the nodes and then I loop like this: for... (3 Replies)
Discussion started by: idet2
3 Replies

9. Shell Programming and Scripting

Can BASH execute commands on a remote server when the commands are embedded in shell

I want to log into a remote server transfer over a new config and then backup the existing config, replace with the new config. I am not sure if I can do this with BASH scripting. I have set up password less login by adding my public key to authorized_keys file, it works. I am a little... (1 Reply)
Discussion started by: bash_in_my_head
1 Replies

10. Shell Programming and Scripting

how to execute shell commands in awk script?

Greetings Friends, Suppose I wish to invoke some shell level commands inside an awk script how to do that? say I wish to use : "wc" on a record to count number of characters in a record Any clues? Thanks, Rishi (1 Reply)
Discussion started by: RishiPahuja
1 Replies
Login or Register to Ask a Question