Ubuntu – Grep string with special character on remote server using ssh

bashcommand linegrepssh

I have a log file on a remote linux server with the following info in it:

Mar 29 18:15:06 mailserver amavis[12049]: (12049-13) Passed CLEAN {RelayedInbound}, [111.111.111.111]:25667 [111.111.111.111] <automated_email@dell.com> -> <johndoe@domain.com>,<orders@domain.com>, Queue-ID: 7711E18023F, Message-ID: <0e1430$gvauj1@ausxipmktpc11.us.dell.com>, mail_id: GQj-5bhH37Yi, Hits: -, size: 15551, queued_as: EE75C180429, 148 ms

I am trying to run a command on the remote server to grep the message id: 0e1430$gvauj1@ausxipmktpc11.us.dell.com The command I am running is:

ssh -t root@remoteserver grep 0e1430$gvauj1@ausxipmktpc11.us.dell.com /root/log

The problem is with the $ in the message id. If I grep it on the remote server I am able to find the id in the logs by surrounding the message id with '0e1430$gvauj1@ausxipmktpc11.us.dell.com' but when I try running the command through ssh it doesn't seem to work.

Best Answer

Quoting and escaping over SSH is a PITA, so send the pattern to grep over a pipe:

$ echo '0e1430$gvauj1@ausxipmktpc11.us.dell.com' | ssh localhost grep -Ff - foo
Mar 29 18:15:06 mailserver amavis[12049]: (12049-13) Passed CLEAN {RelayedInbound}, [111.111.111.111]:25667 [111.111.111.111] <automated_email@dell.com> -> <johndoe@domain.com>,<orders@domain.com>, Queue-ID: 7711E18023F, Message-ID: <0e1430$gvauj1@ausxipmktpc11.us.dell.com>, mail_id: GQj-5bhH37Yi, Hits: -, size: 15551, queued_as: EE75C180429, 148 ms

Use the -F option so that grep doesn't treat it as a regex. The -f - option tells grep to read patterns from stdin.

Or quote and escape if you must:

$ ssh "grep '0e1430\$gvauj1@ausxipmktpc11.us.dell.com' bar"
Mar 29 18:15:06 mailserver amavis[12049]: (12049-13) Passed CLEAN {RelayedInbound}, [111.111.111.111]:25667 [111.111.111.111] <automated_email@dell.com> -> <johndoe@domain.com>,<orders@domain.com>, Queue-ID: 7711E18023F, Message-ID: <0e1430$gvauj1@ausxipmktpc11.us.dell.com>, mail_id: GQj-5bhH37Yi, Hits: -, size: 15551, queued_as: EE75C180429, 148 ms
Related Question