6

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.

6

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
3

In order to execute a grep command on the remote machine which holds the $ sign, you need:

  • Escape the $ using \
  • Escape the new \ using another \

i.e. adding \\ before the $ sign.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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