grep log file in a remote host The 2019 Stack Overflow Developer Survey Results Are InWhen is double-quoting necessary?How do I delete a file named “°” in bashHow to script a database migration using SSH?Obtaining remote host name in bash scriptWhy is my ssh connection unauthorized although I have updated my key on the remote machine?scp (v4) copy from remote to multiple local filenamesIssue with resize and stty on remote ssh script?How to use sed to replace a string using the line number on a remote machine using ssh?grep doesn't print matchesmultiple users remote access using ssh key based authenticationre-write a string with text and numbers onto a newline and insert text
Dual Citizen. Exited the US on Italian passport recently
Evaluating number of iteration with a certain map with While
Limit the amount of RAM Mathematica may access?
Is bread bad for ducks?
Inline version of a function returns different value then non-inline version
How to deal with fear of taking dependencies
Is there a name of the flying bionic bird?
Confusion about non-derivable continuous functions
Does duplicating a spell with Wish count as casting that spell?
It's possible to achieve negative score?
Realistic Alternatives to Dust: What Else Could Feed a Plankton Bloom?
I looked up a future colleague on linkedin before I started a job. I told my colleague about it and he seemed surprised. Should I apologize?
How to make payment on the internet without leaving a money trail?
A poker game description that does not feel gimmicky
If a poisoned arrow's piercing damage is reduced to 0, do you still get poisoned?
Could JWST stay at L2 "forever"?
Output the Arecibo Message
Where does the "burst of radiance" from Holy Weapon originate?
Can't find the latex code for the ⍎ (down tack jot) symbol
Why is it "Tumoren" and not "Tumore"?
Why could you hear an Amstrad CPC working?
How can I fix this gap between bookcases I made?
"Riffle" two strings
"What time...?" or "At what time...?" - what is more grammatically correct?
grep log file in a remote host
The 2019 Stack Overflow Developer Survey Results Are InWhen is double-quoting necessary?How do I delete a file named “°” in bashHow to script a database migration using SSH?Obtaining remote host name in bash scriptWhy is my ssh connection unauthorized although I have updated my key on the remote machine?scp (v4) copy from remote to multiple local filenamesIssue with resize and stty on remote ssh script?How to use sed to replace a string using the line number on a remote machine using ssh?grep doesn't print matchesmultiple users remote access using ssh key based authenticationre-write a string with text and numbers onto a newline and insert text
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
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.
linux bash shell-script rhel
New contributor
anmoreira is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
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.
linux bash shell-script rhel
New contributor
anmoreira is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
It would help if you can explain what you mean by "return the echo". What do you expect to see from the script, and what do you actually see? I suspect that you don't need the backslash and semi-colon;at the end of thesshcommand, because that will causegrepto search only for lines containingsuccess;(success followed by a semi-colon). You probably do not need the backslash/semi-colon at all.
– RobertL
Apr 6 at 7:04
@RobertL The escaped;would be sent over to the remote host, but it would be unescaped there, meaning it would not be part of the pattern used bygrep. To be part of the pattern, it would have to be escaped as\;or as;inside the double quotes.
– Kusalananda♦
Apr 6 at 8:54
@RobertL when I run the script it doesn't return the echo success.
– anmoreira
Apr 6 at 20:11
@anmoreira Does it output anything? Does the log file actually contain the text that you are grepping for (log in on the remote host and check)?
– Kusalananda♦
Apr 6 at 21:24
@Kusalananda it doesn't return nothing. No é ho and no error
– anmoreira
Apr 6 at 21:54
add a comment |
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.
linux bash shell-script rhel
New contributor
anmoreira is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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.
linux bash shell-script rhel
linux bash shell-script rhel
New contributor
anmoreira is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
anmoreira is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Apr 6 at 7:04
Rui F Ribeiro
42k1483142
42k1483142
New contributor
anmoreira is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Apr 6 at 6:16
anmoreiraanmoreira
63
63
New contributor
anmoreira is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
anmoreira is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
anmoreira is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
It would help if you can explain what you mean by "return the echo". What do you expect to see from the script, and what do you actually see? I suspect that you don't need the backslash and semi-colon;at the end of thesshcommand, because that will causegrepto search only for lines containingsuccess;(success followed by a semi-colon). You probably do not need the backslash/semi-colon at all.
– RobertL
Apr 6 at 7:04
@RobertL The escaped;would be sent over to the remote host, but it would be unescaped there, meaning it would not be part of the pattern used bygrep. To be part of the pattern, it would have to be escaped as\;or as;inside the double quotes.
– Kusalananda♦
Apr 6 at 8:54
@RobertL when I run the script it doesn't return the echo success.
– anmoreira
Apr 6 at 20:11
@anmoreira Does it output anything? Does the log file actually contain the text that you are grepping for (log in on the remote host and check)?
– Kusalananda♦
Apr 6 at 21:24
@Kusalananda it doesn't return nothing. No é ho and no error
– anmoreira
Apr 6 at 21:54
add a comment |
It would help if you can explain what you mean by "return the echo". What do you expect to see from the script, and what do you actually see? I suspect that you don't need the backslash and semi-colon;at the end of thesshcommand, because that will causegrepto search only for lines containingsuccess;(success followed by a semi-colon). You probably do not need the backslash/semi-colon at all.
– RobertL
Apr 6 at 7:04
@RobertL The escaped;would be sent over to the remote host, but it would be unescaped there, meaning it would not be part of the pattern used bygrep. To be part of the pattern, it would have to be escaped as\;or as;inside the double quotes.
– Kusalananda♦
Apr 6 at 8:54
@RobertL when I run the script it doesn't return the echo success.
– anmoreira
Apr 6 at 20:11
@anmoreira Does it output anything? Does the log file actually contain the text that you are grepping for (log in on the remote host and check)?
– Kusalananda♦
Apr 6 at 21:24
@Kusalananda it doesn't return nothing. No é ho and no error
– anmoreira
Apr 6 at 21:54
It would help if you can explain what you mean by "return the echo". What do you expect to see from the script, and what do you actually see? I suspect that you don't need the backslash and semi-colon
; at the end of the ssh command, because that will cause grep to search only for lines containing success; (success followed by a semi-colon). You probably do not need the backslash/semi-colon at all.– RobertL
Apr 6 at 7:04
It would help if you can explain what you mean by "return the echo". What do you expect to see from the script, and what do you actually see? I suspect that you don't need the backslash and semi-colon
; at the end of the ssh command, because that will cause grep to search only for lines containing success; (success followed by a semi-colon). You probably do not need the backslash/semi-colon at all.– RobertL
Apr 6 at 7:04
@RobertL The escaped
; would be sent over to the remote host, but it would be unescaped there, meaning it would not be part of the pattern used by grep. To be part of the pattern, it would have to be escaped as \; or as ; inside the double quotes.– Kusalananda♦
Apr 6 at 8:54
@RobertL The escaped
; would be sent over to the remote host, but it would be unescaped there, meaning it would not be part of the pattern used by grep. To be part of the pattern, it would have to be escaped as \; or as ; inside the double quotes.– Kusalananda♦
Apr 6 at 8:54
@RobertL when I run the script it doesn't return the echo success.
– anmoreira
Apr 6 at 20:11
@RobertL when I run the script it doesn't return the echo success.
– anmoreira
Apr 6 at 20:11
@anmoreira Does it output anything? Does the log file actually contain the text that you are grepping for (log in on the remote host and check)?
– Kusalananda♦
Apr 6 at 21:24
@anmoreira Does it output anything? Does the log file actually contain the text that you are grepping for (log in on the remote host and check)?
– Kusalananda♦
Apr 6 at 21:24
@Kusalananda it doesn't return nothing. No é ho and no error
– anmoreira
Apr 6 at 21:54
@Kusalananda it doesn't return nothing. No é ho and no error
– anmoreira
Apr 6 at 21:54
add a comment |
1 Answer
1
active
oldest
votes
The issue 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.
Thanks for the answer Kusalananda, I tried your answer and as mine, i do not receive the echo on the host where I run the script. I do not receive any error also.
– anmoreira
Apr 6 at 22:01
@anmoreira In that case, I presume that the text is not in the file, or the patterns that you use are matching the wrong things.
– Kusalananda♦
Apr 6 at 22:05
found the problem, the hostname was slightly incorrect since the host is part of cluster
– anmoreira
Apr 6 at 22:09
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
anmoreira is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f510853%2fgrep-log-file-in-a-remote-host%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The issue 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.
Thanks for the answer Kusalananda, I tried your answer and as mine, i do not receive the echo on the host where I run the script. I do not receive any error also.
– anmoreira
Apr 6 at 22:01
@anmoreira In that case, I presume that the text is not in the file, or the patterns that you use are matching the wrong things.
– Kusalananda♦
Apr 6 at 22:05
found the problem, the hostname was slightly incorrect since the host is part of cluster
– anmoreira
Apr 6 at 22:09
add a comment |
The issue 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.
Thanks for the answer Kusalananda, I tried your answer and as mine, i do not receive the echo on the host where I run the script. I do not receive any error also.
– anmoreira
Apr 6 at 22:01
@anmoreira In that case, I presume that the text is not in the file, or the patterns that you use are matching the wrong things.
– Kusalananda♦
Apr 6 at 22:05
found the problem, the hostname was slightly incorrect since the host is part of cluster
– anmoreira
Apr 6 at 22:09
add a comment |
The issue 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.
The issue 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.
edited Apr 6 at 22:10
answered Apr 6 at 8:27
Kusalananda♦Kusalananda
140k17261436
140k17261436
Thanks for the answer Kusalananda, I tried your answer and as mine, i do not receive the echo on the host where I run the script. I do not receive any error also.
– anmoreira
Apr 6 at 22:01
@anmoreira In that case, I presume that the text is not in the file, or the patterns that you use are matching the wrong things.
– Kusalananda♦
Apr 6 at 22:05
found the problem, the hostname was slightly incorrect since the host is part of cluster
– anmoreira
Apr 6 at 22:09
add a comment |
Thanks for the answer Kusalananda, I tried your answer and as mine, i do not receive the echo on the host where I run the script. I do not receive any error also.
– anmoreira
Apr 6 at 22:01
@anmoreira In that case, I presume that the text is not in the file, or the patterns that you use are matching the wrong things.
– Kusalananda♦
Apr 6 at 22:05
found the problem, the hostname was slightly incorrect since the host is part of cluster
– anmoreira
Apr 6 at 22:09
Thanks for the answer Kusalananda, I tried your answer and as mine, i do not receive the echo on the host where I run the script. I do not receive any error also.
– anmoreira
Apr 6 at 22:01
Thanks for the answer Kusalananda, I tried your answer and as mine, i do not receive the echo on the host where I run the script. I do not receive any error also.
– anmoreira
Apr 6 at 22:01
@anmoreira In that case, I presume that the text is not in the file, or the patterns that you use are matching the wrong things.
– Kusalananda♦
Apr 6 at 22:05
@anmoreira In that case, I presume that the text is not in the file, or the patterns that you use are matching the wrong things.
– Kusalananda♦
Apr 6 at 22:05
found the problem, the hostname was slightly incorrect since the host is part of cluster
– anmoreira
Apr 6 at 22:09
found the problem, the hostname was slightly incorrect since the host is part of cluster
– anmoreira
Apr 6 at 22:09
add a comment |
anmoreira is a new contributor. Be nice, and check out our Code of Conduct.
anmoreira is a new contributor. Be nice, and check out our Code of Conduct.
anmoreira is a new contributor. Be nice, and check out our Code of Conduct.
anmoreira is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f510853%2fgrep-log-file-in-a-remote-host%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It would help if you can explain what you mean by "return the echo". What do you expect to see from the script, and what do you actually see? I suspect that you don't need the backslash and semi-colon
;at the end of thesshcommand, because that will causegrepto search only for lines containingsuccess;(success followed by a semi-colon). You probably do not need the backslash/semi-colon at all.– RobertL
Apr 6 at 7:04
@RobertL The escaped
;would be sent over to the remote host, but it would be unescaped there, meaning it would not be part of the pattern used bygrep. To be part of the pattern, it would have to be escaped as\;or as;inside the double quotes.– Kusalananda♦
Apr 6 at 8:54
@RobertL when I run the script it doesn't return the echo success.
– anmoreira
Apr 6 at 20:11
@anmoreira Does it output anything? Does the log file actually contain the text that you are grepping for (log in on the remote host and check)?
– Kusalananda♦
Apr 6 at 21:24
@Kusalananda it doesn't return nothing. No é ho and no error
– anmoreira
Apr 6 at 21:54