Category Archives: Bash

List openvpn valid certificates (using index.txt)

list_valid_certificates.sh

#!/bin/bash

# Run this script inside openvpn certificates directory.

rm -fv listado_certificados_validos.txt

CNT=`grep ^V index.txt | wc -l`

for i in 6 7
do
 grep ^V index.txt | cut -d / -f $i | sed 's+/emailAddress++g' | grep ^"CN" >> listado_certificados_validos.txt
done

CNT2=`cat listado_certificados_validos.txt | wc -l`

echo -e "-- VALIDANDO DATOS -- \n"

echo -e $CNT $CNT2" \n"

if [ $CNT2 -eq $CNT ]
then
 cat listado_certificados_validos.txt | sort | nl && echo -e "\nOK"
fi

Sync or copy playlist (.m3u) files

sync_playlist.sh

#!/bin/bash

PLAYLISTS=( rock_tracks.m3u jazz_songs.m3u shazam_01.m3u best_albums.m3u )
ROOTDIR=/home/user/Music/Playlists
DSTDIR=/home/user/Board
CMD="/usr/bin/rsync --progress -u"
TMP=`pwd`/.temp

mkdir $TMP

for i in "${PLAYLISTS[@]}"
do
 grep -v "^#" $ROOTDIR/$i | while read tracks; do du -k "${tracks}" >> $TMP/${i}.sum; done
done

cd $TMP
for i in *.sum
do
 FILE=`echo ${i} | sed s/.sum//g`
 SUM=`cat ${i} | awk '{total=total+$1} END {print total}' | awk '{ human = $1 / 1024} END {print human" M"}'`
 echo -e "$FILE == >> $SUM"
done
cd ..

echo -e "Do you want to synchronize the playlists? (y/n)"
read answer

if [ $answer = y ]
then
 for i in ${PLAYLISTS[@]}
 do
 DIRNAME=`echo ${i} | sed s/.m3u//g`
 mkdir -p $DSTDIR/"${DIRNAME}"
 grep -v "^#" $ROOTDIR/$i | while read tracks; do $CMD "${tracks}" $DSTDIR/"${DIRNAME}"; done
 ls -tr $DSTDIR/"${DIRNAME}" > $TMP/${i}
 mv $TMP/${i} $DSTDIR/"${DIRNAME}"
 done
fi

if [ $answer = n ]
then
 echo -e "OK"
fi

rm -r $TMP

Ping Multiple Host

#!/bin/bash

HOSTS=(192.168.3.241 192.168.3.240 192.168.3.21 192.168.3.216 192.168.3.100 192.168.3.213 192.168.2.239 192.168.2.229 192.168.2.230 192.168.2.218 192.168.2.241 192.168.2.206 192.168.3.219)

count=${#HOSTS[@]}
index=0
while [ "$index" -lt "$count" ]; do
 echo -e "-----------------------------------\n"
 echo -e "Haciendo ping ${HOSTS[$index]} ... \n"
 ping -c 3 ${HOSTS[$index]}
 let "index++"
done

Send email Perl with attached files and remove file after sent

send_email_file_report.pl

#!/usr/bin/perl

use MIME::Lite;

my $attached_file = $ARGV[0];
my $report_directory = "/mnt/reports";

my $msg = MIME::Lite->new(
 From => 'xxxx@xxxx.com',
 To => 'xxx@xxx.com',
 Cc => 'xxxx@xxxx.com, xxxxx@xxxx.com',
 Bcc => 'xxx@xxx.com, xxx@xxx.com',
 Subject => "$attached_file",
 Type => 'multipart/mixed',
);

$msg->attach(
 Type => 'TEXT',
 Data => "Mensaje : $attached_file \nNota: Este correo ha sido generado autom√°ticamente por favor no responda a este mensaje.",
);

$msg->attach(
 Type => 'text/plain',
 Path => "$report_directory/$attached_file",
 Filename => "$attached_file",
 Disposition => 'attachment'
);

$msg->send or die "Mensaje no enviado";

system("rm -rf $report_directory/$attached_file");


------------ bash execution ------------------------

#!/bin/bash

report_directory="/mnt/reports"
exec_directory="/root/bin"

cd $report_directory
for i in *.csv; do $exec_directory/send_email_file_report.pl $i; done

Backup Linux System

#!/bin/bash

# Global Variables
INFO_FILE="info_`hostname`_`date +%Y-%m-%d`"
BACKUP_DIRECTORY="/mnt/backup/Linux"
DIRECTORY_NAME="`hostname`"

# Create backup directory
mkdir -pv $BACKUP_DIRECTORY/$DIRECTORY_NAME

# Output commands backup
echo -e "`hostname` > cat /etc/issue" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
cat /etc/issue >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > uname -a" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
uname -a >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > free -m" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
free -m >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > fdisk -l" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
fdisk -l >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > df -h" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
df -h >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > cat /etc/fstab" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
cat /etc/fstab >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > ifconfig" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
ifconfig >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > netstat -nr" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
netstat -nr >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > cat /etc/rc.local" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
cat /etc/rc.local >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > netstat -tunpl" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
netstat -tunpl >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > pstree" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
pstree >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > crontab -l" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
crontab -l >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > for i in cat /etc/passwd|awk -F: '($3>999 && $3 < 10000 ) {print $1}'; do crontab -l -u $i;done" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
for i in `cat /etc/passwd|awk -F: '($3>999 && $3 < 10000 ) {print $1}'`; do crontab -l -u $i; done >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > iptables -nvL" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
iptables -nvL >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > cat /proc/cpuinfo" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
cat /proc/cpuinfo >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > dmidecode" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
dmidecode >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > lspci" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
lspci >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > lsusb" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
lsusb >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > ls /etc/rc*.d" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
ls /etc/rc*.d >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE

# Backup all directories excluding /proc /sys /dev
tar cf $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE.tar / --exclude=/proc --exclude=/sys --exclude=/dev --exclude=/mnt