grep log file in a remote host

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

I'm trying to grep a string in the log file in a remote host. but I'm having trouble to return the echo to the host where I'm running the script.

#!/bin/bash
#dt=$(date +"%Y%m%d")
#HHMM=$(date '+%H:%M')
key="keys/cash_prod_key"



if
        ssh -o StrictHostKeyChecking=no -qni $key user@host "cat /var/log/FILE_send.log | grep FILENAME | grep -i success"\;
then
        echo "Success"
fi

I don't think this is the most correct way to do it.

profilepic.png
manpreet 2 years ago

ssue was an incorrect host name.

This answer is instead focusing on how to write the code in a better way.

You don't need to run the complete pipeline on the remote host. Running complicated commands on the command line with ssh is rather error prone, not least from a quoting point of view.

Instead:

#!/bin/bash

key="keys/cash_prod_key"

ssh_args=( -o StrictHostKeyChecking=no -qni "$key" user@host )

if ssh "${ssh_args[@]}" cat /var/log/FILE_send.log |
   grep -F 'FILENAME' |
   grep -q -i -F 'success'
then
    echo 'Success'
fi

This would run only the cat on the remote host and then run the two grep commands locally.

I've also added the -F flag to the grep commands, assuming that the strings used as patterns are not regular expressions but literal strings that you'd like to search for. The last grep uses -q as we're not actually interested in seeing the output, just in whether it contains the string success or not.

To make it easier to read, I've put the arguments used with ssh into an array that I later use in the call, and I've also made the lines a bit shorter by simply inserting newlines after the pipe symbols. The script is still syntactically correct. Also note that any expansion of variable etc. should always be double quoted, unless you know in what contexts this is not needed.

Note that I'm assuming that the ssh command is invoked in the correct way.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community