2007年7月19日 星期四

這段code 的意思看不懂

int
stunRand()
{
// return 32 bits of random stuff
assert( sizeof(int) == 4 );
static bool init=false;
if ( !init )
{
init = true;

UInt64 tick;

#if defined(WIN32)
volatile unsigned int lowtick=0,hightick=0;
__asm
{
rdtsc
mov lowtick, eax
mov hightick, edx
}
tick = hightick;
tick <<= 32;
tick |= lowtick;
#else
asm("rdtsc" : "=A" (tick));
#endif
int seed = int(tick);
#ifdef WIN32
srand(seed);
#else
srandom(seed);
#endif
}

#ifdef WIN32
assert( RAND_MAX == 0x7fff );
int r1 = rand();
int r2 = rand();

int ret = (r1<<16) + r2;

return ret;
#else
return random();
#endif
}

尤其是

asm("rdtsc" : "=A" (tick));
int seed = int(tick);

不太懂

2007年7月18日 星期三

想要在 bash 裡面把ㄧ個 group 的資料註解掉該怎麼做

想要在 bash 裡面把ㄧ個 group 的資料註解掉該怎麼做

範例
true << bt_encry
This is comments, Hello, Hello....
bt_encry

another format as following example.
: << COMMENTBLOCK
echo "This line will not echo."
This is a comment line missing the "#" prefix.
This is another comment line missing the "#" prefix.
&*@!!++=
The above line will cause no error message,
because the Bash interpreter will ignore it.
COMMENTBLOCK

Reference the following technique which was called "Here Document".
Example 17-10. Commenting out a block of code

1 #!/bin/bash
2 # commentblock.sh
3
4 : << COMMENTBLOCK
5 echo "This line will not echo."
6 This is a comment line missing the "#" prefix.
7 This is another comment line missing the "#" prefix.
8
9 &*@!!++=
10 The above line will cause no error message,
11 because the Bash interpreter will ignore it.
12 COMMENTBLOCK
13
14 echo "Exit value of above \"COMMENTBLOCK\" is $?." # 0
15 # No error shown.
16
17
18 # The above technique also comes in useful for commenting out
19 #+ a block of working code for debugging purposes.
20 # This saves having to put a "#" at the beginning of each line,
21 #+ then having to go back and delete each "#" later.
22
23 : << DEBUGXXX
24 for file in *
25 do
26 cat "$file"
27 done
28 DEBUGXXX
29
30 exit 0

英文簡寫意義

i.e. == id est 那就是;即
e.g. == exempli gratia 例如

2007年7月17日 星期二

買房子要注意的四大準則

1. 慎選地段良好
2. 景觀優美
3. 產品規劃在水準以上
4. 具備口碑的建商所蓋的房子

選屋四大原則︰地段、景觀、規劃、口碑 缺一不可

位於良好地段與景觀的房子,除了住起來便利、舒適外,房屋價值更有隨時間而歷久彌堅的特色,可說在先天上便立於不敗之地

這也是美國地產大亨唐納‧川普與香港地產鉅子李嘉誠念茲在茲的「地段決定論」。

http://www.housenews.com.tw/subjectDetail.do?operation=subjectDetail&values.subjectNo=23B13B188B62EA75BD15

2007年7月15日 星期日

Linux/BSD terminal 突然不聽使喚的時候(按Enter, 不會出現new line)

Question: Unknown terminal type ?

> name a few) complain that Terminal type eterm is an unknown terminal.

in bash:

export TERM=vt100

or in csh:

setenv TERM vt100

若在pytty下,vi 無法秀出完整的全物頁面,修改
Configuration > Terminal > Keyboard > The Function keys and keypads ==> VT100+

當在 Linux 機器的系統當中,忘記打export TERM=vt100時,
導致系統發生segmentation fault時,可以打下列指令救回來

# export TERM=vt100


當螢幕不聽使喚的時候
[root@localhost AAA]# [root@localhost AAA]# [root@localhost AAA]
# stty echo

ubuntu 利用 putty 執行 vi 會看到一堆亂碼

ubuntu 利用 putty 執行 vi 會看到一堆亂碼, 原因是因為ubuntu 預設支援顯示中文訊息,
採用 UTF-8 編碼, 所以 putty 預設的編碼會導致顯示的訊息為亂碼,
Change Settings -> Window -> Translation -> Receive data assumed to be in which character set 改成 UTF-8 (預設是use font encoding) 即可

另外也可以在 shell 裡執行
export LANG=C

How to disable the bell ring of putty?
Set the following option
Putty Configuration->Terminal->Bell->Set the style of bell->Action to happen when a bell occurs->None(Bell Disabled) (Originally was Make default system alert sound)

2007年7月12日 星期四

Turn 使用上的注意事項

sendRequest Method 不能不間斷連續執行兩次, 容易會有卡住的情形發生

2007年7月10日 星期二

mount windows directory In the Linux shell

mount windows directory In the Linux shell

mount -t smbfs -o username=sophia,password=,debug=4 //192.168.17.178/Upload /mnt/sophia

2007年7月3日 星期二

socket programming 關於 binary file 的 debug 技巧

編輯 binary file
# heme dataindi
(heme 是一套在Linux 上面的binary 編輯程式)

如下
00000000: 01 15 00 58 00 00 00 00 00 00 00 00 74 8A 8D 6F 00 00 00 00 00 0F 00 04 ...X........t..o........
00000018: 72 C6 4B C6 00 13 00 40 00 01 00 2C 55 9A EC 54 44 34 16 08 B4 45 4F 10 r.K....@...,U..TD4...EO.
00000030: 12 00 00 00 00 01 15 00 58 00 00 00 00 00 00 00 00 74 8A 8D 6F 00 00 00 ........X........t..o...
00000048: 00 00 0F 00 04 00 00 00 18 72 C6 4B C6 00 13 00 40 00 01 00 2C 55 9A EC .........r.K....@...,U..
00000060: 54 44 34 16 08 B4 45 4F D4 E0 00 04 TD4...EO....


傳送指定的 binary 檔案
# cat dataindi | ./netcat -u 127.0.0.1 1111 -v -v -x

port scanning for specified target
# ./netcat -x -v -w 2 -z -t target-host 1-1024 ==> scan port 1 to 1024 for the target-host

netcat as server to response huge data
$ cat backup.iso | pv -b | nc -l 3333


Netcat examples
http://www.g-loaded.eu/2006/11/06/netcat-a-couple-of-useful-examples/

Netcat – a couple of useful examples

November 6th, 2006 by George Notaras

One of the Linux command line tools I had initially under-estimated is netcat or just nc. By default, netcat creates a TCP socket either in listening mode (server socket) or a socket that is used in order to connect to a server (client mode). Actually, netcat does not care whether the socket is meant to be a server or a client. All it does is to take the data from stdin and transfer it to the other end across the network.

The simplest example of its usage is to create a server-client chat system. Although this is a very primitive way to chat, it shows how netcat works. In the following examples it is assumed that the machine that creates the listening socket (server) has the 192.168.0.1 IP address. So, create the chat server on this machine and set it to listen to 3333 TCP port:

$ nc -l 3333

On the other end, connect to the server with the following:

$ nc 192.168.0.1 3333

In this case, the keyboard acts as the stdin. Anything you type in the server machine’s terminal is transfered to the client machine and vice-versa.

Transfering Files

In the very same way it can be used to transfer files between two computers. You can create a server that serves the file with the following:

$ cat backup.iso | nc -l 3333

Receive backup.iso on the client machine with the following:

$ nc 192.168.0.1 3333 > backup.iso

As you may have noticed, netcat does not show any info about the progress of the data transfer. This is inconvenient when dealing with large files. In such cases, a pipe-monitoring utility like pv can be used to show a progress indicator. For example, the following shows the total amount of data that has been transfered in real-time on the server side:

$ cat backup.iso | pv -b | nc -l 3333

Of course, the same can be implemented on the client side by piping netcat’s output through pv:

$ nc 192.168.0.1 3333 | pv -b > backup.iso
Other Examples

Netcat is extremely useful for creating a partition image and sending it to a remote machine on-the-fly:

$ dd if=/dev/hdb5 | gzip -9 | nc -l 3333

On the remote machine, connect to the server and receive the partition image with the following command:

$ nc 192.168.0.1 3333 | pv -b > myhdb5partition.img.gz

This might not be as classy as the partition backups using partimage, but it is efficient.

Another useful thing is to compress the critical files on the server machine with tar and have them pulled by a remote machine:

$ tar -czf - /etc/ | nc -l 3333

As you can see, there is a dash in the tar options instead of a filename. This is because tar’s output needs to be passed to netcat.

On the remote machine, the backup is pulled in the same way as before:

$ nc 192.168.0.1 3333 | pv -b > mybackup.tar.gz

Security

It is obvious that using netcat in the way described above, the data travels in the clear across the network. This is acceptable in case of a local network, but, in case of transfers across the internet, then it would be a wise choice to do it through an SSH tunnel.

Using an SSH tunnel has two advantages:

  1. The data is transfered inside an encrypted tunnel, so it is well-protected.
  2. You do not need to keep any open ports in the firewall configuration of the machine that will act as the server, as the connections will take place through SSH.

You pipe the file to a listening socket on the server machine in the same way as before. It is assumed that an SSH server runs on this machine too.

$ cat backup.iso | nc -l 3333

On the client machine connect to the listening socket through an SSH tunnel:

$ ssh -f -L 23333:127.0.0.1:3333 me@192.168.0.1 sleep 10; \
nc 127.0.0.1 23333 | pv -b > backup.iso

This way of creating and using the SSH tunnel has the advantage that the tunnel is automagically closed after file transfer finishes. For more information and explanation about it please read my article about auto-closing SSH tunnels.

Telnet-like Usage

Netcat can be used in order to talk to servers like telnet does. For example, in order to get the definition of the word “server” from the “WordNet” database at the dict.org dictionary server, I’d do:

$ nc dict.org 2628
220 ..............some WELCOME.....
DEFINE wn server
150 1 definitions retrieved
151 "server" wn "WordNet (r) 2.0"
server
n 1: a person whose occupation is to serve at table (as in a
restaurant) [syn: {waiter}]
2: (court games) the player who serves to start a point
3: (computer science) a computer that provides client stations
with access to files and printers as shared resources to a
computer network [syn: {host}]
4: utensil used in serving food or drink
.
250 ok [d/m/c = 1/0/18; 0.000r 0.000u 0.000s]
QUIT
221 bye [d/m/c = 0/0/0; 16.000r 0.000u 0.000s]

Works as a Port Scanner too

A useful command line flag is -z. When it is used, netcat does not initiate a connection to the server, but just informs about the open port it has found. Also, instead of a single port, it can accept a port-range to scan. For example:

$ nc -z 192.168.0.1 80-90
Connection to 192.168.0.1 80 port [tcp/http] succeeded!

In this example, netcat scanned the 80-90 range of ports and reported that port 80 is open on the remote machine.

The man page contains some more interesting examples, so take the time to read it.

Notes

All the above examples have been performed on Fedora 5/6. Netcat syntax may vary slightly among Linux distributions, so read the man page carefully.

Netcat provides a primitive way to transfer data between two networked computers. I wouldn’t say it’s an absolutely necessary tool in the everyday use, but there are times that this primitive functionality is very useful.