Remote script over ssh execution issues.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remote script over ssh execution issues.
# 1  
Old 06-04-2015
Remote script over ssh execution issues.

If I execute below code I am able to get string from column8 and column10 about a process.

Code:
serverA1$> ps -ef | grep rotate | grep 'config' | awk '{print $8" "$10}'
/<Oracle_home>/ohs/bin/odl_rotatelogs -h:/<app_Home>/config/OHS/ohs1/component_events.xml_ohs1
/<Oracle_home>/ohs/bin/odl_rotatelogs -h:/<app_Home>/config/OHS/ohs1/component_events.xml_ohs2
/<Oracle_home>/ohs/bin/odl_rotatelogs -h:/<app_Home>/config/OHS/ohs1/component_events.xml_ohs3


servers
Code:
serverA1
serverB1
serverC1

But when I try to execute below script to execute same command on multiple servers, I am getting "0"s instead of column8 and column10. I think block of lines execution syntax does not like --> $8" "$10 in awk.

Code:
 
#!/bin/bash
for server in `cat servers` ; do
sudo ssh $server "bash -s" << EOF
echo "server=$server"
ps -ef | grep rotate | grep 'config' | awk '{print $8" "$10}'
EOF
done

How can I print output from each server in this format?
Code:
--> $server, $column8, $column10


Moderator's Comments:
Mod Comment Please use CODE tags, not ICODE tags

Last edited by Scrutinizer; 06-04-2015 at 07:34 PM.. Reason: Additional code tags; changed code tags to code tags
# 2  
Old 06-04-2015
A workaround would be to bring the output of ps back and run your awk locally, like so:

Code:
 
 ssh ... << EOF | awk '{print $8,$10}'
 ps -ef ...
 EOF

Of course, this is not a right solution, just a workaround. Others would probably show you how to properly deal with $ expansion in this case.
# 3  
Old 06-04-2015
Quote:
Originally Posted by kchinnam
Code:
sudo ssh $server "bash -s" << EOF
echo "server=$server"
ps -ef | grep rotate | grep 'config' | awk '{print $8" "$10}'
EOF

I suppose the here-document goes to the "sudo"-command, not the "ssh"-command.

Anyways, the command as you wrote it must be wrong (or, to be precise, overly complicated). Whenever a "grep" is piped into another "grep" and/or "awk" then something is to be done:

Code:
ps -ef | 'awk '/rotate/ { /config/ {print $8" "$10}}'

should do the same.

Btw. i suppose the "sudo" is there for the wrong purpose, because i doubt that only "root" is allowed to do a "ssh". You probably want to connect as "root", which you can achieve in two ways:

Code:
ssh root@"$server" .....
ssh -l root $server ...

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 4  
Old 06-05-2015
Code:
ps -ef | 'awk '/rotate/ { /config/ {print $8" "$10}}'   # quote before awk causes error
 bash: line 2: unexpected EOF while looking for matching `''
bash: line 3: syntax error: unexpected end of file

 ps -ef | awk '/rotate/ { /config/ {print $8" "$10}}'   # causes below error
 awk: /rotate/ { /config/ {print " "0}}
awk:                     ^ syntax error

 ps -ef | awk '/rotate/ {print $8" "$10}' # causes below empty result..
 0
 0
 0


Please help with this..

Last edited by Corona688; 06-05-2015 at 03:05 PM..
# 5  
Old 06-05-2015
On a system where the ps -ef output conforms to POSIX standard requirements, the command:
Code:
ps -ef|awk '/rotate/ && /config/{print $8,$10}'

would print the command name and the 2nd argument passed to that command for any commands that contained the strings rotate and config, but that doesn't seem to be what you're showing us. Also note that the standards allow the command and its arguments to be truncated to an implementation-defined line length limit. (And, this could cause the strings you're looking for to be dropped from the ps output you're processing.) Your version of ps may provide options to set various line length limits to control that truncation. What output do you get from the command:
Code:
ps -ef|grep rotate

What are you expecting to find in fields 8 and 10 in the ps output?
# 6  
Old 06-06-2015
Don, I am trying to find inventory of what OHS(oracle http server) instances are up and running on each server. We have around 200 ohs instances on 20 servers. I can get these details on each server using awk, please check my initial post. But same command does not work over "ssh" using $> ssh command <<EOF ... EOF syntax.
I think this syntax treats $ and "" differently than executing directly on the server.. How can get same result over SSH also?
# 7  
Old 06-06-2015
You'll need to escape the $ signs lest they be expanded by your local shell. Try
Code:
ssh $server "bash -s" << EOF
ps -ef | awk -v"server=$server"  '/rotate/ && /config/ {print server, \$8" "\$10}'
EOF

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exiting the Remote server after script execution

Hi All , I'm running a script abc.sh in server "host1" shown as below : #! /bin/bash sh stop.sh ssh user@$host2 "/home/user/prod_work/xyz.sh; sh start.sh The problem I am facing is , the control is not passed to host1 after executing the script "xyz.sh" in host2 . ... (12 Replies)
Discussion started by: Pradeep_1990
12 Replies

2. Shell Programming and Scripting

Shell Script execution issues

Hi, There's a shell script by name "download", which has been created as user "mgr" and that script needs to executed as user "dev". I tried giving privileges 701 on the script download. But it's throwing the error message bin]$ ./download /bin/bash: ./download: Permission denied ... (6 Replies)
Discussion started by: venkatesh17
6 Replies

3. Shell Programming and Scripting

output from remote server during execution of a script

How to see the output from remote server during execution of a script ? I am executing a script (ls) from machine 1 but the o/p should be displayed in machine 2. Can I achieve this ? Example:- Machine 1:- # ls Machine 2:- (console) file1 file2 file 3 dir1 dir2 (0 Replies)
Discussion started by: frintocf
0 Replies

4. HP-UX

Remote ssh execution and .profile issues

Greetings, i'm currently having issues in successfully executing a script from one server to other, and i'm cracking my nut in understanding why. Let's get started with the default info: Server A: briozzo@A:/home/briozzo $ uname -a HP-UX A B.11.31 U ia64 2787251109 unlimited-user license ... (3 Replies)
Discussion started by: nbriozzo
3 Replies

5. Programming

Perl script remote execution as another user

Hi gurus, I have a requirement where I need to remotely run a perl script as another user. Running the script locally as the required user is fine, however I need to su with the script due to filesystem permission issues. I do not want to update permissions on the remote server due to security... (5 Replies)
Discussion started by: melias
5 Replies

6. Cybersecurity

Log remote execution over SSH

If a user execute commands remotely over ssh : $ ssh USERNAME@SERVER COMMANDSHow the SERVER administrator can log those COMMANDS executed in a "not a tty" session ? I searched for my question and get the following suggestions:Anybody give help how to do this ? what the content of "/bin/bash "... (1 Reply)
Discussion started by: new0h
1 Replies

7. Shell Programming and Scripting

help with remote execution of a script

does anyone know how can i execute a script which i locally run as " . /etc/local/host/src.srvr -D ." need to execute above command in rexec command. if i put the command as it is it does not run. Sorry but i am naive in scripting. Thanks rexec sgplqim -l vau -n ' ' (0 Replies)
Discussion started by: NK4U
0 Replies

8. Shell Programming and Scripting

ssh can't back from remote host during script execution

Hi all I wrote a script to execute a script on several remote hosts, but somehow during the execution of the local script, ssh can't come back from the remote host, so that causes my local script hanging... I use the below command to do the job in the local script, any idea? ssh... (12 Replies)
Discussion started by: bzylg
12 Replies

9. Shell Programming and Scripting

problem with remote execution of script using telnet

Hi all, i am trying to remotely execute a script from a different server. this is the code that i use : #!bin/sh pwd (sleep 1 echo "username" sleep 2 echo "pwd" sleep 2 echo "cd /path/to/file" if then echo "script1.sh" echo "mailx -s "Task Executed"... (1 Reply)
Discussion started by: sais
1 Replies

10. Shell Programming and Scripting

Issues using ssh from crontab to run remote script from

I have a solaris9 x86 server using ssh as follows: SSH Version Sun_SSH_1.0, protocol versions 1.5/2.0. The remote server solaris9 sparc has exactly the same version ssh installed. I am running a script on my server which includes the following command to run a script on the remote server:... (4 Replies)
Discussion started by: frustrated1
4 Replies
Login or Register to Ask a Question