1

I an trying to pull cpu data on PMD's (Poll Mode Drivers) from remote servers and this is what it should look like:

pidstat -t -p `pidof ovs-vswitchd` 1 1 | grep -E pmd\|%CPU 01:48:19 PM UID TGID TID %usr %system %guest %CPU CPU Command 01:48:20 PM 997 - 28553 0.00 0.00 0.00 0.00 14 |__pmd8 01:48:20 PM 997 - 28555 100.00 0.00 0.00 100.00 27 |__pmd9 01:48:20 PM 997 - 28556 100.00 1.00 0.00 100.00 38 |__pmd10 01:48:20 PM 997 - 28557 0.00 0.00 0.00 0.00 39 |__pmd12 01:48:20 PM 997 - 28558 100.00 0.00 0.00 100.00 2 |__pmd11 01:48:20 PM 997 - 28559 100.00 0.00 0.00 100.00 15 |__pmd14 01:48:20 PM 997 - 28560 100.00 1.00 0.00 100.00 3 |__pmd13 01:48:20 PM 997 - 28561 100.00 0.00 0.00 100.00 26 |__pmd15 Average: UID TGID TID %usr %system %guest %CPU CPU Command Average: 997 - 28553 0.00 0.00 0.00 0.00 - |__pmd8 Average: 997 - 28555 100.00 0.00 0.00 100.00 - |__pmd9 Average: 997 - 28556 100.00 1.00 0.00 100.00 - |__pmd10 Average: 997 - 28557 0.00 0.00 0.00 0.00 - |__pmd12 Average: 997 - 28558 100.00 0.00 0.00 100.00 - |__pmd11 Average: 997 - 28559 100.00 0.00 0.00 100.00 - |__pmd14 Average: 997 - 28560 100.00 1.00 0.00 100.00 - |__pmd13 Average: 997 - 28561 100.00 0.00 0.00 100.00 - |__pmd15 

This is the command string I am sending to the remote server:

ssh -vvvv [email protected] ''pidstat -t -p '`pidof ovs-vswitchd` 1 1 | grep -E pmd\|%CPU' 

and this is what I am seeing being sent:

pidstat -t -p `pidof ovs-vswitchd` 1 1 | grep -E pmd\\|%CPU 

Everything is correct except for the double back slashes, it should only be a single slash.

2
  • 1
    Did you try not using the back-slash and instead surrounding the regexp within double-quotes ? (also leave the single-quotes as they are) Commented Apr 16, 2019 at 17:00
  • Thanks Rui. I have tried double quotes to no avail and unfortunately removing the back slash does not produce expected result. Thanks again! Commented Apr 17, 2019 at 14:20

1 Answer 1

0

It seems that SSH is simply lying on how many backslashes it actually sends to the server. Let us look at some trivial examples:

ssh -v juha@goliath 'echo h\ello' ... debug1: Sending command: echo h\\ello hello ... 


ssh -v juha@goliath 'echo h\\ello' ... debug1: Sending command: echo h\\\\ello h\ello ... 


ssh -v juha@goliath 'echo h\\\ello' ... debug1: Sending command: echo h\\\\\\ello h\ello ... 


ssh -v juha@goliath 'echo h\\\\ello' ... debug1: Sending command: echo h\\\\\\\\ello h\\ello ... 

It seems, that SSH passes the quoted command correctly to the remote shell, since the latter outputs exactly what we would have gotten if the commands would have been executed locally.

On the other hand, SSH seems to blindly double the number of backslashes in its debug message. This also holds if one introduces an additional level of quoting like in

ssh -v juha@goliath "echo 'h"'\\'"ello'" ... debug1: Sending command: echo 'h\\\\ello' h\\ello ... 

To make the quoting scheme clear for the last example: The last argument in the SSH command is a concatenation of three strings: "echo 'h", '\\', and "ello'". The local shell turns this into echo 'h\\ello' prior passing it to SSH.

Now for your command:

ssh -vvvv [email protected] ''pidstat -t -p '`pidof ovs-vswitchd` 1 1 | grep -E pmd\|%CPU' 

You have two single quotes in front of pidstat. This causes pidstat -t -p to be unquoted, but this should do no harm. Therefore, and from the above, it is quite likely that the remote shell correctly receives the last argument to grep as pmd\|%CPU turning it into the extended regexp pmd|%CPU which is passed to grep as desired.

So, if this does not work as expected, it has probably another reason than the number of backslashes.

You can verify this by avoiding backslashes at all, for example like so:

ssh -v [email protected] 'pidstat -t -p `pidof ovs-vswitchd` 1 1 | grep -E '"'pmd|%CPU'" 

Here the last argument is a concatenation of two strings: 'pidstat -t -p `pidof ovs-vswitchd` 1 1 | grep -E ' and "'pmd|%CPU'". The local shell turns this into pidstat -t -p `pidof ovs-vswitchd` 1 1 | grep -E 'pmd|%CPU' and passes this to SSH.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.