LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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, 06: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 07:07 AM.
 
Old 04-27-2013, 08: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: 168

Rep: Reputation: 23
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 09:07 AM.
 
2 members found this post helpful.
Old 04-27-2013, 08: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 09:11 AM.
 
Old 04-27-2013, 08:49 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,361
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
...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, 09: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: 168

Rep: Reputation: 23
Btw, you would probably want to add 'exit' at the last line of the .sh file
 
Old 04-27-2013, 10: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, 12:22 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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 12:23 PM.
 
Old 04-29-2013, 12:52 PM   #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: 168

Rep: Reputation: 23
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 02:05 PM
Old Bash scripts not working KeenAs Programming 2 05-29-2009 08:55 AM
suse 11.1 nautilus scripts not working NewDisciple SUSE / openSUSE 6 02-15-2009 02:10 PM
Startup scripts not working vikramtheking Solaris / OpenSolaris 5 06-14-2008 06:04 AM
Bash scripts not working anticuchos Linux - Newbie 5 09-25-2005 10:40 PM

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

All times are GMT -5. The time now is 02:09 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration