Scanning Virus in Linux Using ClamAV


#!/bin/bash

Install EPEL Repository
64 Bit
rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

32 Bit
rpm -Uvh http://mirror.overthewire.com.au/pub/epel/6/i386/epel-release-6-8.noarch.rpm

Install Required ClamAV Packages
yum -y install clamav clamd

Clamd Services
/etc/init.d/clamd on
chkconfig clamd on
/etc/init.d/clamd start

Update ClamAV’s from Source
/usr/bin/freshclam

Scanning Virus on CentOS System by Giving Below Commands
clamscan -r /home
clamscan -r /var

Cronjob Details
00 22 * * * Path-Script/clamav-scan.sh (Every Day Night 11PM)

Path Details
clamscan -r /home
clamscan -r /var
clamscan -r /root

Output Details
clamscan -r /var/www
/var/www/html/index.html: OK

Scan Summary
Known viruses: 3965211
Engine version: 0.98.7
Scanned directories: 2
Scanned files: 1
Infected files: 0
Data scanned: 0.01 MB
Data read: 0.01 MB (ratio 1.00:1)
Time: 15.603 sec (0 m 15 s)

Netgauge Ooklaserver Install Script


#!/bin/sh

Variables & Paths
BASE_DOWNLOAD_PATH=’http://cdn.speedtest.speedtest.net/netgauge/’
INSTALL_DIR=”
DAEMON_FILE=’OoklaServer’
PID_FILE=”$DAEMON_FILE.pid”

display_usage() {
echo “This script can be used to install or control a NetGauge Server.”
echo  “Usage:”
echo  “$0 [-f|–force] [-i|–installdir <dir>] command”
echo  “”
echo  ”  Valid commands: install, start, stop, restart”
echo  ”   install – downloads and installs the NetGauge server”
echo  ”   start   – starts the server if not running”
echo  ”   stop    – stops the server if running”
echo  ”   restart – stops the server if running, and restarts it”
echo  ” ”
echo  ”  -f|–force           Do not prompt before install”
echo  ”  -i|–install <dir>   Install to specified folder instead of the current folder”
echo  ”  -h|–help            This help”
echo  “”
}

detect_platform() {
case $( uname -s ) in
Darwin) server_package=’OSX’;;
Linux) server_package=’Linux’
arch=uname -m
if [ “$arch” = “x86_64” ]; then
server_package=’Linux64′
fi
;;
FreeBSD) server_package=’FreeBSD’;;
SunOS) server_package=’Solaris’
arch=uname -p
if [ “$arch” = “sparc” ]; then
server_package=’Sparc’
fi
;;
*)
Please Select the server Platform
echo “1) OSX”
echo “2) Linux (32bit)”
echo “3) Linux (64bit)”
echo “4) Solaris”
echo “5) Sparc”
echo “6) FreeBSD”

read n
case $n in
1) server_package=’OSX’;;
2) server_package=’Linux’;;
3) server_package=’Linux64′;;
4) server_package=’Solaris’;;
5) server_package=’Sparc’;;
6) server_package=’FreeBSD’;;
esac
esac

Server Platform is $server_package
}
confirm_install() {
if [ “$INSTALL_DIR” != “” ]; then
printf “This will install the NetGauge server for $server_package to folder $INSTALL_DIR. Please confirm (y/n) > ”
else
printf “This will install the NetGauge server for $server_package to the current folder. Please confirm (y/n) > ”
fi
read response
if [ “$response” != “y” ]; then
echo “Exiting program.”
exit 1
fi
}

goto_speedtest_folder() {
dir_full=pwd
dir_base=basename $dir_full

echo “Checking Directory Structure”
if [ “$INSTALL_DIR” != “” ]; then
if [ “$dir_base” != “$INSTALL_DIR” ]; then
if [ ! -d “$INSTALL_DIR” ]; then
mkdir “$INSTALL_DIR”
scriptname=basename $0
cp “$scriptname” “$INSTALL_DIR”
fi

cd “$INSTALL_DIR”
fi
fi
}

download_install() {
gzip_download_url=”$BASE_DOWNLOAD_PATH$server_package.tgz”
gzip_download_file=”$server_package.tgz”

curl_path=command -v curl
wget_path=command -v wget
fetch_path=command -v fetch

echo “Downloading Server Files”
if [ -n “$curl_path” ]; then
curl -O $gzip_download_url

elif [ -n “$wget_path” ]; then
wget “$gzip_download_url” -O “$gzip_download_file”

elif [ -n “$fetch_path” ]; then
fetch -o “$gzip_download_file” “$gzip_download_url”
else
echo “This script requires CURL or WGET or FETCH”
exit 1
fi

UNzip Package
if [ -f “$gzip_download_file” ]; then
echo “Extracting Server Files”
tar -zxovf “$gzip_download_file”
rm “$gzip_download_file”
if [ ! -f “${DAEMON_FILE}.properties” ]; then
cp “${DAEMON_FILE}.properties.default” “${DAEMON_FILE}.properties”
fi
else
echo “Error download server package”
exit 1
fi

}

restart_if_running() {
stop_if_running
start
}

stop_if_running() {
if [ -f “$PID_FILE” ]; then
daemon_pid=cat $PID_FILE
if [ $daemon_pid ]; then
printf “Stopping $DAEMON_FILE Daemon ($daemon_pid)”
kill “$daemon_pid”
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do
sleep 1
if kill -0 “$daemon_pid” > /dev/null 2>&1 ; then
printf ” .”
else
break
fi
done
echo “”
fi
fi
}

start_if_not_running() {
if [ -f “$PID_FILE” ]; then
daemon_pid=cat $PID_FILE
if [ $daemon_pid ]; then
if kill -0 “$daemon_pid” > /dev/null 2>&1 ; then
echo “$DAEMON_FILE ($daemon_pid) is already running”
exit 1
fi
fi
fi
start
}

start() {
printf “Starting $DAEMON_FILE”
dir_full=pwd
if [ -f “$DAEMON_FILE” ]; then
chmod +x “$DAEMON_FILE”
daemon_cmd=”./$DAEMON_FILE –daemon –pidfile=$dir_full/$PID_FILE”
$daemon_cmd
else
echo “”
echo “Daemon not installed. Please run install first.”
exit 1
fi

wait for PID file to be created and verify daemon started

for i in 1 2 3 4 5 6 7 8 9 10; do
sleep 1
if [ -f “$PID_FILE” ]; then break; fi
printf ” .”
done
echo “”
if [ -f “$PID_FILE” ]; then
daemon_pid=cat $PID_FILE
echo “Daemon Started ($daemon_pid)”
else
echo “Failed to Start Daemon”
fi
}

prompt=1
action=help
while [ “$1” != “” ]; do
case $1 in
install )                action=install
;;
stop )                    action=stop
;;
start )                    action=start
;;
restart )                action=restart
;;
help )                    action=help
;;
-i | –installdir )        shift
INSTALL_DIR=$1
;;
-f | –force )            prompt=0
;;
-h | –help )           display_usage
exit
;;
* )                     display_usage
exit 1
esac
shift
done

if [ “$action” = “restart” ]; then
restart_if_running
fi

if [ “$action” = “start” ]; then
start_if_not_running
fi

if [ “$action” = “stop” ]; then
stop_if_running
fi

if [ “$action” = “help” ]; then
display_usage
fi

if [ “$action” = “install” ]; then
detect_platform
if [ “$prompt” = “1” ]; then
confirm_install
fi

goto_speedtest_folder

download_install

restart_if_running
fi

NGINX & RTMP Module Installastion Script


#!/bin/bash

Updating Package’s
sudo apt-get update

Installing Dependencies
sudo apt-get install -y build-essential git libpcre3 \ libpcre3-dev openssl libssl-dev zlibc zlib1g zlib1g-dev

Nginx & Creating Dir Nginx
mkdir Nginx
cd Nginx

Downloading Nginx
wget http://nginx.org/download/nginx-1.7.10.tar.gz
tar xvzf nginx-1.7.10.tar.gz

Nginx RTMP Module
git clone https://github.com/arut/nginx-rtmp-module

Compiling Nginx with RTMP Module
cd Nginx-*
./configure –prefix=/etc/nginx \
make
sudo make install

Install the Nginx init scripts
sudo wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo update-rc.d nginx defaults

Starting & Stopping Nginx Service’s
sudo service nginx start
sudo service nginx stop

Installing FFmpeg
sudo apt-add-repository ppa:jon-severinsson/ffmpeg
sudo apt-get update
sudo apt-get install ffmpeg

Configure RTMP Application
echo “rtmp {
server {
listen 1935;
application live {
live on;
}
}
}” >> /etc/nginx/nginx.conf

Re-Starting Nginx Service
sudo /usr/sbin/nginx -s stop && sudo /usr/sbin/nginx -s start

Installation is Finished
Please Check with rtmp://your-ip-or-domainName:1935/live/{your-stream-name}

INIT Script for Nginx


#! /bin/bash

Variables & Paths
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/nginx
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

Include Nginx Defaults
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi

set -e

. /lib/lsb/init-functions

case “$1” in
start)
echo -n “Starting $DESC: ”
start-stop-daemon –start –quiet –pidfile /usr/local/nginx/logs/$NAME.pid
–exec $DAEMON — $DAEMON_OPTS || true
echo “$NAME.”
;;
stop)
echo -n “Stopping $DESC: ”
start-stop-daemon –stop –quiet –pidfile /usr/local/nginx/logs/$NAME.pid
–exec $DAEMON || true
echo “$NAME.”
;;
restart|force-reload)
echo -n “Restarting $DESC: ”
start-stop-daemon –stop –quiet –pidfile
/usr/local/nginx/logs/$NAME.pid –exec $DAEMON || true
sleep 1
start-stop-daemon –start –quiet –pidfile
/usr/local/nginx/logs/$NAME.pid –exec $DAEMON — $DAEMON_OPTS || true
echo “$NAME.”
;;
reload)
echo -n “Reloading $DESC configuration: ”
start-stop-daemon –stop –signal HUP –quiet –pidfile /usr/local/nginx   /logs/$NAME.pid
–exec $DAEMON || true
echo “$NAME.”
;;
status)
status_of_proc -p /usr/local/nginx/logs/$NAME.pid “$DAEMON” nginx &amp;&amp; exit 0 || exit $?
;;
*)
N=/etc/init.d/$NAME
echo “Usage: $N {start|stop|restart|reload|force-reload|status}” &gt;&amp;2
exit 1
;;
esac
exit 0

NFS Backup Script


#!/bin/sh

Backup Files
backup_files=”/home /var/spool/mail /etc /root /boot /opt”
dest=”/mnt/backup”

Create Archive Filename
day=$(date +%A)
hostname=$(hostname -s)
archive_file=”$hostname-$day.tgz”

Print Start Status Message
echo “Backing up $backup_files to $dest/$archive_file”
date
echo

Backup Files Using tar
tar czf $dest/$archive_file $backup_files

Print End Status Message
echo “Backup finished”
date

Long Listing Of Files in $dest to Check Sile Sizes
ls -lh $dest

MySQL Creating User & Grant Access


#!/bin/bash

Ask User to Enter Database Name
read -p “Please Enter Database Name:” dbname

Checking if Database Exist
mysql -Bse “USE $dbname” 2> /dev/null
if [ $? -eq 0 ]; then
read -p “Please enter the username you wish to create : ” username
read -p “Please Enter Host To Allow Access Eg: %,ip or hostname : ” host
read -p “Please Enter the Password for New User ($username) : ” password

MySQL Query Will Create New User & Grant Privileges
query=”GRANT ALL PRIVILEGES ON $dbname.* TO $username@’$host’ IDENTIFIED BY ‘$password'”;

Ask User to Confirm Entered Data
read -p “Executing Query : $query , Please Confirm (y/n) : ” confirm
if [ “$confirm” == ‘y’ ]; then
mysql -e “$query”

Update Privileges
mysql -e “flush privileges”
else
read -p “Aborted, Press any key to continue..”
fi
else
echo “The Database: $dbname does not exist, please specify a database that exists”;
fi

MySQL Database Login SSH Servers


#!/bin/bash

hostname=${1:?”host name?”}
regexp=”(.):(.):(.*)”

Set the sshdb Data to be default if the Database is UNSpecified
default_sshdb=”/home/jing/sshserver/sshdb”
sshdb=${2:-$default_sshdb}

Read the Host to be Connected
account=$(awk -v host=$hostname ‘
/^[^#].*/ {
if ( $1 ~ host ) {
printf $2 “:”$3 “:”$4
exit
}
}
‘ $sshdb)
if [[ $account =~ $regexp ]];then
ip=${BASH_REMATCH[1]}
username=${BASH_REMATCH[2]}
password=${BASH_REMATCH[3]}
fi

Make the Connection
/usr/bin/expect -f /home/jing/sshserver/expectedssh $username $ip $password

MySQL Creating User Account


#!/bin/bash

Create a full user account
/usr/sbin/useradd -m -k /home/template -g apache $1
echo “$2” | /usr/bin/passwd –stdin $1
chmod 750 /home/$1
echo “create database $1; GRANT all on $1.* to $1@’localhost’ identified by ‘$2′” | \
/usr/local/mysql/bin/mysql -hlocalhost -uroot -p$3
echo “set password for ‘$1’@’localhost’ = OLD_PASSWORD(‘$2’)” | \
/usr/local/mysql/bin/mysql -hlocalhost -uroot -p$3

Multiple Database Backup & Clean Up Older Files Script


#!/bin/bash

Intializing Date
DATE=date +%Y%m%d

Using Mysqldump taking Multiple Backups
mysqldump -h localhost –opt -u backups –password=some_pass website1dbname > /home/username/backups/database/website1dbname-backup-$DATE.sql
zip /home/username/backups/database/website1dbname-backup-$DATE.zip /home/username/backups/database/website1dbname-backup-$DATE.sql
rm /home/username/backups/database/website1dbname-backup-$DATE.sql

mysqldump -h localhost –opt -u backups –password=some_pass website2dbname > /home/username/backups/database/website2dbname-backup-$DATE.sql
zip /home/username/backups/database/website2dbname-backup-$DATE.zip /home/username/backups/database/website2dbname-backup-$DATE.sql
rm /home/username/backups/database/website2dbname-backup-$DATE.sql

Deleting Older Files
find /home/username/backups/database/* -type f -mtime +2 -delete

MySQL Backup Database Using Shell Script


#!/bin/bash

    Create Directory with Date where Database backup will be stored
month=$(date | awk ‘{print $2}’)
day=$(date | awk ‘{print $3}’ )
year=$(date | awk ‘{print $6}’)
foldername=$(echo $day$month$year”_backups”)

    List all the databases in /usr/local/dblist file.
mysql -u root -p’mysqlpassword’ -e ‘show databases’ >/usr/local/dblist
list=$(cat /usr/local/dblist)
echo $foldername

    Create Backup Directory in /Backup/mysqlbackup
mkdir -p /Backup/mysqlbackup/$foldername
for i in $list
do
echo $i
mysqldump -u root -p’mysqlpassword’ $i | gzip > /Backup/mysqlbackup/$foldername/$i.sql.gz
echo ” “$i”.sql.gz file saved..”
done