LinuxQuestions.org
Review your favorite Linux distribution.
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 04-27-2013, 05:41 AM   #1
mahan77
LQ Newbie
 
Registered: Apr 2013
Location: London
Distribution: centos
Posts: 14

Rep: Reputation: Disabled
need working scripts


Hello every one
How do I rewrite this file (fail2ban.sh) so I can get it work?
I need this to send a mail from log file


#!/bin/bash
grep - $(date +"%Y-%m-%d" --date="1 days ago") /var/log/fail2ban.log << EOF | /usr/sbin/sendmail -t
to:mail@somedomian.com
from:webServer
subject:Fail2Ban-LOG

EOF

Last edited by mahan77; 04-27-2013 at 06:07 AM.
 
Old 04-27-2013, 07:23 AM   #2
Sigg3.net
Member
 
Registered: Mar 2008
Location: Oslo, Norway
Distribution: Slackware 14.1 64-bit, Ubuntu 15.10, Fedora 17, Ubuntu 12 LTS and Ubuntu server 10.04
Posts: 173

Rep: Reputation: 28
In my experience, the CAT << redirection does not work in bash scripts (as it does in "interactive" mode). Here's an alternative:

Code:
#!/bin/bash
# create tmp file mail
mail=`mktemp mail.XXXXXX`

# add meta info
echo "to:mail@somedomain.com" >> $mail
echo "from:webServer" >> $mail
echo "subject:Fail2Ban-LOG" >> $mail

# msg body
grep - $(date +"%Y-%m-%d" --date="1 days ago") /var/log/fail2ban.log >> $mail

# send it!
cat $mail | /usr/sbin/sendmail -i -t

rm -f $mail
exit
The correct way to replace the "CAT trick" is probably to do a while-loop, but I'm not wearing my brain today:P

Last edited by Sigg3.net; 04-27-2013 at 08:07 AM.
 
2 members found this post helpful.
Old 04-27-2013, 07:38 AM   #3
mahan77
LQ Newbie
 
Registered: Apr 2013
Location: London
Distribution: centos
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Sigg3.net View Post
In my experience, the CAT << redirection does not work in bash scripts (as it does in "interactive" mode). Here's an alternative:

Code:
#!/bin/bash
# create tmp file mail
mail=`mktemp mail.XXXXXX`

# add meta info
echo "to:mail@somedomain.com" >> $mail
echo "from:webServer" >> $mail
echo "subject:Fail2Ban-LOG" >> $mail

# msg body
grep - $(date +"%Y-%m-%d" --date="1 days ago") /var/log/fail2ban.log >> $mail


# send it!
cat $mail | /usr/sbin/sendmail -i -t

rm -f $mail
The correct way to replace the "CAT trick" is probably to do a while-loop, but I'm not wearing my brain today:P
I will try this and update about it
Thanks


The scripts do what I want to do.
Thank you again

Last edited by mahan77; 04-27-2013 at 08:11 AM.
 
Old 04-27-2013, 07:49 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
...or provided mailx is installed:
Code:
#!/bin/sh --
grep - $(date +"%Y-%m-%d" --date="1 days ago") /var/log/fail2ban.log | mail -s "Fail2Ban-LOG" recipient@some.dom.ain
exit 0
...or provided Logwatch is installed:
Code:
#!/bin/sh --
logwatch --detail High --range Yesterday --service fail2ban --mailto recipient@some.dom.ain
exit 0
*BTW fail2ban emails violations by default. Why email more?..
 
Old 04-27-2013, 08:07 AM   #5
Sigg3.net
Member
 
Registered: Mar 2008
Location: Oslo, Norway
Distribution: Slackware 14.1 64-bit, Ubuntu 15.10, Fedora 17, Ubuntu 12 LTS and Ubuntu server 10.04
Posts: 173

Rep: Reputation: 28
Btw, you would probably want to add 'exit' at the last line of the .sh file
 
Old 04-27-2013, 09:03 AM   #6
mahan77
LQ Newbie
 
Registered: Apr 2013
Location: London
Distribution: centos
Posts: 14

Original Poster
Rep: Reputation: Disabled
Thanks for the tips.
I’m new to linux, learning as I go along
 
Old 04-29-2013, 11:22 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by Sigg3.net View Post
In my experience, the CAT << redirection does not work in bash scripts (as it does in "interactive" mode).
Huh? That's not a "trick", it's just a standard here document, and will work with any command that can accept text from stdin.

The only problem I see here is that the OP is trying to send the input into grep, which is already reading data from a file and so will ignore it. This just means that the output has to be reconfigured to be sent into mail correctly.

This could be done with a command substitution inside the heredoc, among other techniques. No need for any external files.

Code:
#!/bin/bash

cat << EOF  | /usr/sbin/sendmail -t
to:mail@somedomian.com
from:webServer
subject:Fail2Ban-LOG
$( grep - $(date +"%Y-%m-%d" --date="1 days ago") /var/log/fail2ban.log )
EOF
And in fact, I don't see the need for a heredoc either. A simple command grouping can do the trick just as well.

Code:
#!/bin/bash

{
echo 'to:mail@somedomian.com'
echo 'from:webServer'
echo 'subject:Fail2Ban-LOG'
grep - $(date +"%Y-%m-%d" --date="1 days ago") /var/log/fail2ban.log
} | /usr/sbin/sendmail -t
PS: See my reply in the OP's previous thread about grepping the logfile.

Last edited by David the H.; 04-29-2013 at 11:23 AM.
 
Old 04-29-2013, 11:52 AM   #8
Sigg3.net
Member
 
Registered: Mar 2008
Location: Oslo, Norway
Distribution: Slackware 14.1 64-bit, Ubuntu 15.10, Fedora 17, Ubuntu 12 LTS and Ubuntu server 10.04
Posts: 173

Rep: Reputation: 28
Nice script there, David!

I only recently learned that it's called a "heredoc", and I still think it's a neat "trick" for those of us still learning bash basics
 
  


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
Nautilus Scripts Not Working (Fedora 12) kwanbis Linux - Newbie 30 01-06-2010 01:05 PM
Old Bash scripts not working KeenAs Programming 2 05-29-2009 07:55 AM
suse 11.1 nautilus scripts not working NewDisciple SUSE / openSUSE 6 02-15-2009 01:10 PM
Startup scripts not working vikramtheking Solaris / OpenSolaris 5 06-14-2008 05:04 AM
Bash scripts not working anticuchos Linux - Newbie 5 09-25-2005 09:40 PM

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

All times are GMT -5. The time now is 12:55 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