Tag: script

  • Simple way to get stats from Apache

    This way you can get stats from your logs but this method is a little bit rudimentary as you are going to get visits from crawlers, bots,… that are not real visits. Parse your logs directly with awk.

    #!/bin/sh

    echo 'Number of unique visitors'
    cat /var/log/apache/www.yoursite.com-access.log |awk '{print $1}' | sort | uniq | wc -l

    echo 'Ip of unique visitors'
    cat cat /var/log/apache/www.yoursite.com-access.log |awk '{print $1}' | sort | uniq | wc -l |awk '{print $1}' | sort | uniq

  • HTTP Proxy Checker in bash

    Usefull script  to check if a proxy is alive.

    #!/bin/bash
    # HTTP Proxy Server's IP Address (or URL)
    proxy_server=$1

    # HTTP Proxy Server's Port Number
    port=$2

    # We're trying to reach this url via the given HTTP Proxy Server
    # (http://www.google.com by default)
    url="http://www.google.com"

    # Timeout time (in seconds)
    timeout=20

    # We're fetching the return code and assigning it to the $result variable
    result=`HEAD -d -p http://$proxy_server:$port -t $timeout $url`

    # If the return code is 200, we've reached to $url successfully
    if [ "$result" = "200 OK" ]; then
    echo "1 (proxy works)"
    # Otherwise, we've got a problem (either the HTTP Proxy Server does not work
    # or the request timed out)
    else
    echo "0 (proxy does not work or request timed out)"
    fi

  • Google URL shortener script

    Maybe it’s no longer working

    #!/bin/bash
    # Google url shortener bash script
    # For information about the app:
    # http://ggl-shortener.appspot.com/instructions/

    app='http://ggl-shortener.appspot.com/?url='
    url="$1"
    protocol=`echo "$1" | sed -e "/^http:\/\//g"`

    if [ -z "$1" ]; then
    echo -e "you need to pass the url through an argument";
    echo -e "e.g. `basename $0` http://url";
    else
    if [ ! "$protocol" ]; then
    curl -s "$app$url" | sed -e 's/{"short_url":"//' -e 's/"}/\n/g'
    else
    repl=`echo "$1" | sed -e 's/^/http:\/\//g'`
    curl -s "$app$repl" | sed -e 's/{"short_url":"//' -e 's/{"error_message":"Bad request/error: bad request/' -e 's/"}/\n/g'
    fi;
    fi

  • Mount Share Network Event Triggered Bash Script

    What this script does is, check the gateway mac address to find out if you are within your lan or in a different place. If at home, mount a share as normal. If away, mount that share as a ssh file system.

    #!/bin/bash
    gateway=$(ip route show 0.0.0.0/0 | awk '{print $3}')
    mactest=$(arp -n -a $gateway | awk '{print $4}')
    targetmac="XX:XX:XX:XX:XX:XX"
    homeup="mount -t cifs -o username=USER,password=PASSWORD //SERVER/SHARE /mnt/remote"
    awayup="sshfs my.dyndns.tld:/path/to/share /mnt/remote"
    down="umount -l /mnt/remote"
    if [ $mactest==$targetmac ]
    then
    case "$2" in
    up)
    $homeup
    ;;
    down)
    $down
    ;;
    esac
    else
    case "$2" in
    up)
    $awayup
    ;;
    down)
    $down
    ;;
    esac
    fi

    exit $?

  • Delete Matching Mail in sendmail queue

     Quick script to delete matching emails in sendmail queue. Really usefull when an account has been hacked and you have thounsands of spam emails in the queue.

    #!/bin/sh

    cd /var/spool/mqueue
    for i in `grep -sl "" qf*`
    do
    j=`ls -1 "$i" | sed 's/qf\(.*\)$/df\1/'`
    echo "Deleting $i ($j)"
    rm $i $j
    done

  • Find absolute path of Bash script

    You should run this command within a script!
     

    abspath="$(cd "${0%/*}" 2>/dev/null; echo "$PWD"/"${0##*/}")"

  • Dirty script to find out free storage space in mount points

    Dirty script to find out free storage space.
     

    for path in `mount | awk '/^\/dev/ {print $3}'`; do df -h "$path"; done;

  • Check if an input is numeric or char

     With this script you can check if an argument to a script is numeric, char or alphanumeric.

    [[ -z "$1" ]] && echo "I cant work without an input" && exit 1

    INPUT="$@"

    [[ "$INPUT" == ?(+|-)+([0-9]) ]] && echo "$INPUT is numeric" && exit 0

    [[ "$INPUT" == +([a-zA-Z]) ]] && echo "$INPUT is character" && exit 0

    [[ "$INPUT" == *([0-9]|[a-zA-Z])* ]] && echo "$INPUT is alpha-numeric" && exit 0

  • Yes/No GUI dialog with gdialog

    An gdialog example in linux. A dialog prompt for yes or no.

    #!/bin/bash
    gdialog --title "some title for the dialog" --yesno "some text \n the yes/no question."

    if [ $? == 0 ]; then
    {commands for yes}
    else
    {maybe other commands for no}
    fi

  • Bash menu in bash with getopts

    A little more sophisticated script than the former bash menu example to get some arguments from command line.

    #!/bin/bash

    while getopts "u:p:" opt; do

    case $opt in
    u)
    echo "-u was triggered, Parameter: $OPTARG"
    dbuser="$OPTARG"
    ;;
    p)
    echo "-p was triggered, Parameter: $OPTARG"
    dbpass="$OPTARG"
    ;;
    \?)
    echo "Invalid option: -$OPTARG"
    exit 1
    ;;
    :)
    echo "Option -$OPTARG requires an argument."
    exit 1
    ;;
    esac

    done

    # Clear all options and reset the command line
    shift $(( OPTIND -1 ))

    # First parameter
    if [ -z "$1" ]; then
    echo "usage: $0 [-u name] [-p password] file"
    exit
    fi