Archive for Tag "bash"

Find And Tar

// find files and tar your archives

find . -type f -name "*.java" | xargs tar rvf myfile.tar

Setup SSH AutoLogin

// autologin with SSH, save as sshsetup.sh

#!/usr/local/bin/bash

function usage()
{
    echo ""
    echo "Authorizes a host for automatic SSH use by sending your key to the remote host ..."
    echo "Usage: $0 remote_host_to_authorize [username:=defaults to current username]"
    echo ""
}

function cleanup()
{
    if [ -f $TEMP_PUB_KEY_XFER ]
    then
        rm $TEMP_PUB_KEY_XFER
    fi
}

function exit_on_error()
{
    cleanup
    exit 1
}

if [ $# -lt 1 -o "$1" = "-h" -o "$1" = "--help" ]
then
    usage
    exit 0
fi

PUB_KEY=~/.ssh/id_dsa.pub
if [ $# -eq 2 ]; then
    USER=$2
else
    USER=`whoami`
fi
HOST_TO_AUTH=$1
TEMP_PUB_KEY_XFER=/tmp/$USER"_TEMP_KEY"

echo "checking for $PUB_KEY ..."
if [ ! -f $PUB_KEY ]; then
    echo "generating your dsa public key (leave passphrase blank and save to $PUB_KEY when prompted) ..."
    ssh-keygen -t dsa
    if [ $? -ne 0 ]; then
        echo "ssh-keygen failed"
        exit_on_error
    fi
fi
echo "OK"

echo "for the following commands you will be asked to supply your password for $HOST_TO_AUTH :"

echo "copying a temp pub key to $HOST_TO_AUTH ..."
cat $PUB_KEY > $TEMP_PUB_KEY_XFER
chmod 700 $TEMP_PUB_KEY_XFER
echo "OK"

remote_key=`basename $TEMP_PUB_KEY_XFER`
scp $TEMP_PUB_KEY_XFER $USER@$HOST_TO_AUTH:~/$remote_key
if [ $? -ne 0 ]; then
    echo "scp failed"
    exit_on_error
fi
    
echo "authorizing $HOST_TO_AUTH for automatic SSH use ..."
ssh $USER@$HOST_TO_AUTH "cat ~/$remote_key >> ~/.ssh/authorized_keys; rm ~/$remote_key"
if [ $? -ne 0 ]; then
    echo "ssh failed"
    exit_on_error
fi
echo "OK"

cleanup
echo "authorization successful!  you can now login automatically to $HOST_TO_AUTH"
exit 0

Convert MP3 to AMR,WAV and AAC

// this script will convert MP3 format to AMR,WAV and AAC

#!/bin/sh

#strip file extension
name=`echo "$1"|cut -d'.' -f1`

#encode to AAC format
ffmpeg -i $1 -ar 22050 -ac 1 -ab 96k  $name.aac

#encode to AMR format
ffmpeg -i $1 -ar 8000 -ac 1 -ab 12.2k  $name.amr

#the last thing is WAV format
ffmpeg -i $1 -ar 8000 -ac 1  $name.wav

Save Last Played Video's on Linux

// after you played the video, you can save the video by searching /tmp for fla files

#!/bin/bash
name=$(/usr/bin/zenity --entry --title="Name your Video" --height=100 --width=300 --text="Please name your video ")
cp /tmp/Fla* ~/Videos/"$name"

Photo Collage using ImageMagick

// here's a simple bash command for creating photo collage using ImageMagick

#rename image to random first
$ for f in `ls *.jpg`; do mv $f $RANDOM.jpg; done

#now make collage
montage '*.jpg' -border 2x2 -background black +polaroid -resize 50% -background LightGray -geometry -50-50 -tile x3 collage.jpg

List All IP Address on Local Area Network

// this command will list all IP address connected on LAN network, require NMAP

nmap -sP 192.168.0.0/24

Netcat Sample

// I usually do this when something is wrong with my HTTP server. Rather than telnetting, it much-much-much shorter

 echo -e "GET / HTTP/1.0\r\n\r\n" | nc 127.0.0.1 80

UNIX Russian Roulette

$ [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live"

Dump HTTP packet Using Tcpdump and Perl

// capture network packet using tcpdump and print in human readable HTTP request and response using perl

#!/usr/bin/perl

use Socket;

$|=1;
open (STDIN,"/usr/sbin/tcpdump -lnx -s 1024 dst port 80 |");
while (<>) {
    if (/^S/) {
        while ($packet=~/(GET|POST|WWW-Authenticate|Authorization).+/g)  {
            $time = localtime;
            $iaddr = inet_aton($client);
            $client_name = gethostbyaddr($iaddr, AF_INET);
            print "[$time] $client ($client_name) -> $host\t$&\n";
        }
        undef $client; undef $host; undef $packet;
        ($client,$host) = /(d+.d+.d+.d+).+ > (d+.d+.d+.d+)/
            if /P d+:d+((d+))/ && $1 > 0;
    }
    next unless $client && $host;
    s/s+//;
    s/([0-9a-fA-F]{2})s?/chr(hex($1))/eg;
    tr/x1F-x7Ern//cd;
    s/0x.?:  //g;
    $packet .= $_;
}

Mount ISO Images

// FreeBSD

mdconfig -a -t vnode -f /path/to/image.iso -u 1
mount -t cd9660 /dev/md1 /mnt/cdrom

#unmount
mount -u /mnt/cdrom
mdconfig -d -u 1

// Ubuntu

#load loop kernel module
sudo modprobe loop
sudo mount debianetch.iso /media/isoimage/ -t iso9660 -o loop

Rotate Nginx Access Logs

#!/bin/sh
mv /var/log/nginx/nginx-access.log /var/log/nginx/nginx-access.log.0
mv /var/log/nginx/nginx-error.log /var/log/nginx/nginx-error.log.0
pid=`cat /var/run/nginx.pid`
kill -USR $pid
gzip --best /data/logs/nginx-access.log.0
gzip --best /data/logs/nginx-error.log.0

PHP FastCGI Server Init Script / Start Script

#!/usr/local/bin/bash

## ABSOLUTE path to the PHP binary
PHPFCGI="/usr/local/bin/php-cgi"

## tcp-port to bind on
FCGIPORT="9000"

## IP to bind on
FCGIADDR="127.0.0.1"

## number of PHP children to spawn
PHP_FCGI_CHILDREN=20

## number of request before php-process will be restarted
PHP_FCGI_MAX_REQUESTS=400

# allowed environment variables sperated by spaces
ALLOWED_ENV="PATH USER"

## if this script is run as root switch to the following user
USERID=nobody

## dunno, but without this, php wouldn't accept request
FCGI_WEB_SERVER_ADDRS="127.0.0.1"

if test x$PHP_FCGI_CHILDREN = x; then
  PHP_FCGI_CHILDREN=5
fi

ALLOWED_ENV="$ALLOWED_ENV PHP_FCGI_CHILDREN"
ALLOWED_ENV="$ALLOWED_ENV PHP_FCGI_MAX_REQUESTS"
ALLOWED_ENV="$ALLOWED_ENV FCGI_WEB_SERVER_ADDRS"

if test x$UID = x0; then
  EX="/usr/bin/su -m $USERID -c \"$PHPFCGI -q -b $FCGIADDR:$FCGIPORT\""
else
  EX="$PHPFCGI -b $FCGIADDR:$FCGIPORT"
fi
echo $EX

# copy the allowed environment variables
E=

for i in $ALLOWED_ENV; do
  E="$E $i=${!i}"
done

# clean environment and set up a new one
nohup env - $E sh -c "$EX" &> /dev/null &

Extract First Image From FLV

ffmpeg -i movie.flv -vcodec mjpeg -vframes 1 -an -f rawvideo -s 320x240 movie.jpg

Shell War Code

// will keep that user out from the shell

while true
do
  kill -9 `ps -aux | grep [u]sername | awk '{ print $2 }'`
  sleep 2
done