Posts

Showing posts from 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

[ 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

[ 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

[ 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_descriptio

[ 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(optio

[ 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

[ 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

[ 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.

[ 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

[ 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

[ 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

[ 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 t

[ 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 $ pac

[ 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 instal

[ 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

[ 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

[ 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 Aut

[ 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

[ 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 s

[ Level 2 ] Python with vim

Python with vim: http://wiki.python.org/moin/Vim Wish this helps. regards, Stanley Huang

[ 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 $c

[ 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

[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

[ 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)

[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

[ 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

[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