LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-15-2018, 04:54 AM   #1
nextStep
Member
 
Registered: Aug 2018
Posts: 32

Rep: Reputation: Disabled
Issue with grep command


Hi All,

I am new to shell script. My requirement is to list user information from remote log files. For that purpose wrote shell script as below(For security reason masked real value)

Code

ssh -i /home/user/.ssh/id_rsa user@hostname zgrep -i -B8 "Decision from API: DECLINED" /path1/pah2/path3/logfile.gz|zgrep "API detection is enabled"


The issue is the above command, if executed from remote terminal will provide the result as expected, but if we run as shell script from the host server the output varies.

This i think grep stop searches after finding the first pattern("Decision from API") and returns the record which is having logs other than "Declined" also.

Could you please advise how to fix.
 
Old 08-15-2018, 05:30 AM   #2
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,158

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Wink

Quote:
Originally Posted by nextStep View Post
Hi All,

I am new to shell script. My requirement is to list user information from remote log files. For that purpose wrote shell script as below(For security reason masked real value)

Code

ssh -i /home/user/.ssh/id_rsa user@hostname zgrep -i -B8 "Decision from API: DECLINED" /path1/pah2/path3/logfile.gz|zgrep "API detection is enabled"


The issue is the above command, if executed from remote terminal will provide the result as expected, but if we run as shell script from the host server the output varies.

This i think grep stop searches after finding the first pattern("Decision from API") and returns the record which is having logs other than "Declined" also.

Could you please advise how to fix.
Remote and logon user has the same account privileges?

Are they logon also to the same profile with remote user?
 
Old 08-15-2018, 05:37 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Welcome to the forum. It helps to put [code] [/code] tags around actual code to make it more readable. The second zgrep should be a regular grep since it is dealing with regular text output from the first one:

Code:
ssh -i /home/user/.ssh/id_rsa user@hostname 'zgrep -i -h -B8 "Decision from API: DECLINED" /path1/pah2/path3/logfile.gz' \
        | grep "API detection is enabled"
The zgrep runs on the remote computer. The grep runs on the local one.

The -h option might also help, since you won't need the file name included. See "man grep"
 
1 members found this post helpful.
Old 08-15-2018, 05:45 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,805

Rep: Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206
The ssh command is evaluated twice, once on the calling host and once on the remote host.
The calling host removes the "quotes", and they are missing on the remote site.
Some work-arounds:
Code:
ssh -i /home/user/.ssh/id_rsa user@hostname zgrep -i -B8 \"Decision from API: DECLINED\" /path1/pah2/path3/logfile.gz | zgrep "API detection is enabled"
The calling host replaces the escaped " by a ". The | zgrep ... runs on the calling host.
Code:
ssh -i /home/user/.ssh/id_rsa user@hostname 'zgrep -i -B8 "Decision from API: DECLINED" /path1/pah2/path3/logfile.gz' | zgrep "API detection is enabled"
The calling host sees the 'string in ticks' and removes the ticks. The | zgrep ... runs on the calling host.
Code:
ssh -i /home/user/.ssh/id_rsa user@hostname 'zgrep -i -B8 "Decision from API: DECLINED" /path1/pah2/path3/logfile.gz | zgrep "API detection is enabled"'
The calling host sees the 'string in ticks' and removes the ticks. The | zgrep ... runs on the remote host.
 
1 members found this post helpful.
Old 08-15-2018, 05:58 AM   #5
nextStep
Member
 
Registered: Aug 2018
Posts: 32

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
Welcome to the forum. It helps to put [code] [/code] tags around actual code to make it more readable. The second zgrep should be a regular grep since it is dealing with regular text output from the first one:

Code:
ssh -i /home/user/.ssh/id_rsa user@hostname 'zgrep -i -h -B8 "Decision from API: DECLINED" /path1/pah2/path3/logfile.gz' \
        | grep "API detection is enabled"
The zgrep runs on the remote computer. The grep runs on the local one.

The -h option might also help, since you won't need the file name included. See "man grep"

Thanks a lot, the above approach worked.
 
Old 08-15-2018, 06:02 AM   #6
nextStep
Member
 
Registered: Aug 2018
Posts: 32

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by MadeInGermany View Post
The ssh command is evaluated twice, once on the calling host and once on the remote host.
The calling host removes the "quotes", and they are missing on the remote site.
Some work-arounds:
Code:
ssh -i /home/user/.ssh/id_rsa user@hostname zgrep -i -B8 \"Decision from API: DECLINED\" /path1/pah2/path3/logfile.gz | zgrep "API detection is enabled"
The calling host replaces the escaped " by a ". The | zgrep ... runs on the calling host.
Code:
ssh -i /home/user/.ssh/id_rsa user@hostname 'zgrep -i -B8 "Decision from API: DECLINED" /path1/pah2/path3/logfile.gz' | zgrep "API detection is enabled"
The calling host sees the 'string in ticks' and removes the ticks. The | zgrep ... runs on the calling host.
Code:
ssh -i /home/user/.ssh/id_rsa user@hostname 'zgrep -i -B8 "Decision from API: DECLINED" /path1/pah2/path3/logfile.gz | zgrep "API detection is enabled"'
The calling host sees the 'string in ticks' and removes the ticks. The | zgrep ... runs on the remote host.

Thanks for the useful tips.This also worked.
 
Old 08-15-2018, 06:07 AM   #7
nextStep
Member
 
Registered: Aug 2018
Posts: 32

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by nextStep View Post
Thanks a lot, the above approach worked.
One query :
Why the single quote put before the zgrep and why \ given before the pipe symbol.

'zgrep -i -h -B8 "Decision from API: DECLINED" /path1/path2/path3/logfile.gz' \
 
Old 08-15-2018, 06:10 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
If the last character in a line is a backslash \ then it tells the shell interpreter that things are continued on the next line. Sometimes it is easier to read when separate instructions are on separate lines even if they are piped from one to the next.

The single quote just groups things for sending to the remote server. Everything else after that is done locally.

Last edited by Turbocapitalist; 08-15-2018 at 06:14 AM. Reason: spelling
 
Old 08-15-2018, 06:57 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,805

Rep: Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206
Usually I avoid a \ at line ends, because copy/paste sometimes adds a space character at the very end.
Instead I break a long line into logical blocks, like this
Code:
ssh -i /home/user/.ssh/id_rsa user@hostname '
  zgrep -i -B8 "Decision from API: DECLINED" /path1/pah2/path3/logfile.gz | grep "API detection is enabled"
'
The remote shell
  • sees the two empty lines and ignores them.
  • sees the indented command, and this has no impact.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] run ps|grep command by script/command line ... ERROR: Unsupported option (BSD syntax) masuch Programming 4 05-23-2012 04:13 AM
command grep issue: how to get occurrences of an pattern look like "cool.a_string" coolloo_djack Linux - General 4 03-13-2010 09:27 AM
How to pass the result of a command to another command (like grep) desb01 Programming 4 06-25-2009 12:09 PM
Help me in Grep Command + cd command in single line JeiPrakash Linux - Newbie 3 05-27-2008 04:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration