Showing posts with label Shell Script To Record Terminal Desktop Sessions Under Linux or Unix Like Operating Systems. Show all posts
Showing posts with label Shell Script To Record Terminal Desktop Sessions Under Linux or Unix Like Operating Systems. Show all posts

Thursday, March 14, 2013

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)