# Remote script over ssh execution issues.

**URL:** <https://community.unix.com/t/remote-script-over-ssh-execution-issues/353131>\
**Category:** Shell Programming and Scripting\
**Created:** [June 4, 2015, 6:26pm UTC](https://community.unix.com/t/remote-script-over-ssh-execution-issues/353131 "2015-06-04T18:26:08Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![kchinnam](https://community.unix.com/letter_avatar/kchinnam/32/6_449c533f586939b6e5f246632a46421f.png) [@kchinnam](https://community.unix.com/u/kchinnam)\
**Post date:** [June 4, 2015, 6:26pm UTC](https://community.unix.com/t/remote-script-over-ssh-execution-issues/353131/1 "2015-06-04T18:26:08Z")

</div>

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

```nohighlight
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

```nohighlight
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.

```nohighlight
 
#!/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?

```nohighlight
--> $server, $column8, $column10

```

---

<div class="post-metadata">

**Author:** ![migurus](https://community.unix.com/letter_avatar/migurus/32/6_449c533f586939b6e5f246632a46421f.png) [@migurus](https://community.unix.com/u/migurus)\
**Post date:** [June 4, 2015, 8:13pm UTC](https://community.unix.com/t/remote-script-over-ssh-execution-issues/353131/2 "2015-06-04T20:13:53Z")

</div>

A workaround would be to bring the output of ps back and run your awk locally, like so:

```nohighlight
 
 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.

---

<div class="post-metadata">

**Author:** ![bakunin](https://community.unix.com/letter_avatar/bakunin/32/6_449c533f586939b6e5f246632a46421f.png) [@bakunin](https://community.unix.com/u/bakunin)\
**Post date:** [June 4, 2015, 8:46pm UTC](https://community.unix.com/t/remote-script-over-ssh-execution-issues/353131/3 "2015-06-04T20:46:02Z")

</div>

> [@kchinnam](#):
>
> ```plaintext
> 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:

```plaintext
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:

```plaintext
ssh root@"$server" .....
ssh -l root $server ...

```

I hope this helps.

bakunin

---

<div class="post-metadata">

**Author:** ![kchinnam](https://community.unix.com/letter_avatar/kchinnam/32/6_449c533f586939b6e5f246632a46421f.png) [@kchinnam](https://community.unix.com/u/kchinnam)\
**Post date:** [June 5, 2015, 1:50pm UTC](https://community.unix.com/t/remote-script-over-ssh-execution-issues/353131/4 "2015-06-05T13:50:21Z")

</div>

```nohighlight
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..

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [June 5, 2015, 3:36pm UTC](https://community.unix.com/t/remote-script-over-ssh-execution-issues/353131/5 "2015-06-05T15:36:20Z")

</div>

On a system where the `ps -ef` output conforms to POSIX standard requirements, the command:

```nohighlight
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:

```nohighlight
ps -ef|grep rotate

```

What are you expecting to find in fields 8 and 10 in the `ps` output?

---

<div class="post-metadata">

**Author:** ![kchinnam](https://community.unix.com/letter_avatar/kchinnam/32/6_449c533f586939b6e5f246632a46421f.png) [@kchinnam](https://community.unix.com/u/kchinnam)\
**Post date:** [June 6, 2015, 2:32am UTC](https://community.unix.com/t/remote-script-over-ssh-execution-issues/353131/6 "2015-06-06T02:32:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [June 6, 2015, 3:41am UTC](https://community.unix.com/t/remote-script-over-ssh-execution-issues/353131/7 "2015-06-06T03:41:04Z")

</div>

You'll need to escape the ` $ ` signs lest they be expanded by your local shell. Try

```nohighlight
ssh $server "bash -s" << EOF
ps -ef | awk -v"server=$server" '/rotate/ && /config/ {print server, \$8" "\$10}'
EOF

```

---

<div class="post-metadata">

**Author:** ![kchinnam](https://community.unix.com/letter_avatar/kchinnam/32/6_449c533f586939b6e5f246632a46421f.png) [@kchinnam](https://community.unix.com/u/kchinnam)\
**Post date:** [June 8, 2015, 2:14pm UTC](https://community.unix.com/t/remote-script-over-ssh-execution-issues/353131/8 "2015-06-08T14:14:19Z")

</div>

Your code is now escaping special characters and printing server name. But it is also printing below line after each server's output.

```nohighlight
 /<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
 serverA1 awk server=serverA1
 

```

How do I suppress that?

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [June 8, 2015, 11:51pm UTC](https://community.unix.com/t/remote-script-over-ssh-execution-issues/353131/9 "2015-06-08T23:51:47Z")

</div>

Or, if you quote EOF all expansion is disabled, this does leave you to resolve server on the remote side perhaps you could use hostname like this:

```nohighlight
ssh $server "bash -s" << "EOF"
ps -ef | awk -v server=$(hostname -s) '/[r]otate/ && /config/ {print server, $8" "$10}'
EOF

```

---

<div class="post-metadata">

**Author:** ![kchinnam](https://community.unix.com/letter_avatar/kchinnam/32/6_449c533f586939b6e5f246632a46421f.png) [@kchinnam](https://community.unix.com/u/kchinnam)\
**Post date:** [June 9, 2015, 2:58pm UTC](https://community.unix.com/t/remote-script-over-ssh-execution-issues/353131/10 "2015-06-09T14:58:18Z")

</div>

```nohighlight
server=$(hostname -s)

```

is resolving hostname to local hostname.

Below code is working.. but I am not sure how its working, can you explain?

```nohighlight
 
 ps -ef | awk -v "server=$server" '/[r]otate/ && /config/ {print server, \$8" "\$10}'
 

```

it is filtering out

```nohighlight
serverA1 awk server=serverA1

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [June 9, 2015, 3:22pm UTC](https://community.unix.com/t/remote-script-over-ssh-execution-issues/353131/11 "2015-06-09T15:22:22Z")

</div>

That's the `awk` process itself. It should not be matched due to the `[r]` brackets. Are you sure you used above command pipe?

Add a ` 1 ` between closing brace and closing quote and post the result.

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [June 9, 2015, 4:16pm UTC](https://community.unix.com/t/remote-script-over-ssh-execution-issues/353131/12 "2015-06-09T16:16:10Z")

</div>

The quotes around `"EOF"` are important, they stop this local-side expansion of the `$()` command substitution eg:

```nohighlight
$ hostname -s
MyPC

$ ssh MyServer <<EOF
   echo $(hostname -s)
EOF
MyPC

$ ssh MyServer <<"EOF"
  echo $(hostname -s)
EOF
MyServer

```

---

<div class="post-metadata">

**Author:** ![kchinnam](https://community.unix.com/letter_avatar/kchinnam/32/6_449c533f586939b6e5f246632a46421f.png) [@kchinnam](https://community.unix.com/u/kchinnam)\
**Post date:** [June 25, 2015, 11:10pm UTC](https://community.unix.com/t/remote-script-over-ssh-execution-issues/353131/13 "2015-06-25T23:10:16Z")

</div>

This is really useful, I am having fun with this capability..
