Wednesday, November 28, 2012

using tunnel in putty

Connect 'ssh' as usual with session.

Right click on top panel of active putty ssh connection terminal, and select 'change settings'.

Under 'tunnel' section, added tunnelling for port 22 and port 5900 (remote vnc desktop over ssh tunnel).

Remote desktop from Linux to office PC using 'vinagre'.

Remote desktop from home ubuntu to office ubuntu with 'vinagre' over 'putty ssh tunnel'.

Scp from Linux to office PC via 'putty ssh tunnel'.

USB shielding issue

floating shield will behave like an antenna that causing EMI concern.

Thursday, November 15, 2012

Curl web service libarary

Talking to 3G USB Sierra Modem Under Linux

[root@GsnCommsModule root]# cat /dev/ttyUSB4 &
[1] 601
[root@GsnCommsModule root]# cat > /dev/ttyUSB4






Monday, November 12, 2012

Monday, September 10, 2012

Hard drive health check

[1] How To Geek
[2] PC Support About

Is your hard drive healthy or need to be replaced in case of losing your precious data?

i would use ddrescue to ghost it to a new hard drive [1].

Monday, July 23, 2012


[1] PAP intro on WiKi
[2] CHAP intro on WiKi


PAP transmits unencrypted ASCII passwords over the network and is therefore considered insecure. It is used as a last resort when the remote server does not support a stronger authentication protocol, like CHAP or EAP (the latter is actually a framework). [1]

CHAP requires that both the client and server know the plaintext of the secret, although it is never sent over the network.[2]

Wednesday, March 21, 2012

C Call C++


From [3]
 // C++ code:
 extern "C" void f(int);
 void f(int i){
  // ...
 /* C code: */
 void f(int);
 void cc(int i){
  /* ... */

// C++ code:
 class C {
  // ...
  virtual double f(int);

 extern "C" double call_C_f(C* p, int i) // wrapper function
  return p->f(i);

 /* C code: */
 double call_C_f(struct C* p, int i);
 void ccc(struct C* p, int i){
  double d = call_C_f(p,i);
  /* ... */

 // C++ code:
 void f(int);
 void f(double);

 extern "C" void f_i(int i) { f(i); }
 extern "C" void f_d(double d) { f(d); }

 /* C code: */
 void f_i(int);
 void f_d(double);

 void cccc(int i,double d){
  /* ... */

From [1]

// This is C++ code
#include "Fred.h"
int main()
  Fred fred;


/* This is C code */
#include "Fred.h"

void c_function(Fred* fred){
   unsigned char lc;

   printf("return of C++ Wilma: %d\n",lc);


// This is C++ code
#include "Fred.h"
#include  /*std::cout<<*/

//Fred::Fred() : a_(0) { }
Fred::Fred() { }

void Fred::wilma(int a) { std::cout<<"wilma(arg) print arg "<        

Fred* cplusplus_callback_function(Fred* fred){
  return fred;

//Provide access of C++ methods from C code.
unsigned char Cplusplus_CallbackFunction_Wilma(int a, Fred* fred){


/* This header can be read by both C and C++ compilers */
 #ifndef FRED_H
 #define FRED_H

 #ifdef __cplusplus
   class Fred {
     void wilma(int);
     int a_;
   typedef struct Fred  Fred;


 #ifdef __cplusplus
 extern "C" {

 #if defined(__STDC__) || defined(__cplusplus)
   extern void c_function(Fred*);   /* ANSI C prototypes */
   extern Fred* cplusplus_callback_function(Fred*);
   extern unsigned char Cplusplus_CallbackFunction_Wilma(int a, Fred* fred);
   extern void c_function();        /* K&R style */
   extern Fred* cplusplus_callback_function();

 #ifdef __cplusplus

 #endif /*FRED_H*/

PROGRAM = $(DESTDIR)/CCallCplusplus
INCL = -I ./
CP = g++
C = gcc
#C = arm-none-linux-gnueabi-g++

OBJS := $(addprefix $(DESTDIR)/,main.o Fred.o c-function.o)
DEBUG = -g


 $(CP) -o $(PROGRAM) $(OBJS) -lpthread

 $(CP) -c $(CFLAGS) $< -o $@ $(INCL)

 $(C) -c $(CFLAGS) $< -o $@ $(INCL)

clean :
 rm -f $(OBJS)
 rm -f $(PROGRAM)


wilma(arg) print arg 123
wilma(arg) print arg 456
return of C++ Wilma: 0

Tuesday, March 6, 2012

Shell exit on Error

[1] A little madness
[2] WiKi

Bash Tip: Exit on Error

Back in my post Your Next Programming Language I mentioned I would post occassional tips about bash scripting. As soon as I started writing my next script, it occured to me: the first thing I always do when writing a new bash script is set the errexit option:

set -e

This option makes your script bail out when it detects an error (a command exiting with a non-zero exit code). Without this option the script will plough on, and mayhem often ensues. In all the noise generated it can be a pain to found the root cause of the problem. So I make it a rule to set this option and fail as early as possible.

Exit on error

Bash can be told to exit immediately if a command fails. From the bash manual ("set -e"):

"Exit immediately if a simple command (see SHELL GRAMMAR above) exits with a non-zero status. The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test in an if statement, part of a && or || list, or if the command's return value is being inverted via !. A trap on ERR, if set, is executed before the shell exits."

To let bash exit on error, different notations can be used:

Specify `bash -e' as shebang interpreter
Start shell script with `bash -e'
Use `set -e' in shell script
Use `set -o errexit' in shell script
Use `trap exit ERR' in shell script

Specify `bash -e' as the shebang interpreter

Sunday, February 26, 2012

How to generate patch under Linux

[1] Linux byexamples

First, how to create patch file?
Patch file is a readable file that created by diff with -c (context output format). It doesn’t matter and if you wanna know more, man diff. To patch the entire folder of source codes(as usually people do)I do as bellow:

Assume Original source code at folder Tb01, and latest source code at folder Tb02. And there have multiple sub directories at Tb01 and Tb02 too.

diff -crB Tb01 Tb02 > Tb02.patch

-c context, -r recursive (multiple levels dir), -B is to ignore Blank Lines.
I put -B because blank lines is really useless for patching, sometimes I need to manually read the patch file to track the changes, without -B is really headache.

How to patch?
First of all, please do a dry-run before really patch it. Bare in mind, patch will be working very specifically. Let say the version 3 Tb03.patch is use to patch from Tb02, if you apply patch on Tb01, sometimes it will corrupt your source code. So, to make sure it works, do a dry run. Dry-run means a fake-test, do it at the directory of the source code targeted to patch.

Doing dry-run like this:

patch --dry-run -p1 -i Tb02.patch

The success output looks like this:

patching file TbApi.cpp
patching file TbApi.h
patching file TbCard.cpp

The failure ouptut looks like this:

patching file TbCard.cpp
Hunk #2 FAILED at 585.
1 out of 2 hunks FAILED -- saving rejects to file TbCard.cpp.rej
patching file TbCard.h
Hunk #1 FAILED at 57.
Hunk #2 FAILED at 77.
Hunk #3 succeeded at 83 with fuzz 1 (offset -21 lines).

At last, if the dry-run is giving good result, do this and enjoy the compilation.

patch -p1 -i Tb02.patch

1. Network Theory Ltd Patch Intro
2. How to create and use a patch in Linux

Sunday, January 8, 2012

Changing MAC address under Linux Command Line

[1] Ref

$ ifconfig -a | grep HWaddr
eth0  Link encap:Ethernet HWaddr 00:80:48:BA:d1:20

# ifconfig eth0 down
# ifconfig eth0 hw ether 00:80:48:BA:d1:30
# ifconfig eth0 up
# ifconfig eth0 |grep HWaddr
In Linux, Windows, Mac OS X, or a different operating system
changing MAC address is only temporary. 
Once you reboot your machine, the operating system reflects the 
physical MAC address burnt in your network card and not the MAC address you set.

Thursday, January 5, 2012

Set Time and Date under Linux


Setting the system clock

To set the system clock under Linux, use the date command. As an example, to set the current time and date to July 31, 11:16pm, type ``date 07312316'' (note that the time is given in 24 hour notation). If you wanted to change the year as well, you could type ``date 073123161998''. To set the seconds as well, type ``date 07312316.30'' or ``date 073123161998.30''. To see what Linux thinks the current local time is, run date with no arguments.

Setting the hardware clock

To set the hardware clock, my favourite way is to set the system clock first, and then set the hardware clock to the current system clock by typing ``/sbin/hwclock --systohc'' (or ``/sbin/hwclock --systohc --utc'' if you are keeping the hardware clock in UTC). To see what the hardware clock is currently set to, run hwclock with no arguments. If the hardware clock is in UTC and you want to see the local equivalent, type ``/sbin/hwclock --utc''