0

I'm writing a Python script to parse various logs across different hosts. I can successfully run bash scripts via Python's subprocess module, however, I want to minimize the number of ssh calls, so I'm trying to combine ssh, grep, and find into 1 command.

The problem begins when some hosts may not have any log files. My command is something like:

ssh SOME_HOST 'grep "SOME_VALUE" $(find SOME_PATH -type f -name "*.log")'

When there are no log files, this becomes grep "SOME_VALUE" and hangs forever. I tried wrapping it in double quotes so that it becomes grep "SOME_VALUE" "" instead:

ssh SOME_HOST 'grep "SOME_VALUE" "$(find SOME_PATH -type f -name "*.log")"'

Now I have the exact opposite problem: when there are some logs found, I get:

grep: FILE_A^JFILE_B^J... : No such file or directory

I've been stuck on this for a few days and googling / RTFMing hasn't helped much. I have tried tr to replace newlines with spaces but got some other weird errors :( Is there an easy way to do this in one command?

1
  • 1
    You'd have the same problem if you were doing this locally, without ssh.
    – Barmar
    Commented Jul 5 at 17:24

1 Answer 1

1

Add an extra filename argument so the argument list is never empty. You can use /dev/null as a dummy file that never matches grep.

ssh SOME_HOST 'grep "SOME_VALUE" $(find SOME_PATH -type f -name "*.log") /dev/null'

4
  • unfortunately exec and xargs are disabled on the machines due to security policies
    – OM222O
    Commented Jul 5 at 17:28
  • Add an extra /dev/null filename argument.
    – Barmar
    Commented Jul 5 at 17:32
  • I wasn't aware of this trick, it works! thanks.
    – OM222O
    Commented Jul 5 at 17:43
  • 2
    It's also an old trick that we used to use before the -H option was added to grep, so we'd get filenames even if only one file matches the wildcard.
    – Barmar
    Commented Jul 5 at 17:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.