5 ms·
> localhost:~$ ssh remoteserver "cat /var/log/nginx/access.log" | grep badstuff.php It would be much more traffic (and execution time) friendly, if you put pip
by avaika 8y ago
> localhost:~$ ssh remoteserver "cat /var/log/nginx/access.log" | grep badstuff.php
It would be much more traffic (and execution time) friendly, if you put pipe and grep to "" as well, so grep would be executed on remote server and ssh won't have to send the entire log to your machine.
And I'd like to highlight the overwhelming awesomeness of ~/.ssh/config file. If you haven't heard about it or use shell alias instead please please read about it. It supports autocomplete, dynamic paths to key files and many other cool things which can make your ssh client experience simply great.
- j1elo 8y agoThe article does touch on that optimization: In this example the grep is being performed on the local system after the log file has been pushed across the ssh session. If the file is large it would be more efficient to run the grep on the remote side simply by enclosing the pipe and grep in the double quotes.
- h1d 8y agoI see no benefit of running grep locally in any case unless maybe local grep is aliased in a complex way to colorize and decorate the outputs in some other way. Either way this is not a great example as you should just login and run it on the remote machine as running through pipe causes the connection to be established every time unless you keep a persistent connection open behind which is explained in the MultiPlex section which is very handy when you need to run many commands over ssh in cases of like taking backups from that machine.
- isostatic 8y agoThe obvious case would be when the remote grep doesn't exist at all, or is a cutdown non-gnu version of grep which didn't support things like extended or perl regexps
- h1d 8y agoWhat chance is there that grep doesn't exist on a machine that has ssh and cat? Having some undesired grep version could be one reason but not enough to justify piping everything before grep in the example.
- yjftsjthsd-h 8y agoThere are systems with SSH that are not Unix. For instance, Cisco switches support SSH but don't have a grep. (They do have an equivalent, but it's unfamiliar and likely less feature-rich)
- rzzzt 8y agoWhile I myself am not a purist, some people would consider this an instance of UUOC: http://porkmail.org/era/unix/award.html http://porkmail.org/era/unix/award.html > localhost:~$ ssh remoteserver "grep badstuff.php /var/log/nginx/access.log"
- Matt3o12_ 8y agoThe only reason I (and probably many other people) prefer cat | grep instead of just using grep is because it is much easier to change the search term this way. With cat, it’s only a ctrl-W away from using a different term. If I only used grep, i would have to move my cursor which can get pretty annoying with a long file path. That being said, only using grep is my preferred approach (if I don’t forget it) in script and “compact lines” (I.e ssh, watch, and everything that expects the command as a string). If you are grepping a file multiple times, I would recommend to connect to the server anyways (because it is much faster). That being said, if this is a single occurrence, and saves you from the pitfalls of using the local grep instead of the remote one, your approach is preferable in that case.
- acqq 8y ago> .ssh/config Do you mean this or something not described there? https://nerderati.com/2011/03/17/simplify-your-life-with-an-ssh-config-file/ https://nerderati.com/2011/03/17/simplify-your-life-with-an-...
- avaika 8y agoYes, exactly this.