1

I'm stuck in a situation,

I run this command on node2

netstat -tlpna|grep `ps aux|grep tnslsnr|grep -v grep|awk '{print $2}'`|head -1

and it works fine and gives me this output

tcp        0      0 10.6.1.22:1521          0.0.0.0:*               LISTEN      2601/tnslsnr 

But when i run this command from clinet machine to node2 it gives me error.

ssh  root@$(pcs status|grep oracle_LR|awk '{print $4}') 'netstat -tulpna $(ps aux|grep -v grep|grep tnslsnr'|awk '{print $2})'

enter image description here

After Following from @steeldiver

I added double quote around awk and it is working

ssh root@$(pcs status|grep oracle_LR|awk '{print $4}') 'netstat -tulpna|grep $(ps aux|grep tnslsnr|grep -v grep|awk "{print \$2}")'|head -1

gives me this output

tcp 0 0 xxx.xxx.xxx.xxx:1525 0.0.0.0:*
LISTEN 2601/tnslsnr

BUT

when i add this line in my script.

enter image description here

$echo "<pre>`ssh root@$(pcs status|grep oracle_LR|awk '{print $4}') 'netstat -tulpna|grep $(ps aux|grep tnslsnr|grep -v grep|awk "{print \$2}")'|head -1`</pre>"

and when i run it gives me this output

grep: oracle: No such file or directory
grep: 2601: No such file or directory
grep: 0.0: No such file or directory
grep: 0.0: No such file or directory
grep: 227812: No such file or directory
grep: 18536: No such file or directory
grep: ?: No such file or directory
grep: Ssl: No such file or directory
grep: Aug27: No such file or directory
grep: 6:16: No such file or directory
grep: LISTENER: No such file or directory

I have been playing around with this but nothing is working

OmiPenguin
  • 4,308
  • 1
  • It also appears you haven’t put enough single quotes in the ssh line. There is one before the netstat at the beginning of the remote command, but the one at the end is for the ask statement. – jsbillings Sep 13 '20 at 13:32
  • can you please check the new edits in question – OmiPenguin Sep 14 '20 at 11:47
  • 2
    You should probably avoid all that nesting and try splitting your command into simpler pieces; as you can see, it is really hard to debug in its current state. Nevertheless, the errors from grep seem to say that it is getting a full line of output from ps, and the best candidate for that is awk "{print \$2}": if the applied quoting is not enough, it easily becomes awk "{print }" (because there isn't a positional parameter n. 2 in the shell running it). Maybe awk "{print \\$2}" is enough to avoid that error. – fra-san Sep 14 '20 at 12:19
  • 1
    @fra-san, Tons of thanks i just added extra \ in awk like you mentioned, but i dont understand why do we have to add two \ when using double quotes , my understanding was to use single \ when using $ sign within double quotes – OmiPenguin Sep 15 '20 at 07:06
  • That is because of the command substitution in backticks. It makes nesting harder because outer substitutions remove one level of backslashes in inner substitutions too. See, for instance, http://mywiki.wooledge.org/BashFAQ/082. – fra-san Sep 15 '20 at 09:04

0 Answers0