Showing posts with label linux shell script. Show all posts
Showing posts with label linux shell script. Show all posts

Thursday, March 14, 2013

Linux Shell Script To Backup and Restore MBR Master Boot Record



Linux Shell Script To Backup and Restore MBR Master Boot Record


 

mbrback shell script creates a backup of your hard drive’s MBR and its partition table. You can then use mbrback to restore the MBR boot code, full MBR, or partition table from the backup files.

 


#!/bin/bash

# Script Name: mbrback    http://igurublog.wordpress.com/downloads/script-mbrback/

# Requires: util-linux

# License: GNU GENERAL PUBLIC LICENSE Version 3 http://www.gnu.org/licenses/gpl-3.0.txt


# do not change these variables!

argsneeded=1

restoretype=""

back=""

devname=""


help ()

{

echo 'mbrback version 1.0.0'

echo 'Creates MBR and partition table backups of DEVICE named:'

echo '    HOST-DEVICE-MBR-back'

echo '    HOST-DEVICE-partition-back.sf'

echo 'Restores MBR and partition table from specified backup file'

echo 'Usage: sudo mbrback DEVICE [BACKUPFOLDER]'

echo '       (creates backup files of DEVICE)'

echo 'Usage: sudo mbrback --restoreboot DEVICE [BACKUPFILE]'

echo '       (restores MBR boot code only)'

echo 'Usage: sudo mbrback --restorefullmbr DEVICE [BACKUPFILE]'

echo '       (restores entire MBR)'

echo 'Usage: sudo mbrback --restorepart DEVICE [BACKUPFILE.sf]'

echo '       (restores partition table)'

echo 'Example: sudo mbrback sda'

echo '         (creates MBR and partition table backups of'

echo '          /dev/sda in current folder)'

echo 'Example: sudo mbrback /dev/sda'

echo '         (creates MBR and partition table backups of'

echo '          /dev/sda in current folder)'

echo 'Example: sudo mbrback sda /mybackups'

echo '         (creates MBR and partition table backups of'

echo '          /dev/sda in /mybackups)'

echo 'Example: sudo mbrback --restoreboot sda /mybackups/sys-sda-MBR-back'

echo '         (restores MBR boot code of /dev/sda using'

echo '          /mybackups/sys-sda-MBR-back)'

echo 'Example: sudo mbrback --restorepart sda /mybackups/sys-sda-partition-back.sf'

echo '         (restores partition table of /dev/sda using sfdisk file '

echo '          /mybackups/sys-sda-partition-back.sf)'

echo

echo "When restoring, mbrback will always tell you what it's going to do"

echo "and allow you to abort before it writes to disk."

echo

echo "Instructions and updates:"

echo "http://igurublog.wordpress.com/downloads/script-mbrback/"

echo

}


index=0

while [ "$1" != "" ];

do

if [ "${1:0:1}" = "-" ]; then

case "$1" in

--help | -help )

help

exit

;;

--restoreboot )

if [ "$restoretype" = "" ]; then

restoretype="boot"

else

echo 'mbrback: can only use one restore option'

exit 1

fi

;;

--restorefullmbr )

if [ "$restoretype" = "" ]; then

restoretype="fullmbr"

else

echo 'mbrback: can only use one restore option'

exit 1

fi

;;

--restorepart )

if [ "$restoretype" = "" ]; then

restoretype="part"

else

echo 'mbrback: can only use one restore option'

echo

help

exit 1

fi

;;

* )

echo "mbrback: Unknown option $1"

echo

help

exit 1

;;

esac

else

let "index+=1"

case $index in

1 )

devname=`basename "$1"`

if [ ! -b "/dev/$devname" ]; then

echo "mbrback: /dev/$devname is not a valid device"

exit 1

fi

;;

2 )

back="$1"

;;

* )

echo "mbrback: Too many arguments"

exit 1

;;

esac

fi

shift

done

if (( index < $argsneeded )) || [ "$devname" = "" ]; then

echo "mbrback: missing arguments"

echo

help

exit 1

fi


if [ `whoami` != "root" ]; then

echo 'mbrback: must be run with sudo'

exit 1

fi


sysname=$HOSTNAME


if [ "$restoretype" = "" ]; then

# create MBR and table backups

if [ "$back" = "" ]; then

back=`pwd`

else

if [ ! -d "$back" ]; then

echo "mbrback: $back is not a valid backup folder"

exit 1

fi

fi

dd if=/dev/$devname of="$back/$sysname-$devname-MBR-back" bs=512 count=1

sfdisk -d /dev/$devname > "$back/$sysname-$devname-partition-back.sf"

else

# restore

if [ "$back" = "" ]; then

echo "mbrback: you must specify a backup file"

exit 1

elif [ ! -f "$back" ]; then

echo "mbrback: file not found - $back"

exit 1

fi

if [ "$restoretype" = "boot" ] || [ "$restoretype" = "fullmbr" ]; then

sfhead=`head --bytes=21 "$back"`

if [ "$sfhead" = "# partition table of " ]; then

echo "mbrback: $back is not an MBR backup file"

exit 1

fi

if [ "$(stat -c%s "$back")" != "512" ]; then

echo "mbrback: $back is wrong size for an MBR backup file"

exit 1

fi

fi

if [ "$restoretype" = "part" ]; then

sfhead=`head --bytes=21 "$back"`

if [ "$sfhead" != "# partition table of " ]; then

echo "mbrback: $back not a valid sfdisk backup file"

exit 1

fi

echo

echo "You are about to overwrite your /dev/$devname partition table with"

echo "the contents of $back"

echo

echo "WARNING!!! Unless the partition table has been damaged or you"

echo "           have accidentally deleted a partition, you should abort."

echo

echo "WARNING!!! Restoring the partition table from an out-of-date backup"

echo "           may render ALL the data on your drive unreadable."

echo

echo "WARNING!!! Do not proceed if /dev/$devname is mounted."

echo

elif [ "$restoretype" = "boot" ]; then

echo

echo "You are about to overwrite your /dev/$devname MBR boot code with"

echo "the contents of $back"

echo

echo "WARNING: Restoring your MBR boot code from an out-of-date MBR backup"

echo "         file may render your computer unbootable."

elif [ "$restoretype" = "fullmbr" ]; then

echo

echo "You are about to overwrite your ENTIRE /dev/$devname MBR with"

echo "the contents of $back"

echo

echo "WARNING!!! The full MBR contains both boot code and the drive's"

echo "           partition table.  Unless the partition table has been"

echo "           damaged or you have accidentally deleted a partition"

echo "           you should abort and restore boot code only with"

echo "           --restoreboot instead."

echo

echo "WARNING!!! Restoring your full MBR from an out-of-date MBR backup may"

echo "           render your computer unbootable and ALL the data on your"

echo "           drive unreadable."

echo

echo "WARNING!!! Do not proceed if /dev/$devname is mounted."

fi

echo

echo "Do you want to proceed? (you must type yes to proceed)"

read s1

if [ "$s1" != "yes" ]; then

echo "mbrback: no changes made - aborted at user request"

exit 2

fi

if [ "$restoretype" = "part" ]; then

sfdisk /dev/$devname < "$back"

elif [ "$restoretype" = "boot" ]; then

dd if="$back" of=/dev/$devname bs=448 count=1

elif [ "$restoretype" = "fullmbr" ]; then

dd if="$back" of=/dev/$devname bs=512 count=1

fi

echo "/dev/$devname was updated"

fi


exit 0


Linux Shell Script To Backup and Restore MBR Master Boot Record



Linux Shell Script To Backup and Restore MBR Master Boot Record


 

mbrback shell script creates a backup of your hard drive’s MBR and its partition table. You can then use mbrback to restore the MBR boot code, full MBR, or partition table from the backup files.

 


#!/bin/bash

# Script Name: mbrback    http://igurublog.wordpress.com/downloads/script-mbrback/

# Requires: util-linux

# License: GNU GENERAL PUBLIC LICENSE Version 3 http://www.gnu.org/licenses/gpl-3.0.txt


# do not change these variables!

argsneeded=1

restoretype=""

back=""

devname=""


help ()

{

echo 'mbrback version 1.0.0'

echo 'Creates MBR and partition table backups of DEVICE named:'

echo '    HOST-DEVICE-MBR-back'

echo '    HOST-DEVICE-partition-back.sf'

echo 'Restores MBR and partition table from specified backup file'

echo 'Usage: sudo mbrback DEVICE [BACKUPFOLDER]'

echo '       (creates backup files of DEVICE)'

echo 'Usage: sudo mbrback --restoreboot DEVICE [BACKUPFILE]'

echo '       (restores MBR boot code only)'

echo 'Usage: sudo mbrback --restorefullmbr DEVICE [BACKUPFILE]'

echo '       (restores entire MBR)'

echo 'Usage: sudo mbrback --restorepart DEVICE [BACKUPFILE.sf]'

echo '       (restores partition table)'

echo 'Example: sudo mbrback sda'

echo '         (creates MBR and partition table backups of'

echo '          /dev/sda in current folder)'

echo 'Example: sudo mbrback /dev/sda'

echo '         (creates MBR and partition table backups of'

echo '          /dev/sda in current folder)'

echo 'Example: sudo mbrback sda /mybackups'

echo '         (creates MBR and partition table backups of'

echo '          /dev/sda in /mybackups)'

echo 'Example: sudo mbrback --restoreboot sda /mybackups/sys-sda-MBR-back'

echo '         (restores MBR boot code of /dev/sda using'

echo '          /mybackups/sys-sda-MBR-back)'

echo 'Example: sudo mbrback --restorepart sda /mybackups/sys-sda-partition-back.sf'

echo '         (restores partition table of /dev/sda using sfdisk file '

echo '          /mybackups/sys-sda-partition-back.sf)'

echo

echo "When restoring, mbrback will always tell you what it's going to do"

echo "and allow you to abort before it writes to disk."

echo

echo "Instructions and updates:"

echo "http://igurublog.wordpress.com/downloads/script-mbrback/"

echo

}


index=0

while [ "$1" != "" ];

do

if [ "${1:0:1}" = "-" ]; then

case "$1" in

--help | -help )

help

exit

;;

--restoreboot )

if [ "$restoretype" = "" ]; then

restoretype="boot"

else

echo 'mbrback: can only use one restore option'

exit 1

fi

;;

--restorefullmbr )

if [ "$restoretype" = "" ]; then

restoretype="fullmbr"

else

echo 'mbrback: can only use one restore option'

exit 1

fi

;;

--restorepart )

if [ "$restoretype" = "" ]; then

restoretype="part"

else

echo 'mbrback: can only use one restore option'

echo

help

exit 1

fi

;;

* )

echo "mbrback: Unknown option $1"

echo

help

exit 1

;;

esac

else

let "index+=1"

case $index in

1 )

devname=`basename "$1"`

if [ ! -b "/dev/$devname" ]; then

echo "mbrback: /dev/$devname is not a valid device"

exit 1

fi

;;

2 )

back="$1"

;;

* )

echo "mbrback: Too many arguments"

exit 1

;;

esac

fi

shift

done

if (( index < $argsneeded )) || [ "$devname" = "" ]; then

echo "mbrback: missing arguments"

echo

help

exit 1

fi


if [ `whoami` != "root" ]; then

echo 'mbrback: must be run with sudo'

exit 1

fi


sysname=$HOSTNAME


if [ "$restoretype" = "" ]; then

# create MBR and table backups

if [ "$back" = "" ]; then

back=`pwd`

else

if [ ! -d "$back" ]; then

echo "mbrback: $back is not a valid backup folder"

exit 1

fi

fi

dd if=/dev/$devname of="$back/$sysname-$devname-MBR-back" bs=512 count=1

sfdisk -d /dev/$devname > "$back/$sysname-$devname-partition-back.sf"

else

# restore

if [ "$back" = "" ]; then

echo "mbrback: you must specify a backup file"

exit 1

elif [ ! -f "$back" ]; then

echo "mbrback: file not found - $back"

exit 1

fi

if [ "$restoretype" = "boot" ] || [ "$restoretype" = "fullmbr" ]; then

sfhead=`head --bytes=21 "$back"`

if [ "$sfhead" = "# partition table of " ]; then

echo "mbrback: $back is not an MBR backup file"

exit 1

fi

if [ "$(stat -c%s "$back")" != "512" ]; then

echo "mbrback: $back is wrong size for an MBR backup file"

exit 1

fi

fi

if [ "$restoretype" = "part" ]; then

sfhead=`head --bytes=21 "$back"`

if [ "$sfhead" != "# partition table of " ]; then

echo "mbrback: $back not a valid sfdisk backup file"

exit 1

fi

echo

echo "You are about to overwrite your /dev/$devname partition table with"

echo "the contents of $back"

echo

echo "WARNING!!! Unless the partition table has been damaged or you"

echo "           have accidentally deleted a partition, you should abort."

echo

echo "WARNING!!! Restoring the partition table from an out-of-date backup"

echo "           may render ALL the data on your drive unreadable."

echo

echo "WARNING!!! Do not proceed if /dev/$devname is mounted."

echo

elif [ "$restoretype" = "boot" ]; then

echo

echo "You are about to overwrite your /dev/$devname MBR boot code with"

echo "the contents of $back"

echo

echo "WARNING: Restoring your MBR boot code from an out-of-date MBR backup"

echo "         file may render your computer unbootable."

elif [ "$restoretype" = "fullmbr" ]; then

echo

echo "You are about to overwrite your ENTIRE /dev/$devname MBR with"

echo "the contents of $back"

echo

echo "WARNING!!! The full MBR contains both boot code and the drive's"

echo "           partition table.  Unless the partition table has been"

echo "           damaged or you have accidentally deleted a partition"

echo "           you should abort and restore boot code only with"

echo "           --restoreboot instead."

echo

echo "WARNING!!! Restoring your full MBR from an out-of-date MBR backup may"

echo "           render your computer unbootable and ALL the data on your"

echo "           drive unreadable."

echo

echo "WARNING!!! Do not proceed if /dev/$devname is mounted."

fi

echo

echo "Do you want to proceed? (you must type yes to proceed)"

read s1

if [ "$s1" != "yes" ]; then

echo "mbrback: no changes made - aborted at user request"

exit 2

fi

if [ "$restoretype" = "part" ]; then

sfdisk /dev/$devname < "$back"

elif [ "$restoretype" = "boot" ]; then

dd if="$back" of=/dev/$devname bs=448 count=1

elif [ "$restoretype" = "fullmbr" ]; then

dd if="$back" of=/dev/$devname bs=512 count=1

fi

echo "/dev/$devname was updated"

fi


exit 0


Shell Script To Record Terminal Desktop Sessions Under Linux or Unix Like Operating Systems


Shell Script To Record Terminal Desktop Sessions Under Linux or Unix Like Operating Systems


simple shell script wrapper to record current terminal session of a linux or unix desktop. This script act as a wrapper to recordMyDesktop command which produces a file that contains a video and audio recording of a linux desktop session. The default behavior of recording is to mark areas that have changed (through libxdamage) and update the frame. This wrapper gets current window name and remove window decoration.

 


#!/bin/bash

# A simple shell script wrapper to record current terminal session of a linux 

# desktop. May work under other Unix like operating systems too. 

# Tested on RHEL 6.x, Debian 6.x, and Ubuntu Linux

# ----------------------------------------------------------------------------

# Written by Vivek Gite

# (c) 2012 nixCraft under GNU GPL v2.0+

# ----------------------------------------------------------------------------

# Last updated: 19/Aug/2012

# ----------------------------------------------------------------------------

 

_xw=/usr/bin/xwininfo

_recd=/usr/bin/recordmydesktop

_awk=/usr/bin/awk

_grep=/bin/grep

_file="$1"

_output=""

 

# change this to match your PS1 settings

_regex='vivek@wks01: '

 

die(){

echo -e "$1"

exit ${2:9999}

}

 

[ $# -eq 0 ] && die "Usage: $0 filename.ogv\n\nRecord terminal desktop sessions under Linux or Unix." 1

 

 

# add extension .ogv if not given

_ext="${_file%%.ogv}"

[[ "$_ext" == "$_file" ]] && _output="$_file.ogv" || _output="$_file"

 

[ ! -x "$_xw" ] && die "Error: $_xw not found or set correct \$_xw in $0" 2

[ ! -x "$_recd" ] && die "Error: $_recd not found or set correct \$_recd in $0" 3

[ ! -x "$_awk" ] && die "Error: $_awk not found or set correct \$_awk in $0" 4

[ ! -x "$_grep" ] && die "Error: $_grep not found or set correct \$_grep in $0" 5

 

#get terminal window id

_id=$($_xw -root -tree | $_grep "$_regex" | $_awk '{ print $1}')

 

#get terminal windows  x,y, width, and hight

_x=$($_xw -id $_id | $_grep 'Absolute upper-left X' | $_awk '{ print $4}')

_y=$($_xw -id $_id | $_grep 'Absolute upper-left Y' | $_awk '{ print $4}')

_w=$($_xw -id $_id | $_grep 'Width:' | $_awk '{ print $2}')

_h=$($_xw -id $_id | $_grep 'Height:' | $_awk '{ print $2}')

 

x=$(( $_x + 8 ))

y=$(( $_y + 57 ))

width=$(( $_w -31 ))

height=$(( $_h -62 ))

 

$_recd --no-sound -x $x  -y $y --width $width --height $height -o $_output

 


How do I use this script?




    1. Open a terminal.

    2. Open Tab.

    3. Run script as:



./script.sh foo.ogv



    1. Switch to tab # 1 and run your command.

    2. When done switch back to tab #2 and press CTRL + C to save recording to foo.ogv.

    3. To run foo.ogv type:



gnome-open foo.ogv
OR
mplayer foo.ogv



Shell Script To Record Terminal Desktop Sessions Under Linux or Unix Like Operating Systems


Shell Script To Record Terminal Desktop Sessions Under Linux or Unix Like Operating Systems


simple shell script wrapper to record current terminal session of a linux or unix desktop. This script act as a wrapper to recordMyDesktop command which produces a file that contains a video and audio recording of a linux desktop session. The default behavior of recording is to mark areas that have changed (through libxdamage) and update the frame. This wrapper gets current window name and remove window decoration.

 


#!/bin/bash

# A simple shell script wrapper to record current terminal session of a linux 

# desktop. May work under other Unix like operating systems too. 

# Tested on RHEL 6.x, Debian 6.x, and Ubuntu Linux

# ----------------------------------------------------------------------------

# Written by Vivek Gite

# (c) 2012 nixCraft under GNU GPL v2.0+

# ----------------------------------------------------------------------------

# Last updated: 19/Aug/2012

# ----------------------------------------------------------------------------

 

_xw=/usr/bin/xwininfo

_recd=/usr/bin/recordmydesktop

_awk=/usr/bin/awk

_grep=/bin/grep

_file="$1"

_output=""

 

# change this to match your PS1 settings

_regex='vivek@wks01: '

 

die(){

echo -e "$1"

exit ${2:9999}

}

 

[ $# -eq 0 ] && die "Usage: $0 filename.ogv\n\nRecord terminal desktop sessions under Linux or Unix." 1

 

 

# add extension .ogv if not given

_ext="${_file%%.ogv}"

[[ "$_ext" == "$_file" ]] && _output="$_file.ogv" || _output="$_file"

 

[ ! -x "$_xw" ] && die "Error: $_xw not found or set correct \$_xw in $0" 2

[ ! -x "$_recd" ] && die "Error: $_recd not found or set correct \$_recd in $0" 3

[ ! -x "$_awk" ] && die "Error: $_awk not found or set correct \$_awk in $0" 4

[ ! -x "$_grep" ] && die "Error: $_grep not found or set correct \$_grep in $0" 5

 

#get terminal window id

_id=$($_xw -root -tree | $_grep "$_regex" | $_awk '{ print $1}')

 

#get terminal windows  x,y, width, and hight

_x=$($_xw -id $_id | $_grep 'Absolute upper-left X' | $_awk '{ print $4}')

_y=$($_xw -id $_id | $_grep 'Absolute upper-left Y' | $_awk '{ print $4}')

_w=$($_xw -id $_id | $_grep 'Width:' | $_awk '{ print $2}')

_h=$($_xw -id $_id | $_grep 'Height:' | $_awk '{ print $2}')

 

x=$(( $_x + 8 ))

y=$(( $_y + 57 ))

width=$(( $_w -31 ))

height=$(( $_h -62 ))

 

$_recd --no-sound -x $x  -y $y --width $width --height $height -o $_output

 


How do I use this script?




    1. Open a terminal.

    2. Open Tab.

    3. Run script as:



./script.sh foo.ogv



    1. Switch to tab # 1 and run your command.

    2. When done switch back to tab #2 and press CTRL + C to save recording to foo.ogv.

    3. To run foo.ogv type:



gnome-open foo.ogv
OR
mplayer foo.ogv



RHEL / CentOS Linux: Nginx Chroot Jail Start / Stop / Restart Shell Script


RHEL / CentOS Linux: Nginx Chroot Jail Start / Stop / Restart Shell Script


simple shell script to start / stop / restart chrooted nginx web server under CentOS / RHEL Linux. You must have Nginx web server setup in a chroot (jail) so that you can minimizes the damage done by a potential break-in by isolating the web server to a small section of the filesystem. You can also mount $jail/tmp as a separate filesystem (/images/tmpfile.bin) with the noexec,nosuid, nodev options under Linux like operating systems.

 


#!/bin/bash

# Name : nginx.rc  

# URL: http://bash.cyberciti.biz/security/linux-nginx-start-stop-restart-chrooted-jail/

# Purpose: A simple shell script wrapper to chroot nginx in $_newroot under Linux

# ----------------------------------------------------------------------------

# Author: nixCraft

# Copyright: 2011 nixCraft under GNU GPL v2.0+

# ----------------------------------------------------------------------------

# Last updated: 18/Dec/2012 - Added support for secure /tmp mount

# Last updated: 19/Dec/2012 - Bug fixed in ln 

# Last updated: 10/Mar/2013 - Bug fixed in status()

# ----------------------------------------------------------------------------

 

# jail location - must be up, see how to setup nginx using chroot

# http://www.cyberciti.biz/faq/howto-run-nginx-in-a-chroot-jail/

_newroot="/nginxjail"

 

# RHEL nginx and other binary paths

_nginx="/usr/sbin/nginx"

_chroot="/usr/sbin/chroot"

_killall="/usr/bin/killall"

 

# 0 turn off or # 1 turn on

_securetmp=0

_securetmproot="/path/to/images/nginx_jail_tmp.bin"

 

 

[ ! -d "$_newroot" ] &# mount /tmp securely inside $_newroot

# see http://www.cyberciti.biz/faq/howto-mount-tmp-as-separate-filesystem-with-noexec-nosuid-nodev/

mounttmp(){

if [ $_securetmp -eq 1 ]

then

mount | grep -q $_securetmproot

if [ $? -eq  0 ]

then

echo "*** Secure root enabled and mounted ***"

else

echo "*** Turning on secure /tmp..."

[ ! -f "$_securetmproot" ] &mount -o loop,noexec,nosuid,rw "$_securetmproot" "$_newroot/tmp"

chmod 1777 "$_newroot/tmp"

rm -rf "$_newroot/var/tmp"

ln -s ../tmp "$_newroot/var/tmp"

fi

fi

}

 

start(){

echo -en "Starting nginx...\t\t\t"

$_chroot $_newroot $_nginx && echo -en "[ OK ]" || echo "[ Failed ]"

}

 

stop(){

echo -en "Stoping nginx...\t\t\t"

$_killall "${_nginx##*/}" && echo -en "[ OK ]" || echo "[ Failed ]"

}

 

reload(){

echo -en "Reloading nginx...\t\t\t"

$_chroot $_newroot $_nginx -s reload && echo -en "[ OK ]" || echo "[ Failed ]"

}

 

## Fancy status

status(){

echo 

pgrep -u ${_nginx##*/} ${_nginx##*/} &>/dev/null

[ $? -eq 0 ] && echo "*** Nginx running on $(hostname) ***" || echo "*** Nginx not found on $(hostname) ***"

echo 

echo "*** PID ***"

#pgrep -u ${_nginx##*/} ${_nginx##*/}

ps aux | grep "${_nginx##*/}" | egrep -v 'grep|bash'

echo

 

echo "FD stats:"

for p in $(pidof ${_nginx##*/}); do echo "PID # $p has $(lsof -n -a -p $p|wc -l) fd opend."; done

echo

 

echo "Jail dir location:"

pwdx $(pgrep -u "root" "${_nginx##*/}") | grep  --color "$_newroot"

echo 

 

echo "*** PORT ***"

netstat -tulpn | egrep --color  ':80|:443'

}

 

## Make sure /tmp is securely mounted inside jail ##

mounttmp

 

## main ##

case "$1" in

        start)

                start 

                ;;

        stop)

                stop 

                ;;

        restart)

                stop 

                start 

                ;;

        reload)

                reload

;; 

        status)

                status

                ;;

        *)

                echo $"Usage: $0 {start|stop|restart|reload|status}"

                ;;

esac

 

# just send \n

echo



How do I use this script?


Download the script:# cd /tmp
# wget http://bash.cyberciti.biz/dl/593.sh.zip
# unzip 593.sh.zip
# mv 593.sh /etc/rc.d/nginx.jail.rc
# chmod +x /etc/rc.d/nginx.jail.rc

Use it as follows:
# /etc/rc.d/nginx.jail.rc start
# /etc/rc.d/nginx.jail.rc stop
# /etc/rc.d/nginx.jail.rc restart
# /etc/rc.d/nginx.jail.rc status

Sample outputs:

Fig.01 nginx.rc in action
Fig.01 nginx.rc in action




RHEL / CentOS Linux: Nginx Chroot Jail Start / Stop / Restart Shell Script


RHEL / CentOS Linux: Nginx Chroot Jail Start / Stop / Restart Shell Script


simple shell script to start / stop / restart chrooted nginx web server under CentOS / RHEL Linux. You must have Nginx web server setup in a chroot (jail) so that you can minimizes the damage done by a potential break-in by isolating the web server to a small section of the filesystem. You can also mount $jail/tmp as a separate filesystem (/images/tmpfile.bin) with the noexec,nosuid, nodev options under Linux like operating systems.

 


#!/bin/bash

# Name : nginx.rc  

# URL: http://bash.cyberciti.biz/security/linux-nginx-start-stop-restart-chrooted-jail/

# Purpose: A simple shell script wrapper to chroot nginx in $_newroot under Linux

# ----------------------------------------------------------------------------

# Author: nixCraft

# Copyright: 2011 nixCraft under GNU GPL v2.0+

# ----------------------------------------------------------------------------

# Last updated: 18/Dec/2012 - Added support for secure /tmp mount

# Last updated: 19/Dec/2012 - Bug fixed in ln 

# Last updated: 10/Mar/2013 - Bug fixed in status()

# ----------------------------------------------------------------------------

 

# jail location - must be up, see how to setup nginx using chroot

# http://www.cyberciti.biz/faq/howto-run-nginx-in-a-chroot-jail/

_newroot="/nginxjail"

 

# RHEL nginx and other binary paths

_nginx="/usr/sbin/nginx"

_chroot="/usr/sbin/chroot"

_killall="/usr/bin/killall"

 

# 0 turn off or # 1 turn on

_securetmp=0

_securetmproot="/path/to/images/nginx_jail_tmp.bin"

 

 

[ ! -d "$_newroot" ] &# mount /tmp securely inside $_newroot

# see http://www.cyberciti.biz/faq/howto-mount-tmp-as-separate-filesystem-with-noexec-nosuid-nodev/

mounttmp(){

if [ $_securetmp -eq 1 ]

then

mount | grep -q $_securetmproot

if [ $? -eq  0 ]

then

echo "*** Secure root enabled and mounted ***"

else

echo "*** Turning on secure /tmp..."

[ ! -f "$_securetmproot" ] &mount -o loop,noexec,nosuid,rw "$_securetmproot" "$_newroot/tmp"

chmod 1777 "$_newroot/tmp"

rm -rf "$_newroot/var/tmp"

ln -s ../tmp "$_newroot/var/tmp"

fi

fi

}

 

start(){

echo -en "Starting nginx...\t\t\t"

$_chroot $_newroot $_nginx && echo -en "[ OK ]" || echo "[ Failed ]"

}

 

stop(){

echo -en "Stoping nginx...\t\t\t"

$_killall "${_nginx##*/}" && echo -en "[ OK ]" || echo "[ Failed ]"

}

 

reload(){

echo -en "Reloading nginx...\t\t\t"

$_chroot $_newroot $_nginx -s reload && echo -en "[ OK ]" || echo "[ Failed ]"

}

 

## Fancy status

status(){

echo 

pgrep -u ${_nginx##*/} ${_nginx##*/} &>/dev/null

[ $? -eq 0 ] && echo "*** Nginx running on $(hostname) ***" || echo "*** Nginx not found on $(hostname) ***"

echo 

echo "*** PID ***"

#pgrep -u ${_nginx##*/} ${_nginx##*/}

ps aux | grep "${_nginx##*/}" | egrep -v 'grep|bash'

echo

 

echo "FD stats:"

for p in $(pidof ${_nginx##*/}); do echo "PID # $p has $(lsof -n -a -p $p|wc -l) fd opend."; done

echo

 

echo "Jail dir location:"

pwdx $(pgrep -u "root" "${_nginx##*/}") | grep  --color "$_newroot"

echo 

 

echo "*** PORT ***"

netstat -tulpn | egrep --color  ':80|:443'

}

 

## Make sure /tmp is securely mounted inside jail ##

mounttmp

 

## main ##

case "$1" in

        start)

                start 

                ;;

        stop)

                stop 

                ;;

        restart)

                stop 

                start 

                ;;

        reload)

                reload

;; 

        status)

                status

                ;;

        *)

                echo $"Usage: $0 {start|stop|restart|reload|status}"

                ;;

esac

 

# just send \n

echo



How do I use this script?


Download the script:# cd /tmp
# wget http://bash.cyberciti.biz/dl/593.sh.zip
# unzip 593.sh.zip
# mv 593.sh /etc/rc.d/nginx.jail.rc
# chmod +x /etc/rc.d/nginx.jail.rc

Use it as follows:
# /etc/rc.d/nginx.jail.rc start
# /etc/rc.d/nginx.jail.rc stop
# /etc/rc.d/nginx.jail.rc restart
# /etc/rc.d/nginx.jail.rc status

Sample outputs:

Fig.01 nginx.rc in action
Fig.01 nginx.rc in action




Linux / Unix Script: Simple Process Checker To Find Out If A Service Is Running or Not


Linux / Unix Script: Simple Process Checker To Find Out If A Service Is Running or Not


 

simple shell script to find out whether critical services are running or, not under Linux or Unix operating systems. The script can send notification using email.

 

#!/bin/bash
# Name : service.chk 
# URL: http://bash.cyberciti.biz/monitoring/simple-process-checker-script/
# Purpose: A simple process checker. Find out if service is running or not.
# Tested on: Debian and RHEL based system only.
# ----------------------------------------------------------------------------
# Author: nixCraft
# Copyright: 2009 nixCraft under GNU GPL v2.0+
# ----------------------------------------------------------------------------
# Last updated: 13/Mar/2013 - Added support for email and other enhancements 
# Last updated: 05/Dec/2011 - Added support for binary path check
# ----------------------------------------------------------------------------

## Change as per your distro 
_pgrep="/usr/bin/pgrep"
_mail="/usr/bin/mail"

## Add binary list here
_chklist="/usr/bin/php-cgi /usr/sbin/nginx /usr/sbin/lighttpd /usr/sbin/mysqld /usr/sbin/apache2 /usr/sbin/named /usr/sbin/pgsqld"

## yes | no
_sendemail="no"

## Add your email id
_email="your@mobile.email.id.example.com"

## Do not change below
_failed="false"
_service="Service:"

_running() {
local p="${1##*/}"
local s="true"
$_pgrep "${p}" >/dev/null || { s="false"; _failed="true"; _service="${_service} $1,"; }
[[ "$s" == "true" ]] && echo "$1 running" || { echo -n "$1 not running"; [[ ! -f "$1" ]] && echo " [ $1 not found ]" || echo ; }
}

## header
echo "Service status on ${HOSTNAME} @ $(date)"
echo "------------------------------------------------------"

## Check if your service is running or not 
for s in $_chklist
do
_running "$s"
done

## Send a quick email update (good for cron jobs) ##
[[ "$_failed" == "true" && "$_sendemail" == "yes" ]] && { _mess="$_service failed on $HOSTNAME @ $(date)"; 
                                      $_mail -s 'Service not found' "$_email" < "${_mess}";
                                                   }


Sample outputs:

Fig.01: Script in action (click to enlarge)
Fig.01: Script in action (click to enlarge)


Linux / Unix Script: Simple Process Checker To Find Out If A Service Is Running or Not


Linux / Unix Script: Simple Process Checker To Find Out If A Service Is Running or Not


 

simple shell script to find out whether critical services are running or, not under Linux or Unix operating systems. The script can send notification using email.

 

#!/bin/bash
# Name : service.chk 
# URL: http://bash.cyberciti.biz/monitoring/simple-process-checker-script/
# Purpose: A simple process checker. Find out if service is running or not.
# Tested on: Debian and RHEL based system only.
# ----------------------------------------------------------------------------
# Author: nixCraft
# Copyright: 2009 nixCraft under GNU GPL v2.0+
# ----------------------------------------------------------------------------
# Last updated: 13/Mar/2013 - Added support for email and other enhancements 
# Last updated: 05/Dec/2011 - Added support for binary path check
# ----------------------------------------------------------------------------

## Change as per your distro 
_pgrep="/usr/bin/pgrep"
_mail="/usr/bin/mail"

## Add binary list here
_chklist="/usr/bin/php-cgi /usr/sbin/nginx /usr/sbin/lighttpd /usr/sbin/mysqld /usr/sbin/apache2 /usr/sbin/named /usr/sbin/pgsqld"

## yes | no
_sendemail="no"

## Add your email id
_email="your@mobile.email.id.example.com"

## Do not change below
_failed="false"
_service="Service:"

_running() {
local p="${1##*/}"
local s="true"
$_pgrep "${p}" >/dev/null || { s="false"; _failed="true"; _service="${_service} $1,"; }
[[ "$s" == "true" ]] && echo "$1 running" || { echo -n "$1 not running"; [[ ! -f "$1" ]] && echo " [ $1 not found ]" || echo ; }
}

## header
echo "Service status on ${HOSTNAME} @ $(date)"
echo "------------------------------------------------------"

## Check if your service is running or not 
for s in $_chklist
do
_running "$s"
done

## Send a quick email update (good for cron jobs) ##
[[ "$_failed" == "true" && "$_sendemail" == "yes" ]] && { _mess="$_service failed on $HOSTNAME @ $(date)"; 
                                      $_mail -s 'Service not found' "$_email" < "${_mess}";
                                                   }


Sample outputs:

Fig.01: Script in action (click to enlarge)
Fig.01: Script in action (click to enlarge)