Skip to content
Snippets Groups Projects
Unverified Commit 610e1626 authored by David Beniamine's avatar David Beniamine
Browse files

a Few usefull scripts

parents
Branches
No related tags found
No related merge requests found
#!/bin/bash
# Copyright (C) 2015 Beniamine, David <David@Beniamine.net>
# Author: Beniamine, David <David@Beniamine.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
sendername="Tetras Libre"
recipient="$sendername <admin@tetras-libre.fr>"
level=info
date="$(date +%y%m%d_%H%M)"
archiveFile="HealthReport-$date.tar.xz"
logdir="/var/log"
# Log to add if we are in alert mode
alertfiles="$logdir/mysql.log $logdir/faillog \
$logdir/daemon.log $logdir/auth.log $logdir/kern.log"
# Log files to always include
files="$logdir/bckp.log $logdir/fail2ban.log"
# No need for an actual random number as we know this sentence will never
# appear in the mail
BOUNDARY="unique-boundary-42"
BOUNDARY2="unique-boundary-24"
usage()
{
echo "Usage $0 [options]"
echo "-h Display this help and quit"
echo "-l level Set the report level {info,warning, alert}, default: info"
echo "-f list include file listeds must be between \"\""
echo "-m message Add a message to the e-mail"
}
# Switch to alert level if needed
set_level()
{
# Check for dead services => level to alert
services=$(/bin/systemctl --failed --all)
dead_srv=$(echo "$services" | grep "mysql\|apache\|ssh\|fail2ban")
if [ ! -z "$dead_srv" ]
then
text="$text\nlevel switch to alert because an important service is down.\nold level: $leveldead\nservices: $dead_srv"
level="alert"
fi
subject="[$level] Health report $date for $(hostname)"
}
# Create archive file
create_archive()
{
if [ "$level" != "info" ]
then
files="$files $alertfiles"
fi
tar cvJf $archiveFile $files
ATT_MIMETYPE=$(file -ib $archiveFile | cut -d";" -f1) # detect mime type
ATT_ENCODED=$(base64 < $archiveFile) # encode attachment
rm $archiveFile
}
list_upgradable_packages()
{
/usr/bin/aptitude update > /dev/null 2>&1
res=$(/usr/bin/aptitude search ~U)
if [ -z "$res" ]
then
res="All packages are up to date"
fi
echo $res | sed 's/ i /\ni /g'
}
# Execute the given command and print it as code
do_cmd()
{
$@ | sed 's/^\(.*\)$/\t\1/g'
}
# Actual content of the mail, must be markdown format
generate_markdown_content()
{
cat <<-EOF
# Health report from $(hostname)
$(echo -e "$text")
## System status
### Uptime
$(do_cmd uptime)
### IP
$(do_cmd $(dirname $0)/getip.sh)
### Is system running ?
$(do_cmd /bin/systemctl is-system-running)
### Upgradable packages
$(do_cmd list_upgradable_packages)
### Services
#### Failed
$(do_cmd echo "$services")
#### Requiring restart
$(do_cmd /usr/sbin/checkrestart)
#### Ufw
$(do_cmd /usr/sbin/ufw status numbered)
## System usage
### Users
$(do_cmd who -a)
### Memory
$(do_cmd free -h)
### Disk
$(do_cmd df -h)
### CPU
#### Freq
$(do_cmd cpufreq-info)
#### Sensors
$(do_cmd sensors)
## Contrab
$(do_cmd crontab -l)
Best regards,
$sendername
EOF
}
generate_mail_content()
{
# Bufferize the markdow version
if [ -z "$markdown_content" ]
then
markdown_content=$(generate_markdown_content | sed 's/$/\\n/g')
fi
# print the requested verions
if [ "$1" == "html" ]
then
echo -e "$markdown_content" | /usr/bin/pandoc --standalone
else
echo -e "$markdown_content"
fi
}
# Generate a multipart mail with the report and attachement, format:
# multipart / mixed
# |- multipart / alternative
# |- text / plain
# |- text / html
# |- attachement
generate_mail()
{
cat <<-EOF
To: $recipient
Subject: $subject
Auto-Submitted: auto-generated
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="$BOUNDARY"
--$BOUNDARY
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="$BOUNDARY2"
Content-Disposition: inline
--$BOUNDARY2
MIME-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
$(generate_mail_content plain)
--$BOUNDARY2
MIME-Version: 1.0
Content-Type: text/html; charset="us-ascii"
Content-Transfer-Encoding: 7bit
$(generate_mail_content html)
--$BOUNDARY2--
--$BOUNDARY
MIME-Version: 1.0
Content-Disposition: attachment; filename="$archiveFile"
Content-Transfer-Encoding: base64
Content-Type: $ATT_MIMETYPE; name="$archiveFile"
$ATT_ENCODED
--$BOUNDARY--
EOF
}
while getopts "hl:f:m:" opt; do
case $opt in
f)
files="$files $OPTARG"
;;
l)
level=$OPTARG
;;
h)
usage
exit 0
;;
m)
text="$OPTARG"
;;
\?)
echo "Invalid option: -$opt"
usage
exit 1
;;
esac
done
set_level
create_archive
generate_mail | /usr/sbin/sendmail -F "$sendername" -t
#!/bin/bash
#
# Copyright (C) 2015 Beniamine, David <David@Beniamine.net>
# Author: Beniamine, David <David@Beniamine.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This script should be used by fail2ban to send abuse report
#
ip=$1
sendername=$2
sender=$3
jailname=report
if [ -z "$3" ]
then
echo "Usage $0 ip sendername sender"
exit 1
fi
get_abuse_addr()
{
regex='[A-Z0-9._%+-]\+@[A-Z0-9.-]\+\.[A-Z]\{2,4\}'
# Getting the mail address the write way
addr=$(whois -b $ip | awk '/abuse-mailbox/ {print $2}')
if [ -z "$addr" ]
then
# no abuse-mailbox filled, guessing
addr=$(whois $ip | grep -i "abuse.*.@" | grep -i -o $regex | head -n 1)
if [ -z "$addr" ]
then
# First method doesn't work get any mail address in whois
addr=$(whois $ip | grep -v '^changed' | grep -i -o $regex | head -n 1)
fi
fi
echo $addr
}
generate_mail()
{
generate_mail_header
echo "$log" | sed 's/[^:]*://'
generate_mail_footer
}
banlog()
{
grep "Ban $ip" /var/log/fail2ban.log*
}
generate_mail_header()
{
cat <<-EOF
To: $recipient
Subject: Abuse report for address $ip
Reply-To: $sendername <$sender>
Bcc: $sendername <$sender>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Auto-Submitted: auto-generated
This is an automated email generated because the address $ip have tried to
connect without success to my servers and have been banned $nb times by
fail2ban over the past few days:
EOF
}
generate_mail_footer()
{
cat <<-EOF
You received this e-mail either because your address appears as the abuse
address for $ip or because no abuse address is filled for $ip and your address
is the first on the whois.
Hoping that you will be able to do something to mitigate these attacks,
Best regards,
$sendername
EOF
}
log=$(banlog)
nb=$(echo "$log" | wc -l)
recipient=$(get_abuse_addr)
if [ -z "$recipient" ]
then
recipient=$sender
fi
previous=$(echo "$log" | grep "\[$jailname\]")
[ ! -z "$previous" ] && echo "$ip already reported" && exit 0
generate_mail | sendmail -F "$sendername" -t
#!/bin/bash
file=""
subject=""
to=root
usage()
{
echo "Usage $0 -f file -s subject [options]"
echo "-f file Use file as the content of the message"
echo "-s subject Set the subject of the message"
echo "-t address Set the To adress, default $to"
echo "-a file Attach file"
echo "-h Display this help and quit"
}
while getopts "ht:f:s:a:" opt; do
case $opt in
t)
to="$OPTARG"
;;
f)
file="$OPTARG"
;;
s)
subject="$OPTARG"
;;
a)
attach="-a $OPTARG"
;;
h)
usage
exit 0
;;
\?)
echo "Invalid option: -$opt"
usage
exit 1
;;
esac
done
if [ -z "$subject" ] || [ -z "$file" ]
then
echo "Subject and/or message empty"
usage
exit 1
fi
/usr/bin/mutt -e "set content_type=text/html" -s "$subject" $attach -- "$to" < $file
unban.sh 0 → 100755
#!/bin/bash
if [ -z "$1" ]
then
echo "Usage $0 ip"
echo "Undo fail2ban for all protocols for ip"
exit 1
fi
ip=$1
nojail=true
# Trying to remove from all jails
for jail in $(fail2ban-client status | grep 'Jail list' | \
sed -e 's/.*list:\s*//' -e 's/,//g')
do
echo "Looking for $ip in jail $jail"
fail2ban-client set $jail unbanip $ip > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "ip removed from jail $jail"
nojail=false
fi
done
if $nojail
then
echo "$ip wasn't in any jail"
fi
# To avoid mail report
sed -i "/Ban $ip/d" /var/log/fail2ban.log
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment