Friday, June 28, 2013

[Level 1] Synergy -- Multi hosts share the same keyborad/mouse.

There is a software call "Synergy" that could let mutli hosts share the same keyborad/mouse (virtual-KVM).
http://synergy-foss.org
It's funny, try it.

Wish this helps.

regards,
Stanley Huang

Wednesday, June 26, 2013

[ Level 1 ] Assign external python path to program.

Sometimes, you want to assign external python path to program, then you could use "PYTHONPATH" environment variables for this purpose. ex.
$ PYTHONPATH=/tmp/my_python_path python - <<EOF
> import sys
> print sys.path
> EOF
['', '/tmp/my_python_path', '/usr/lib/python2.7', ...]
$
Wish this helps.

regards,

Stanley Huang

Wednesday, May 29, 2013

[ Level 1 ] create cpio file with find.

Cpio is a good tool to pack files in one image, and it's fast.
You could use find command to find files that you want and also use gzip to compress it.
ex.
$ find ./ -print | cpio -o -Hnewc | gzip > /tmp/my.cpio.gz

Wish this helps.

regards,
Stanley Huang

Tuesday, May 28, 2013

[ Level 2 ] Create an egg for Python in Ubuntu.

How to create an egg file for Python.
First of all, you must have setuptools module.
$ sudo apt-get -y install python-setuptools

Now, you could try to create an empty egg now.
$ mkdir /tmp/demo
$ cd /tmp/demo
$ cat &ht; ./setup.py <<EOF
#!/bin/env python
#-*- coding:utf-8 -*-
from setuptools import setup
setup()
EOF
$ python setup.py bdist_egg ## bdist_egg is the option for creating egg.
$ ls -ALb
build  dist  setup.py  UNKNOWN.egg-info
You could find, we have three more directories after you execute setup.py
build ->
dist -> final egg file
UNKNOW.egg-info -> egg info

Now, we could give setuptools more information about egg.
cat > ./setup.py <<EOF
#!/bin/env python
#-*- coding:utf-8 -*-
from setuptools import setup, find_packages
setup(
        name = "my_first_egg",
        version="0.0.1",
        packages = find_packages(),
        zip_safe = False,

        description = "my first egg.",
        long_description = "long description of my first egg.",
        author = "Stanley Huang",
        author_email = "wenlien1001 at gmail.com",

        license = "GPLv2",
        keywords = ("demo", "python", "egg"),
        platforms = "Independant",
        url = "http://stanley-huang.blogspot.tw/",
        )
EOF
And you could create a sub folder named as "name" attribute (my_first_egg) and put your source code into ti.
$ mkdir ./my_first_app
$ touch ./my_first_app/__init__.py
$ cat > ./my_first_app/helloworld.py <<EOF
#!/bin/env python
#-*- coding:utf-8 -*-
print 'Hello World!'
EOF
$ chomd a+x ./my_first_app/helloworld.py
$ python setup.py bdist_egg
$ file ./dist/my_first_egg-0.0.1-py2.7.egg 
./dist/my_first_egg-0.0.1-py2.7.egg: Zip archive data, at least v2.0 to extract
$ unzip -l ./dist/my_first_egg-0.0.1-py2.7.egg
Archive:  ./dist/my_first_egg-0.0.1-py2.7.egg
  Length      Date    Time    Name
---------  ---------- -----   ----
        1  2013-05-28 08:40   EGG-INFO/dependency_links.txt
      239  2013-05-28 08:40   EGG-INFO/SOURCES.txt
        1  2013-05-28 08:40   EGG-INFO/not-zip-safe
       13  2013-05-28 08:40   EGG-INFO/top_level.txt
      295  2013-05-28 08:40   EGG-INFO/PKG-INFO
       38  2013-05-28 08:40   my_first_egg/helloworld.py
        0  2013-05-28 08:39   my_first_egg/__init__.py
      164  2013-05-28 08:40   my_first_egg/helloworld.pyc
      140  2013-05-28 08:40   my_first_egg/__init__.pyc
---------                     -------
      891                     9 files

Usually we put source file in src folder, therefore, you may want to change finding direcotory.
You could change the setting in setup.py
packages=find_packages('src'),
package_dir = {'':'src'}
You could try it!
Wish this helps.

regards,
Stanley Huang

Wednesday, May 22, 2013

[ Level 2 ] Fix duplicate vg/lv name.

1. scan vgs:
$ vgscan
2. get vg information:
$ vgdisplay
  --- Volume group ---
  VG Name               vg00
...
  VG UUID               FUoK9F-9mgs-jO4M-vub1-Y7zs-6wpQ-uM2S11
  --- Volume group ---
  VG Name               vg00
...
  VG UUID               FUoK9F-9mgs-jO4M-vub1-Y7zs-6wpQ-uM2S12
3. export vg
$ vgexport
4. rename vg name
$ vgrename FUoK9F-9mgs-jO4M-vub1-Y7zs-6wpQ-uM2S12 vg01
5. import vg
$ vgimport vg01
6. get vg information:
$ vgdisplay
  --- Volume group ---
  VG Name               vg00
...
  VG UUID               FUoK9F-9mgs-jO4M-vub1-Y7zs-6wpQ-uM2S11
  --- Volume group ---
  VG Name               vg01
...
  VG UUID               FUoK9F-9mgs-jO4M-vub1-Y7zs-6wpQ-uM2S12
7. get lv information
$ lvscan
  ACTIVE            '/dev/vg00/lv00' [13.99 GiB] inherit
  inactive            '/dev/vg01/lv00' [13.99 GiB] inherit
8. active vg01
$ vgchange --ignorelockingfailure --noudevsync --sysinit -ay vg01 >/dev/null 2>&1 &&
9. rename lv name(optional) 
$ lvrename vg01 lv00 lv01
10. get lv information
$ lvscan
  ACTIVE            '/dev/vg00/lv00' [13.99 GiB] inherit
  ACTIVE            '/dev/vg01/lv01' [13.99 GiB] inherit
Wish this helps.

regards,
Stanley Huang

Monday, May 6, 2013

[ Level 2 ] Get last argument in bash.

You could use the following methods to get the last argument that you pass to script.
#!/bin/bash
getLastArg1() {
  for last
  do
    true
  done
  echo $last
}

getLastArg2() {
  echo $@ | awk '{print($NF)}'
}

getLastArg3() {
  eval echo `echo \\$${#@}`
}

getLastArg1 $@
getLastArg2 $@
getLastArg3 $@
$ ./test.sh a b c
c
c
c
$

Wish this helps.

regards,
Stanley Huang

Sunday, May 5, 2013

[ Level 3 ] Test your Python code.

Ref:
https://python-guide.readthedocs.org/en/latest/writing/tests.html

Mock:
http://www.voidspace.org.uk/python/mock/getting-started.html#

Wish this helps.

regards,
Stanley Huang

[ Level 2 ] Tips of unittest for Python.

There are tips for Python:
1. Use mock for replace method.
ex.
myMock=mox.Mox()
myMock.StubOutWithMock(myModule, 'replaceMethod')
myModule.replaceMethod(input).AndReturn('Hello World!')
myMock.ReplayAll()
expected = 'Hello World!'
self.assertEqual(module.replaceMethod(input), expected)
myMock.VerifyAll()  #verify if all mocks be executed.
myMock.UnsetStubs() #release all mocks

2. Use -m to test one method only.
ex.
$ python -m unittest myApp.TestClass.testMethod

Wish this helps.

regards,
Stanley Huang

[ Level 2 ] Coverage in Python.

$ sudo pip install coverage
$ coverage run myApp.py arg1 arg2...
$ coverage report -m
$ coverage html
$ coverage help (run) ## or coverage run --help

Commands of coverage:
    run – Run a Python program and collect execution data.
    report – Report coverage results.
    html – Produce annotated HTML listings with coverage results.
    xml – Produce an XML report with coverage results.
    annotate – Annotate source files with coverage results.
    erase – Erase previously collected coverage data.
    combine – Combine together a number of data files.
    debug – Get diagnostic information.

Ref:
http://nedbatchelder.com/code/coverage/
http://nedbatchelder.com/code/coverage/cmd.html#cmd

Wish this helps.

regards,
Stanley Huang

Saturday, May 4, 2013

[ Level 1 ] The popular MySQL GUI client tools

There are the popular MySQL GUI client tools:
. Aqua Data Studio. http://www.aquafold.com/aquadatastudio_downloads.html
. CLIENT FOR MYSQL BY ENGINSITE. http://www.enginsite.com/Download.htm
. dbForge Studio. http://www.devart.com/dbforge/mysql/studio/ 
. DBTools Manager. http://www.dbtools.com.br/EN/dbmanagerpro/ 
. DbVisualizer. http://www.dbvis.com/
. Dreamcoder for MySQL. http://www.sqldeveloper.net/database-tools/mysql/overview.html
. HeidiSQL. http://www.heidisql.com/ 
. Navicat. http://www.navicat.com/en/products/navicat_mysql/mysql_overview.html
. MyDB Studio. http://www.mydb-studio.com 
. phpMyAdmin. http://www.phpmyadmin.net/home_page/
. Sequel Pro. http://www.sequelpro.com/
. SQL Examiner Suite. http://www.sqlaccessories.com/SQL_Examiner_Suite/
. SQL Maestro MySQL Tools Family. http://www.sqlmaestro.com/products/mysql/ 
. SQLWave. http://www.nerocode.com/ 
. SQLyog. https://www.webyog.com/
. SQuirreL. http://squirrel-sql.sourceforge.net/
. Toad for MySQL. http://www.quest.com/toad-for-mysql/
. Workbench. http://dev.mysql.com/downloads/workbench/
. xBaseView. http://www.xbaseview.com/download.htm
Ref:
http://www.databasejournal.com/features/mysql/article.php/3880961/Top-10-MySQL-GUI-Tools.htm
http://stackoverflow.com/questions/9185/what-is-the-best-mysql-client-application-for-windows
http://www.sqldeveloper.net/database-tools/mysql/overview.html
http://www.freshdesignweb.com/10top-mysql-client-interface-gui.html

Wish this helps.

regards,
Stanley Huang

[ Level 3 ] How to build MySQL client tool - mysql

First, you have to download MySQL source and use the following command to build it.
$ ./configure --without-server --enable-thread-safe-client 
  --with-client-ldflags=-all-static --prefix=/usr/local/mysql 
   --with-machine-type=powerpc --with-zlib-dir=/usr/local/zlib 
    --without-debug  --without-docs --with-big-tables 
If you don't want to build it, there is a way to get it. (The sample is for Ubuntu only.)
$ apt-get install libmysqlclient15-dev

Wish this helps.

regards,
Stanley Huang

Wednesday, April 24, 2013

[ Level 1 ] A good editor, Sublime.

Here is a power editor called "Sublime", please follow the link to get more information.

http://www.sublimetext.com/

Some useful settings:
1. enable vim.
In Perferences -> Settings-Default, empty the value of ignored_packages,
e.g. "ignored_packages": [],
Default ignore the package Vintage, the vim pakcage.

2. default with command mode.
Add one line in Perferences -> Settings-User:
"vintage_start_in_command_mode": true,
The default mode of vintage would be "command mode"

3. tab for 4 spaces.
In Perferences -> Settings-Default, modify the value of tab_size with 4,
e.g. "tab_size": 4,
And also modify the value of translate_tabs_to_spaces with true,
e.g. "translate_tabs_to_spaces": true,

4. auto-trim tailing spaces.
Add one line in Perferences -> Settings-User:
"trim_trailing_white_space_on_save": true,

Wish this helps.

regards,
Stanley Huang

Saturday, April 20, 2013

[ Level 3 ] The script for creating virtualbox envrionment (Ubuntu 12.04) with lauch new OS image.

In my company, I need to test it when I build a new image.
Therefore I have to setup my testing environment for integration test.

I wrote a script to automatically setup the environment from new install Ubuntu.
The steps are:
1. Download and setup tftp server.
2. Download and setup dhcp server with netboot option.
3. Install virtualbox.

After that you could create a VM then choose netboot. (the first interface must connect to tap0 to get boot server information)

#!/bin/bash

######## utilities ########
_getTasks() {
  indent="$1"
  cat $0 | grep '()' | grep -v "^_" | grep "^[^ ]" | cut -d'(' -f1 | sed -e "s/^/$indent/"
}

_showUsage() {
  indent="  "
  cat <<EOF
Usage:
  $0 task
Ex.
  $0 `_getTasks | head -1`
tasks:
${indent}[ all ]
`_getTasks "$indent"`
EOF
}

_die() {
  echo "$1"
  exit ${2:-1}
}

_checkID() {
  uid=`id | cut -d'(' -f1 | cut -d'=' -f2`
  [ $uid -ne 0 ] && _die "Need root to execute this command, exit!"
}
######## utilities ########

######## create tftpd configuration ########
TFTP_DIRECTORY=/tftpboot

installTftpd() {
  apt-get -y remove tftpd ## conflict with tftpd-hpa
  apt-get -y install tftpd-hpa tftp
}
#installTftpd

createTftpdConf() {
  echo "Run createTftpdConf()"
  cat > /etc/default/tftpd-hpa <<EOF
TFTP_USERNAME="tftp"
TFTP_DIRECTORY="$TFTP_DIRECTORY"
TFTP_ADDRESS="0.0.0.0:69"
TFTP_OPTIONS="-c --secure"
EOF
}
#createTftpdConf
######## create tftpd configuration ########

######## create /tftpboot content ########
installSyslinux() {
  apt-get -y install syslinux syslinux-common
}

testTftp() {
  test_filename="$1"
  cd /tmp
  rm -f ./$test_filename
  tftp localhost <<EOF
get /$test_filename
EOF
  [ ! ./$test_filename ] && echo "Get file ($test_filename) from tftp server fail..." && exit 1
  rm -f ./$test_filename
}

createTftpbootContent() {
  echo "Run createTftpbootContent()"
  mkdir -p $TFTP_DIRECTORY
  cp /usr/lib/syslinux/pxelinux.0 $TFTP_DIRECTORY
  cp /usr/lib/syslinux/*menu.c32 $TFTP_DIRECTORY
  mkdir -p $TFTP_DIRECTORY/pxelinux.cfg && chmod 777 $TFTP_DIRECTORY/pxelinux.cfg
  mkdir -p $TFTP_DIRECTORY/boot && chmod 777 $TFTP_DIRECTORY/boot 
  cat > $TFTP_DIRECTORY/pxelinux.cfg/default <<EOF
prompt 0
default default_install
timeout 3

label default_install
  kernel boot/vmlinuz-2.6.32.24
  append initrd=boot/default-installer.img
EOF
  chown -R tftp:tftp $TFTP_DIRECTORY
  testTftp "pxelinux.0"
}
#createTftpbootContent

######## create /tftpboot content ########

######## install user-mod linux package ########
installUserModPackages() {
  echo "Run installUserModPackages()"
  apt-get -y install uml-utilities
}
#installUserModPackages

_getRealUserName() {
  tty_filter="`tty | sed -e 's|/dev/||'`"
  who | grep "$tty_filter" | awk '{print($1)}'
}

createTap() {
  echo "Run createTap()"
  user_name="`_getRealUserName`"
  subnet=10.10
  netmask=255.255.255.0
  TIMEOUT=5

  interface_file="/etc/network/interfaces"
  interface_orig="$interface_file.orig"
  [ ! -f $interface_orig ] && cp $interface_file $interface_orig
  if [ -f $interface_orig ]
  then
    read -t $TIMEOUT -p "Do you want to use orig interfaces file ($interface_orig) to be the template? (Timeout for $TIMEOUT seconds)) [Y/n]: " Yn
    [ "$Yn" != "n" ] && [ "$Yn" != "N" ] && cp $interface_orig $interface_file
  fi

  for index in `seq 0 1`
  do
  cat >> $interface_file <<EOF

auto tap$index
iface tap$index inet static
pre-up tunctl -t tap$index -u $user_name
up ifconfig tap$index up
down ifconfig tap$index down
address $subnet.$index.254
netmask $netmask
EOF
  done

  echo "New interfaces file:"
  echo "================================================"
  cat $interface_file
  echo "================================================"
}
#createTap
######## install user-mod linux package ########

######## create dhcpd.conf ########
installDhcpd() {
  apt-get -y install isc-dhcp-server ## isc-dhcp-server replaced dhcp3-server
}

createDhcpdConf() {
  echo "Run createDhcpdConf()"
  subnet=10.10 ## for tap0/tap1
  #subnet=192.168 ## for virtualbox vboxnet0
  netmask=255.255.255.0
  TIMEOUT=5

  dhcpd_conf_file="/etc/dhcp/dhcpd.conf"
  dhcpd_conf_orig="$dhcpd_conf_file.orig"
  [ ! -f $dhcpd_conf_orig ] && cp $dhcpd_conf_file $dhcpd_conf_orig
  if [ -f $dhcpd_conf_orig ]
  then
    read -t $TIMEOUT -p "Do you want to use orig dhcpd file ($dhcpd_conf_file) to be the template? (Timeout for $TIMEOUT seconds)) [Y/n]: " Yn
    [ "$Yn" != "n" ] && [ "$Yn" != "N" ] && cp $dhcpd_conf_orig $dhcpd_conf_file
  fi

  for index in `seq 0 1` ## for tap0/tap1
  #for index in `seq 56 56` ## for virtualbox vboxnet0
  do
  cat >> $dhcpd_conf_file <<EOF

subnet $subnet.$index.0 netmask $netmask {
   option subnet-mask $netmask;
   option broadcast-address $subnet.$index.255;
   option routers $subnet.$index.254;
   range $subnet.$index.210 $subnet.$index.220;
 
   filename "/pxelinux.0";
}
EOF
  done
  echo "New dhcpd config file:"
  echo "================================================"
  cat $dhcpd_conf_file
  echo "================================================"
}
#createDhcpdConf

checkDhcpService() {
  netstat -tlunp | grep dhcp || _die "Cannot find binding address of dhcp!"
}
#checkDhcpService

######## install virtualbox ########
installVBox() {
  echo "Run installVBox()"

  ## Url for virtualbox 4.2.12 to download
  vbox_binary_url_4_2_12_for_12_10="http://download.virtualbox.org/virtualbox/4.2.12/virtualbox-4.2_4.2.12-84980~Ubuntu~quantal_amd64.deb"
  ## Url for virtualbox extension pack 4.2.12 to download
  vbox_extpack_url_4_2_12="http://dlc.sun.com.edgesuite.net/virtualbox/4.2.12/Oracle_VM_VirtualBox_Extension_Pack-4.2.12.vbox-extpack "

  ## download virtualbox and install it
  vbox_binary_url=$vbox_binary_url_4_2_12_for_12_10
  vbox_binary_filename=${vbox_binary_url##*/}
  wget $vbox_binary_url
  dpkg -i ./$vbox_binary_filename

  ## setup vbox driver
  apt-get -y install linux-headers-3.5.0-27 linux-headers-3.5.0-27-generic
  /etc/init.d/vboxdrv setup

  ## download virtualbox extension and install it
  vbox_extpack_url=$vbox_extpack_url_4_2_12
  vbox_extpack_filename=${vbox_extpack_url##*/}
  wget $vbox_extpack_url
  vboxmanage extpack install ./$vbox_extpack_filename
}
#installVBox
######## install virtualbox ########

######## main ########
_checkID

## Start install vm
echo "Start install vm..."
if [ -z "$1" ]
then
  _showUsage
elif [ "$1" == "all" ]
then
  task_list="$@"
else
  task_list="$1"
fi

for task in $task_list
do
  eval $task
done
## Install done
echo "Install vm done! Please restart the machine~"


Wish this helps.

regards,
Stanley Huang

Friday, April 19, 2013

[ Level 3 ] The script for setup the Ubuntu for Juniper VPN.

There is an paper told us how to setup Ubuntu 12.04 for Juniper VPN.
Then I follow the guide and also write a script for installation/setup.
The steps as the following:
1. Download ans install ia32-libs to support 32 bit libraries.
2. Download and install x64 JRE, jre-7u21-linux-x64.tar.gz.
3. Create plugin link for firefox/google chromium.
4. Update alternative Java.
5. Download and install i586 JRE, jre-7u21-linux-i586.tar.gz.

After that, you could login to your VPN.

#!/bin/bash
## url link for 64 bit JRE.
jre_x64_source_url=http://javadl.sun.com/webapps/download/AutoDL?BundleId=76853
## url link for 32 bit JRE
jre_i586_source_url=http://javadl.sun.com/webapps/download/AutoDL?BundleId=76851

die() {
  echo "$1"
  exit ${2:-1}
}

checkRootID() {
  uid=`id | cut -d'(' -f1 | cut -d'=' -f2`
  [ $uid -ne 0 ] && die "Need root to execute this command, exit!"
}
checkRootID
 
pek2c() {
  bDebug && read -p "Press enter key to continue..." x
}

syncFS() {
  sync;sync;sync
  sleep 3
}

changeOwner() {
  chown -R root:root $java_dir
}

installJREx64() {
  ## install jre x64
  jre_x64_filename=jre_x64.tar.gz
  rm -f $exec_at_dir/${jre_x64_source_url##*/}
  cd $exec_at_dir
  wget $jre_x64_source_url
  mv $exec_at_dir/${jre_x64_source_url##*/} $java_dir/$jre_x64_filename
  cd $java_dir
  tar zxf $java_dir/$jre_x64_filename
  jre_x64_version=`tar ztvf $java_dir/$jre_x64_filename | head -1 | awk '{print($NF)}' | sed -e s'|/$||'`
  syncFS
  pek2c
  update-alternatives --install /usr/bin/java java /usr/java/$jre_x64_version/bin/java 1 
  update-alternatives --set java /usr/java/$jre_x64_version/bin/java
  pek2c
  cd /usr/lib/mozilla/plugins && rm -f ./libnpjp2.so ./libjavaplugin.so ./libjavaplugin_oji.so && ln -sf /usr/java/$jre_x64_version/lib/amd64/libnpjp2.so ./
  cd /usr/lib/chromium-browser/plugins && ln -sf /usr/java/$jre_x64_version/lib/amd64/libnpjp2.so ./
  pek2c
  rm $java_dir/$jre_x64_filename
}

installJREi586() {
  ## install jre i586
  jre_i586_filename=jre_i586.tar.gz
  rm -f $exec_at_dir/${jre_i586_source_url##*/}
  cd $exec_at_dir
  wget $jre_i586_source_url
  mv $exec_at_dir/${jre_i586_source_url##*/} $java_dir/$jre_i586_filename
  cd $java_dir
  tar zxf $java_dir/$jre_i586_filename
  syncFS
  pek2c
  rm $java_dir/$jre_i586_filename
}

######## main ########

[ "$1" == "-d" ] && set -vx && bDebug=true

## global vars, prepare environment
exec_at_dir="`pwd`" ## where to execute this command.
cd $exec_at_dir/./`dirname $0`
run_at_dir="`pwd`" ## where should this script run at.
java_dir="/usr/java"
mkdir -p $java_dir
apt-get install -y ia32-libs

## install JRE x86 first
installJREx64

## install JRE x586 second
installJREi586

## change owner
changeOwner




Wish this helps.

regards,
Stanley Huang

Wednesday, April 17, 2013

[ Level 1 ] How to remove rc listed in dpkg -l @Ubuntu.

You could use dpkg -P to remove it.

e.g.
$ dpkg -l virtualbox
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                               Version                Architecture           Description
+++-==================================-======================-======================-=========================================================================
ii  virtualbox                         4.1.18-dfsg-1ubuntu1.1 amd64                  x86 virtualization solution - base binaries

$ apt-get remove virtualbox
...

$ dpkg -l | grep virtual
...
rc  virtualbox                                4.1.18-dfsg-1ubuntu1.1                    amd64        x86 virtualization solution - base binaries
rc  virtualbox-qt                             4.1.18-dfsg-1ubuntu1.1                    amd64        x86 virtualization solution - Qt based user interface

$ package_name="virtualbox" dpkg -l | grep ^rc | grep "$package_name" | cut -d' ' -f3| sudo xargs dpkg -P
...

$ package_name="" dpkg -l | grep ^rc | grep "$package_name" | cut -d' ' -f3| sudo xargs dpkg -P ## clean all unused rc(s).

Wish this helps.

regards,
Stanley Huang

Tuesday, April 16, 2013

[ Level 2 ] Ubuntu package install script.

I wrote an easy script for install Ubuntu packages.
#!/bin/bash

#
# This script is for install popular packages
#
#$: check package, if exists, skip the following prefix #^
#@: apt-get install packages
#!: unix command
#^@: apt-get install packages, depends on previous #$
#^!: unix command, depends on previous #$
#

showUsage() {
  cat <<EOF
Usage:
  $0
  $0 [package name]
Ex.
  $0
  $0 rdesktop
EOF
}

isPackageNotInstall() {
  dpkg -l $1 >/dev/null 2>&1
  if [ $? -eq 0 ]
  then
    echo "Package '$1' exists, skip it!"
    return 1
  else
    return 0
  fi
}

getContent() {
  declare -i begin
  declare -i end
  declare -i size
  begin=$1
  end=$2
  size=$end-$begin+1
  tail -n +$begin $0 | head -$size
}

getPackageName() {
  echo $1 | cut -d' ' -f2- | cut -d'#' -f1
}

getCommand() {
  echo "$line" | cut -d' ' -f2- | cut -d'#' -f1
}

die() {
  echo $1
  exit 1
}

aptGetInstall() {
  echo -n "Start install package '$@'..."
  apt-get -y install $@ && echo "OK" || die "FAIL"
}

commandExecute() {
  echo -n "Start execute command '$@'..."
  $@ && echo "OK" || die "FAIL"
}


########## main ##########
if [ ! -z "$@" ]
then
  apt-get -y install $@
else
  start_from_here_string="Package List, Start from here!"
  stop_at_here_string="Package List, Stop at here!"
  start_line_no=`grep -n "$start_from_here_string" "$0" | tail -1 | cut -d: -f1`
  stop_line_no=`grep -n "$stop_at_here_string" "$0" | tail -1 | cut -d: -f1`
  SKIP_FLAG=false
  CHECK_FLAG=false
  getContent $start_line_no $stop_line_no | while read line
  do
    if echo "$line" | grep -e "^#\\$" >/dev/null 2>&1
    then
      CHECK_FLAG=true 
      if isPackageNotInstall `getPackageName "$line"`
      then
        echo "Package (`getPackageName '$line'`) doesn't exist!"
        SKIP_FLAG=false
      else
        SKIP_FLAG=true
      fi
      continue
    elif echo "$line" | grep -e "^#^" >/dev/null 2>&1
    then
      $SKIP_FLAG && echo "Dependy exists, skip `getPackageName "$line"`" && continue
      line=`echo "\$line" | sed -e 's/^#^/#/'`
    else
      CHECK_FLAG=false
    fi

    echo $line | grep -e "^#@" >/dev/null 2>&1 && isPackageNotInstall `getPackageName "$line"` && aptGetInstall `getPackageName "$line"` 
    echo $line | grep -e "^#!" >/dev/null 2>&1 && commandExecute `getCommand "$line"`
  done
  exit
  
  cat $0 | grep ^#@ | awk '{print($2)}' | while read sPkg
  do
    echo "pkg install $sPkg"
  done
fi

exit
exit

##
## Package List, Start from here!
##

#### app ####
#@ vlc
#@ testdisk
#@ nfs-common
#@ openssh-server
#@ samba

#### system ####
#@ htop
##@ scim scim-pinyin scim-chewing scim-tables-zh scim-qtimm im-switch
##! im-switch -s scim

#### network ####
#@ chromium-browser
##@ openvpn
#@ wireshark
#@ transmission
#@ rdesktop

#### media ####
#@ gimp
#@ avidemux
#@ audacity
#@ freemind
#@ flashplugin-nonfree
##! sudo cp /usr/lib/flashplugin-installer/libflashplayer.so /usr/lib/chromium-browser/plugins
#@ mimms # download mms streaming

#### tools ####
#@ meld
#@ vim
#@ tree
##@ adacontrol # ptree
#@ minicom

#### developer tools ####
#@ maven2
#@ visualvm
#@ libssl-dev

#### install acrobat reader ####
#@ ia32-libs wget
##! wget http://ardownload.adobe.com/pub/adobe/reader/unix/9.x/9.1.1/enu/AdbeRdr9.1.1-1_i486linux_enu.bin
##! chmod +x AdbeRdr9.1.1-1_i486linux_enu.bin
##! ./AdbeRdr9.1.1-1_i486linux_enu.bin

#$ grub-customizer
#^! add-apt-repository ppa:danielrichter2007/grub-customizer
#^! apt-get update
#^@ grub-customizer

##
## Package List, Stop at here!
##

Wish this helps.

regards,
Stanley Huang

[ Level 2 ] Recovery disk partition utility in for Ubuntu.

There is a tool for Ubuntu could help you to recover disk partition, called "testdisk'.

$ sudo apt-get -y install testdisk
$ sudo testdisk
and choose:
Create -> [select disk] -> Intel -> Analyse -> Quick Search -> Write
And done~

Wish this helps.

regards,
Stanley Huang

Friday, April 12, 2013

[ Level 2 ] Enable USB 3.0 in Ubuntu 12.04

After I upgrade 12.04, the USB 3.0 port seems not work.
Then I google the solution:
add "blacklist uas" in the file /etc/modprobe.d/blacklist.conf

Wish this helps.

regards,
Stanley Huang

Thursday, March 28, 2013

[ Level 1 ] Install Indicator-VirtualBox in Ubuntu

You could install indicator-virtualbox to launch virtualbox guest OS.
https://launchpad.net/indicator-virtualbox/
http://indicator-virtualbox.en.uptodown.com/ubuntu

Wish this helps.

regards,
Stanley Huang

[Level 2] Allow remote display in Ubuntu 12.04/10.04

If you want to allow remote display in Ubuntu 12.04, you have do the following steps:
1. remove "-nolisten tcp" from /etc/X11/xinit/xserverrc
#[before]
$ cat /etc/X11/xinit/xserverrc
exec /usr/bin/X -nolisten tcp "$@"

#[after]
$ cat /etc/X11/xinit/xserverrc
exec /usr/bin/X "$@"

2. add "xserver-allow-tcp=true" into /etc/lightdm/lightdm.conf
#[before]
[SeatDefaults]
user-session=ubuntu
greeter-session=unity-greeter

#[after]
[SeatDefaults]
user-session=ubuntu
greeter-session=unity-greeter
xserver-allow-tcp=true

3. restart X

4. allow remote x connection
local> xhost +

5. ssh remote server and set DISPLAY
local> ssh remote_server
remote> export DISPLAY=local_ip:0.0
remote> xclock
in 10.04, you have to change above step 2 as the following:
2-10.04: add "DisallowTCP=false" in "security" section.
#[before]
$ cat /etc/gdm/custom.conf 
[daemon]
TimedLoginEnable=false
AutomaticLoginEnable=true
TimedLogin=stanley
AutomaticLogin=stanley
TimedLoginDelay=30
DefaultSession=gnome

#[after]
$ cat /etc/gdm/custom.conf 
[daemon]
TimedLoginEnable=false
AutomaticLoginEnable=true
TimedLogin=stanley
AutomaticLogin=stanley
TimedLoginDelay=30
DefaultSession=gnome

[security]
DisallowTCP=false


Wish this helps.

regards,
Stanley Huang

Tuesday, March 26, 2013

[ Level 2 ] Allow VirtualBox guest OS to create symbolic link file in host shared folder.

Allow VirtualBox guest OS to create symbolic link file in host shared folder:
$ VBoxManage setextradata "VM Name" VBoxInternal2/SharedFoldersEnableSymlinksCreate[absolution path for shared folder] 1
$ VBoxManage setextradata "VM Name" VBoxInternal2/SharedFoldersEnableSymlinksCreate/[shared name] 1
Ex.
$ VBoxManage setextradata "MyVM" VBoxInternal2/SharedFoldersEnableSymlinksCreate/home/stanley/shared_folder 1
$ VBoxManage setextradata "MyVM" VBoxInternal2/SharedFoldersEnableSymlinksCreate/shared_name 1

And you could verify it by command:
VBoxManage getextradata "MyVM" enumerate

After setup, you have to shutdown VM (not restart) and boot up it again.

Wish this helps.

regards,
Stanley Huang

Monday, March 18, 2013

[ Level 1 ] Ubuntu tools for Mac OS.

Here are some useful tools for Mac OS.
$ apt-cache search hfs
hfsplus - Tools to access HFS+ formatted volumes
hfsutils - Tools for reading and writing Macintosh volumes
libhfsp-dev - Library to access HFS+ formatted volumes
libhfsp0 - Shared library to access HFS+ formatted volumes
squashfs-tools - Tool to create and append to squashfs filesystems
squashfs-tools-dbg - Tool to create and append to squashfs filesystems (debug)
dmg2img - Tool for converting compress dmg files to hfsplus images
hfsprogs - mkfs and fsck for HFS and HFS+ file systems
hfsutils-tcltk - Tcl/Tk interfaces for reading and writing Macintosh volumes
libhfstdc++6-4.6-dbg-armel-cross - GNU Standard C++ Library v3 (debugging files)
libhfstdc++6-armel-cross - GNU Standard C++ Library v3 (hard float ABI)
rdiff-backup-fs - Fuse filesystem for accessing rdiff-backup archives
$ sudo apt-get -y install hfsplus hfsutils hfsprogs
$ sudo apt-get -y install dmg2img
Other packages for hfs:
$ apt-cache search squashfs
squashfs-tools - Tool to create and append to squashfs filesystems
squashfs-tools-dbg - Tool to create and append to squashfs filesystems (debug)

Wish this helps.

regards,
Stanley Huang

Saturday, March 16, 2013

[ Level 2 ] Python with vim

Python with vim:
http://wiki.python.org/moin/Vim
Wish this helps.

regards,
Stanley Huang

Thursday, March 14, 2013

[ Level 1 ] How ansi color in terminal by Python script.

Sample for show ansi color in terminal by Python script.
#!/bin/env python
import os
print os.popen('echo "\033[31m\033[43mHello World!\033[0m"').read()
print '\033[31m\033[43m' + 'Hello World!' + '\033[0m'

Wish this helps.

regards,
Stanley Huang

[ Level 2 ] Connect pool for ssh.

If you want to use the concept "connection pool" for ssh, you could try the following sample.
#!/bin/bash
#
# You might need to modify sshd_config with the following attributes:
#
# MaxStartup 200
# MaxSessions 200
#
[ "$1" == "-d" ] && set -vx && shift
control_master="$1" && [ "$control_master" != "false" ] && control_master=true
username="$2" && [ -z $username ] && username=stanley
remote_host="$3" && [ -z $remote_host ] && remote_host=127.0.0.1
cmd1="$4"&& [ -z $cmd1 ] && cmd1=ls
control_master_option=""
client_cmd="/tmp/a.sh"

buildControlMaster() {
  if $control_master
  then
    ssh -N -o "ControlMaster auto" -o "ControlPath ~/.ssh/master-$username@$remote_host" $username@$remote_host &
  fi
}

createClientCmd() {
  mkdir -p /tmp/add
  rm /tmp/add/* $client_cmd
  if $control_master
  then
    control_master_option="-o \"ControlMaster auto\" -o \"ControlPath ~/.ssh/master-$username@$remote_host\""
  fi
  echo "ssh $control_master_option $username@$remote_host $cmd1 >/tmp/add/\$\$.txt 2>&1" >> $client_cmd
}

test() {
for i in `seq 1 100`
do
  bash $client_cmd &
done
wait ## wait for all child process finished!
}

buildControlMaster
createClientCmd
time test

$ ./sshControlMaster.sh true ## if open fail, ssh would create isolate session.
channel 67: open failed: administratively prohibited: open failed
channel 69: open failed: administratively prohibited: open failed
channel 86: open failed: administratively prohibited: open failed
channel 88: open failed: administratively prohibited: open failed
channel 72: open failed: administratively prohibited: open failed
channel 18: open failed: administratively prohibited: open failed
...
channel 37: open failed: administratively prohibited: open failed
channel 1: open failed: administratively prohibited: open failed
channel 5: open failed: administratively prohibited: open failed
channel 10: open failed: administratively prohibited: open failed
channel 6: open failed: administratively prohibited: open failed
channel 11: open failed: administratively prohibited: open failed

real 0m5.176s
user 0m0.424s
sys 0m0.232s
$ ./sshControlMaster.sh false ## always create isolate session.

real 0m8.926s
user 0m0.544s
sys 0m0.288s
$ 

You will still get the performance enhancement even though some open session fail and create isolate one.

Wish this helps.

regards,
Stanley Huang

Monday, March 11, 2013

[ Level 1 ] Customize your vim environment.

You could customize your vim environment by this tools.
http://yoursachet.com
MEMO:
1. comment "filetype plugin indent on" and "autoindent" to avoid auto indent mess up the indent during past "Python" script code.
2. add, :vmap <C-C>"+y, to enable Ctrl-C to copy text into clipborad.
2-1. $ vim --version | grep xterm_clipboard
2-2. $ sudo apt-get -y install vim-gtk
2-3. $ sudo update-alternatives --config vim
3. add the following settings to set different tab definition with different extension name,
autocmd FileType py :setlocal sw=4 ts=4 sts=4
autocmd FileType sh :setlocal sw=2 ts=2 sts=2



Wish this helps.

regards,
Stanley Huang

Saturday, March 9, 2013

[Level 3] Network Simulator

Memo it:
1. openWNS: www.openwns.org/Wiki
2. ns-3: www.nsnam.org, ns-3 provides python API to use.

Wish this helps.

regards,
Stanley Huang

[ Level 1 ] How to get vim all commands and argument list.

Use command:
:help :all
:help argument-list

Wish this helps.

regards,
Stanley Huang

[ Level 2 ] How to edit/display mulit-files in one terminal.

You could use serval ways to do that.
1. :tabe => tab edit. You use tabn/tabN/tabn[#] to change tab
2. :vs => virtical split. Back to command mode (press ESC key) then you use ctrl-w to change window
3. :sp => horizontal split. Back to command mode (press ESC key) then you use ctrl-w to change window

To get more help, use commands:
:help :tabe
:help :vs
:help :sp

Wish this helps.

regards,
Stanley Huang

Tuesday, February 19, 2013

[ Level 3 ] Bash script for color ansi.

#!/bin/bash

## Get ansi color code
## Usage:
##   getAnsiColorCode (f)orward_ground/(b)ack_ground color_name
## ex.
##   getAnsiColorCode f/b black/red/green/yellow/blue/magenta/cyan(purple)/white/default
getAnsiColorCode() {
  sReturnCode=""
  fb='f'
  color='default'

  if [ $# -eq 1 ]
  then
    color="$1"
  elif [ $# -gt 1 ]
  then
    fb="$1"
    color="$2"
  fi

  if [ "$fb" == "b" ]
  then
    sReturnCode='4'
  else
    sReturnCode='3'
  fi

  case $color in
  black)
    sReturnCode="${sReturnCode}0"
    ;;
  red)
    sReturnCode="${sReturnCode}1"
    ;;
  green)
    sReturnCode="${sReturnCode}2"
    ;;
  yellow)
    sReturnCode="${sReturnCode}3"
    ;;
  blue)
    sReturnCode="${sReturnCode}4"
    ;;
  magenta)
    sReturnCode="${sReturnCode}5"
    ;;
  cyan|purple)
    sReturnCode="${sReturnCode}6"
    ;;
  white)
    sReturnCode="${sReturnCode}7"
    ;;
  *)
    sReturnCode="${sReturnCode}9"
    ;;
  esac
  echo ${sReturnCode}m
}

## color echo message
## Usage:
##   cecho fg_color bg_color
## ex.
##   cecho black red message
cecho() {
  fgColor='default'
  bgColor='default'
  message=''
  if [ $# -ne 0 ]
  then
    if [ $# -eq 1 ]
    then
      echo $1
    else
      if [ $# -eq 2 ]
      then
        fgColor=$1
        message="$2"
      else
        fgColor=$1
        bgColor=$2
        message="$3"
      fi
      fgCode=`getAnsiColorCode f $fgColor`
      bgCode=`getAnsiColorCode b $bgColor`
      echo -en "\033[$fgCode\033[$bgCode$message\033[0m";
    fi
  fi
}

## status echo message
## Usage:
##   secho OK/FAIL message
## ex.
##   secho OK/FAIL message
secho() {
  status='OK'
  message=''
  if [ $# -eq 1 ]
  then
    message="$1"
  else
    status="$1"
    message="$2"
  fi

  echo -n "$message ... ["
  if [ "$1" == "OK" ]
  then
    cecho green OK
  elif [ "$1" == "FAIL" ]
  then
    cecho red FAIL
  else
    cecho yellow "$1"
  fi
  echo "]"
}

secho OK "Run $0 is"
$ ./testColor2.sh 
Run ./testColor2.sh is ... [OK]

Wish this helps.

regards,
Stanley Huang

Saturday, January 26, 2013

[Level 3] How to setup Juniper VPN in Ubuntu 64 bit environment.

A good sample to demo how to setup Juniper VPN in Ubuntu 64 bit environment.
http://wireless.siu.edu/install-ubuntu-64.htm

Wish this helps.

regards,
Stanley Huang

Tuesday, January 8, 2013

[ Level 3 ] Python MRO.

Python MRO introduction. http://docs.python-guide.org/en/latest/writing/style/
Wish this helps.

regards,
Stanley Huang

[ Level 3 ] Python coding style.

Python coding style: http://docs.python-guide.org/en/latest/writing/style/
Wish this helps.

regards,
Stanley Huang

[Level 2] Implement with syntax in Python.

#!/bin/env python
class myOpen(object):
    def __init__(self, filename, readwrite):
        self.__filename = filename
        self.__readwrite = readwrite
        self.__fd = None

    def __enter__(self):
        self.__fd = open(self.__filename, self.__readwrite)
        return self.__fd

    def __exit__(self, *args):
        self.__fd.close()

with myOpen('/tmp/my.txt', 'r') as f:
    print f.readlines()

with myOpen('/tmp/hello.txt', 'w') as f:
    f.write('hello world!\n')
$ ./test.py 
['-rwxr--r-- 1 stanley stanley   846 2012-12-26 13:07 ./c.py\n', '-rwxr--r-- 1 stanley stanley   486 2013-01-08 17:54 ./test.py\n']
$ cat ./my.txt 
-rwxr--r-- 1 stanley stanley   846 2012-12-26 13:07 ./c.py
-rwxr--r-- 1 stanley stanley   486 2013-01-08 17:54 ./test.py
$ cat ./hello.txt 
hello world!


Wish this helps.

regards,
Stanley Huang

Monday, January 7, 2013

[Level 3] Advanced vim settings

There is a good sample for vim settings: http://amix.dk/vim/vimrc.html
Wish this helps.

regards,
Stanley Huang