2013-09-19

How to unlock encrypted file system on Linux automatically


  1. create a keyfile with random content
  2. $ sudo su -
    $ dd if=/dev/random of=/root/keyfile bs=1024 count=4
  3. Add the file to LUKS
  4. $ cryptsetyp luksAddKey /dev/sdb1 /root/keyfile
  5. Edit /etc/crypttab
  6. $ sdb1_crypt /dev/sdb1 /root/keyfile luks
  7. Edit /etc/fstab
  8. /dev/mapper/sdb1_crypt /media/Repository ext4 defaults 0 2
  9. Reboot or Remount

2013-08-27

Ternary operator(?:) in bash

Ternary operator is a short form of if/then/else
case $a in
    1) b=$c ;;
    *) b=$d ;;
esac

[[ $a=1 ]] && b=$c || b=$d
# At this case, 3rd part can be executed in case 2nd part fails

[[ $a=1 ]] && { b=$c; true; } || b=$d
# to avoid executing 3rd part by accident when 2nd part fails
## semicolon(;) is needed in {}

2013-08-23

Count number of visitors(IP addresses) with IIS log files

* Instruction
  1. Mount IIS log directory into Linux box
    $ sudo mount -t cifs -o ro,username=USER,password=PASSWD //HOST_IP/SHARE /MOUNT_POINT
  2. Run
    #!/bin/bash
    
    site_num=(1 2 7 8)
    site_addr=("AAA.com" "BBB.com" "CCC.com" "DDD.com")
    echo "SITE_NAME DATE WEEK TRAFFIC DB_CONN VISITOR"
    for ix in ${!site_num[*]}
    do
      d=${site_num[$ix]}
      for f in $(ls W3SVC$d)
      do
        if [[ ${f:4:6} -ge $(date -d 'last monday' +"%y%m%d") && ${f:4:6} -lt $(date +"%y%m%d") ]]
        then
          visitors=$(cat W3SVC$d/$f | grep ^[0-9] | awk '{if(!match($9, /^192.168./) && !match($9, /^10.10./)) print $9}' | sort | uniq | wc -l)
          sendbytes=$(cat W3SVC$d/$f | grep ^[0-9] | awk '{print $16}' | awk '{total=total+$1}END{printf "%.f", total}')
          dbconns=$(cat W3SVC$d/$f | awk '{if(match($5,/^\/$/) || match($5,/asp/)) print $9}' | wc -l)
          weeknum=$(date -dyesterday +%V)
          [ $(date -d yesterday +%u) -eq 1 ] && {mon=$(date -d yesterday +%m.%d); true} || mon=$(date -d last-monday +%m.%d)
          [ $(date -d yesterday +%u) -eq 7 ] && {sun=$(date -d yesterday +%m.%d); true} || sun=$(date -d sunday +%m.%d)
          printf "%s 20%s-%s-%s %s(%s-%s) %s %s %s\n" ${site_addr[$ix]} ${f:4:2} ${f:6:2} ${f:8:2} $weeknum %mon %sun $sendbytes $dbconns $visitors
        fi
      done
    done
    
  3. * Sample result
    SITE_NAME DATE WEEK TRAFFIC DB_CONN VISITOR
    AAA.com 2013-08-25 34(08.19-08.25) 12329500782 54908 7335
    BBB.com 2013-08-25 34(08.19-08.25) 13074318914 66700 6442
    CCC.com 2013-08-25 34(08.19-08.25) 23734108053 140609 12877
    DDD.com 2013-08-25 34(08.19-08.25) 8913154535 58417 5172
    

How to run NginX/PHP/MySQL on Mac


Needed to run DRUPAL on Mac
  1. Install XCode
  2. Install XCode Command Line Tools
  3. XCode > Preferences > Downloads > Command Line Tools
  4. Install Homebrew - Similar to MacPorts but better
  5. $ ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
    $ brew doctor
    $ brew tap josegonzalez/homebrew-php
    $ brew tap homebrew/dupes
  6. Install MySQL
  7. $ brew install mysql
    $ unset TMPDIR
    $ mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
    $ sudo mv /usr/local/opt/mysql/my-new.cnf /usr/local/opt/mysql/my.cnf
    $ cp `brew --prefix mysql`/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
    $ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
  8. Install NginX
  9. $ brew install nginx
    $ sudo cp `brew --prefix nginx`/homebrew.mxcl.nginx.plist /Library/LaunchDaemons/
    $ sudo sed -i -e 's/`whoami`/root/g' `brew --prefix nginx`/homebrew.mxcl.nginx.plist
    $ sudo mkdir /var/log/nginx/
  10. Install PHP
  11. $ brew install --without-apache --with-fpm --with-mysql php54
    $ sudo cp `brew --prefix php54`/homebrew-php.josegonzalez.php54.plist  /Library/LaunchAgents/
    $ sudo launchctl load -w /Library/LaunchAgents/homebrew-php.josegonzalez.php54.plist

2013-08-09

How to extract MP3 audio from ASF in Linux environment

COMMAND :
for files in $(ls); do avconv -i $(files) -vn -acodec wmav2 -f asf ${files:0:2}".mp3"; done

If FILES are
01.asf
02.asf
03.asf

2013-06-29

Manage multiple SSH private keys

There are 2 ways to manage multiple SSH private keys.

Step 0. Create a file named ~/.ssh/config

Step 1-1st, SSH can be configured with both host names and their identity files.

Host server1.remote.host
IdentityFile ~/.ssh/server1.remote.host.privatekey
Host server2.remote.host
IdentityFile ~/.ssh/server2.remote.host.privatekey

Step 1-2nd, SSH can be configured with a per-user configuration file.

$ vi config
Type the followings...

IdentityFie ~/.ssh/id_dsa_%h_%r
IdentityFie ~/.ssh/id_rsa_%h_%r
IdentityFie ~/.ssh/id_dsa
IdentityFie ~/.ssh/id_rsa

Save and quit.

$ssh username@remote.host

=> SSH client will try to read the private key at one of the following files
~/.ssh/id_dsa_remote.host_username
~/.ssh/id_rsa_remote.host_username
~/.ssh/id_dsa
~/.ssh/id_rsa

2013-01-18

How to setup virtual directory with PHP support on Nginx

Setup virtual directory with PHP support.
location /phpmyadmin {
    alias /opt/phpmyadmin/;
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

When to use mutliple virtual directories, seperate and include the configuration.
Create /etc/nginx/conf.d/php.inc
location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

Include above file into the virtual directory configuration.
location /phpmyadmin {
    alias /opt/phpmyadmin/;
    include /etc/nginx/conf.d/php.inc;
}

LikeApache - htaccess on Nginx

Nginx does not use .htaccess file for the performance issue.

See details at LikeApache-htaccess