Very Generic Linux Service Watchdog script

A strong watchdog script- indeed!

#!/bin/bash
#
# watchdog
#
# Run as a cron job to keep an eye on what_to_monitor which should always
# be running. Restart what_to_monitor and send notification as needed.
#
# This needs to be run as root or a user that can start system services.
#
# Revisions: 0.1 (20100506), 0.2 (20100507)

NAME=what_to_monitor
START=/full/path/to/$NAME
NOTIFY=person1email
NOTIFYCC=person2email
GREP=/bin/grep
PS=/bin/ps
NOP=/bin/true
DATE=/bin/date
MAIL=/bin/mail
RM=/bin/rm

$PS -ef|$GREP -v grep|$GREP $NAME >/dev/null 2>&1
case "$?" in
 0)
 # It is running in this case so we do nothing.
 $NOP
 ;;
 1)
 echo "$NAME is NOT RUNNING. Starting $NAME and sending notices."
 $START 2>&1 >/dev/null &
 NOTICE=/tmp/watchdog.txt
 echo "$NAME was not running and was started on `$DATE`" > $NOTICE
 $MAIL -n -s "watchdog notice" -c $NOTIFYCC $NOTIFY < $NOTICE
 $RM -f $NOTICE
 ;;
esac

exit

Src: http://blog.eracc.com/2010/05/08/linux-monitor-a-service-with-a-watchdog-script/

Share

IP Setting on CentOS6 using Shell Script

#!/bin/bash

if [ $# -eq 5 ]
then

echo ""
echo "Taking the backup and Changing the hostname from $(hostname) to $1 ..."

sed -i.bk "s/$(hostname)/$1/g" /etc/sysconfig/network

echo ""
echo "Backing up & Assigning the Static IP ..."
echo ""

cp /etc/sysconfig/network-scripts/ifcfg-$2 /etc/sysconfig/network-scripts/$2.bk

cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-$2

DEVICE=$2
BOOTPROTO=static
IPADDR=$3.$4
NETMASK=255.255.255.0
GATEWAY=$3.$5
ONBOOT=yes
EOF

echo "Changing the dns ..."
echo ""

sed -i.bk "s/nameserver.*/nameserver $3.$5/" /etc/resolv.conf

echo "Adding $1 as hostname to the /etc/hosts file .."
echo ""

sed -i.bk "/$(hostname)$/d" /etc/hosts
echo "$3.$4 $1" >> /etc/hosts

echo "Restarting the Network Service, Please connect it using the new IP Address if you are using ssh ..."

service network restart

else

echo "Usage: ip.sh <hostname> <interface> <baseip> <ipaddress> <gateway/dns>"
echo "Example: ip.sh testname eth0 10.10.10 41 1"

fi
Share

Bash Shellshock fix with scripts for Debian, Ubuntu, CentOS and other distros. including old

First check if your Bash is vulnerable, execute the following command-

env x='() { :;}; echo vulnerable’ bash -c ‘echo this is a test’

If your system is vulnerable, you will see:

vulnerable

this is a test

If your system is not vulnerable, you will see:

bash: warning: x: ignoring function definition attempt

bash: error importing function definition for `x’

this is a test

To check for the vulnerability CVE-2014-6271, run the following in Bash:

env X='() { (a)=>\’ sh -c “echo date”; cat echo

If your system is vulnerable, you will see:

bash: X: line 1: syntax error near unexpected toke `=’

bash: X: line 1: `’

bash: error importing function for `X’

Sun Sep 08:17:32 EST 2014

If your system is not vulernable, you will see:

date

cat: echo: No such file or directory

To test the vulnerability CVE-2014-7186, run the following in Bash:

bash -c ‘true <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF’ || echo “CVE-2014-7186 vulnerable, redir_stack”

If your system is not vulnerable, you will see:

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

So, let’s start fixing it!

 

Below, I’ve tried to put various contributors solution to place in a single entry.

For Debian 6 (Squeeze)

 

Append this to your sources.list:

deb http://http.debian.net/debian squeeze-lts main contrib non-free

deb-src http://http.debian.net/debian squeeze-lts main contrib non-free

and then run

apt-get update

apt-get install -t squeeze-lts –only-upgrade bash Continue reading “Bash Shellshock fix with scripts for Debian, Ubuntu, CentOS and other distros. including old” »

Share

CRON scripts to running in seconds interval

 

This problem can be solved with simple bash script. For example, if you need to run a PHP script on every 20 seconds, you can create a bash script like this:
#!/bin/bash
#Name:myscript.sh
#Desc:Run script in every 20 seconds
while (sleep 20 && php /path_to_your_script/your_script_name.php) &
do
wait $!
done
Then, make script executable, and add it to the system startup. That’s all.
Share

Ozeki PHP Plugin

<?php
$gatewayURL = 'http://192.168.1.100:9333/ozeki?';
$request = 'login=sendmail';
$request .= '&password=mypassword';
$request .= '&action=sendMessage';
$request .= '&messageType=SMS:TEXT';
$request .= '&recepient='.urlencode('01711673093');
$request .= '&messageData='.urlencode("PHP Hello World");

$url = $gatewayURL . $request;

//Open the URL to send the message
file($url);
?>
Share

Send HTTP requests using cURL

 

Reading a URL via GET:
curl http://example.com/
Defining any HTTP method (like POST or PUT):
curl http://example.com/users/1 -XPUT
Sending data with a request:
curl http://example.com/users -d"first_name=Bruce&last_name=Wayne"
If you use -d and do not set an HTTP request method it automatically defaults to POST. Performing basic authentication:
curl http://user:password@example.com/users/1
All together now:
curl http://user:password@example.com/users/1 -XPUT -d"screen_name=batman"
Share

PHP enable error reporting

You can set PHP error reporting on in php.ini file (in case you have access to this file). Set the following lines:

error_reporting = E_ALL
display_errors = On

If you have no access to php.ini file but you can modify .htaccess file on your server (in root of your public_hml folder – your Joomla! site), try to add the following code there (on some servers this option is disabled, so you need to ask your webhosting operator):

# Displaying PHP errors
php_flag display_errors on
php_value error_reporting 6143

Share