星期三, 8月 11, 2010

[Linux] 如何sniffer 送出或是接受的protocol content.

在寫網路程式,常常需要處理protocol的問題,http要看header and content對不對,可是一直用Wireshark sniffer也是很煩,幸好linux可以用很簡單的方式看到~~~^^

mkfifo proxypipe
cat proxypipe | nc -l  11111 | tee -a inflow | nc localhost 11112 | tee -a outflow 1>proxypipe

可以把input放在inflow,把output放在outflow~~~~^^

好用呦~~~^^

來源出自
http://smaftoul.wordpress.com/2009/05/13/netcat-as-a-logging-tcp-proxy/

星期二, 6月 22, 2010

[Linux] 當打錯command的時候怎麼辦....(#rm -rf /)

我個人常常打太快,常常rm的結果就是

#rm -rf /
慘了,電腦就得從灌了
Google了一下,發現真的沒甚麼方式可以recover,但皇天不負苦心人,可以用yum reinstall~~~~^^

#rpm -qa --qf '%{name}\n' | sort -u | xargs -i yum reinstall -y --verbose {}

星期一, 6月 21, 2010

[C++] convert string to int or boolean

http://www.codeguru.com/forum/showthread.php?t=358996
這個link有高手寫出C++ convert string to boolean,code如下

template
bool from_string(T &Value,const std::string &str,std::ios_base & (*f)(std::ios_base&))
// converts string to streamable value, and returns true on success and false otherwise.
{
std::istringstream stream(str);
stream>>f>>Value;
return (!stream.fail()) && stream.get()==std::istringstream::traits_type::eof();
}


如果是convert string to int,可以參考這個link
http://www.codeguru.com/forum/showthread.php?t=231054
code如下

template
bool from_string(T& t,
const std::string& s,
std::ios_base& (*f)(std::ios_base&))
{
std::istringstream iss(s);
return !(iss >> f >> t).fail();
}

如果是C的部分可以用atoi, atof function
但如果是str = "0" 會搞不清楚是fail return 0 還是convert to 0,如果是大於 INT_MAX,也就是 0x7FFFFFFF,就只會回傳 0x7FFFFFFF。

還有strtol functaion,可以參考下面的link
http://linux.die.net/man/3/strtol

至於atoi and strtol的差異就是strtol當有錯誤的時候,會設置errno
但atoi不能,所以可以說strtol是atoi的加強版,請參考下面的link
http://learn.akae.cn/media/ch25s03.html
https://www.securecoding.cert.org/confluence/display/seccode/INT06-C.+Use+strtol%28%29+or+a+related+function+to+convert+a+string+token+to+an+integer
所以建議用strtol

星期一, 4月 12, 2010

星期六, 4月 10, 2010

[Python] python egg 安裝

常常不知道怎麼安裝python的package齁~~~

照做~~~~

http://www.ibm.com/developerworks/cn/linux/l-cppeak3.html

星期四, 4月 01, 2010

[Python] String to JSON轉換必殺技

http://eishn.blog.163.com/blog/static/65231820070181041204/

如果你不想裝JSON module,請服用~~~~^^
json = lambda s, encoding = sys.getdefaultencoding(): repr(unicode(s, encoding))[1:]

PS. 據說最後那行代碼很容易令人摸不著頭腦, 你不妨把json() 換成(string) escape() 來理解。
如果你用escape 來定義這行代碼, 你只能得到escape;
如果你用 json 來命名, 你得到了 JSON。

星期三, 3月 17, 2010

[Linux] time 怎麼用 GNU verison

個人常常使用time來看program跑多久,man time的說明中,有--verbose,還有format,但
# time -v ps aux
這樣都會有顯示
-bash: -v: command not found
real 0m0.001s
user 0m0.000s
sys 0m0.001s

後來才知道原來是bash這個shell中,time的指令沒有GNU版本的功能
所以怎麼跑GNU版本的time呢?
就是..
# /usr/bin/time -v ps aux

水啦~~~~^^

星期五, 1月 15, 2010

[Linux] vim 怎麼用?!

http://blog.vgod.tw/2009/12/08/vim-cheat-sheet-for-programmers/

這位部落客,把Vim常用的部份給圖形化了,這真是太屌了~~~^^
vim的指令(command)不會,上鳥哥太慢~~~^^
有PDF可以下載嚕~~~^^

星期三, 1月 13, 2010

[Linux] About "kernel: TCP: time wait bucket table overflow"

tcp_max_tw_buckets

Maximal number of timewait sockets held by system simultaneously. If this number is exceeded TIME_WAIT socket is immediately destroyed and warning is printed. This limit exists only to prevent simple DoS attacks, you must not lower the limit artificially, but rather increase it (probably, after increasing installed memory), if network conditions require more than default value (180000).

所以當你設小的時候呢,/var/log/messages就會出現一堆"kernel: TCP: time wait bucket table overflow",所以default值千萬別亂改,如果你的程式會有狠大的traffic

怎麼改回來呢?
在/etc/sysctl.conf加這行
net.ipv4.tcp_max_tw_buckets = 180000

還要
echo 180000 > /proc/sys/net/ipv4/tcp_max_tw_buckets

星期二, 12月 29, 2009

[Linux]linux記憶體被吃光

相信很多人遇到用top看明明就沒有程式用記憶體很兇,但是記憶體還是被用光了
怎麼辦呢?
http://blog.chinaunix.net/u/27173/showart_467689.html
http://blog.yesican.tw/?p=912

其實就是linux為了提高disk IO的存取效率,所以有cache pagecache, dentries and inodes
怎麼free呢

#free
#sync
#echo 3 > /proc/sys/vm/drop_caches
#echo 0 > /proc/sys/vm/drop_caches

/proc/sys/vm/drop_caches (since Linux 2.6.16)
Writing to this file causes the kernel to drop clean caches,
dentries and inodes from memory, causing that memory to become
free.

To free pagecache, use echo 1 > /proc/sys/vm/drop_caches; to
free dentries and inodes, use echo 2 > /proc/sys/vm/drop_caches;
to free pagecache, dentries and inodes, use echo 3 >
/proc/sys/vm/drop_caches.

Because this is a non-destructive operation and dirty objects
are not freeable, the user should run sync(8) first.

-----------------------------------------------------------------
Update
http://ssorc.tw/rewrite.php/read-599.html
這篇對於linux memory usage有更詳細的說明
節錄buffer and cache的說明~~~^^

Free中的buffer和cache︰(它們都是佔用內存)︰

  buffer : 作為buffer cache的內存,是塊設備的讀寫緩沖區

  cache: 作為page cache的內存, 文件系統的cache

  如果 cache 的值很大,說明cache住的文件數很多。如果頻繁訪問到的文件都能被cache住,那麼磁盤的讀IO bi會非常小。

Read more: http://ssorc.tw/rewrite.php/read-599.html#ixzz0gbxRjPrV

------------------------------------------------------------------------
[Update]
http://virtualthreads.blogspot.com/2006/02/understanding-memory-usage-on-linux.html
但這一篇就在說明ps很不準,別看ps的memory usage

星期一, 12月 21, 2009

[Linux] logrotate setup and test

如果要利用logrotate幫你rotate log,請在加入一個file在/etc/logrotate.d/下面
# vim /etc/logrotate.d/aaa
content:
/var/log/aaa {
daily
ifempty
rotate 7
}

# service syslog restart

如何知道設定對不對,請打
# logrotate /etc/logrotate.conf

至於有那些可以設定,請man logrotate嚕

[linux] Too many open files

想知道自己某個程式的開啟檔案數,可以用以下的指令察看:

lsof -p [pid of your process] | wc -l

如果想調大檔案開啟數,可以調整fs.file-max這個核心參數。如果memory夠大,可以將此值加大到65536,或是更大:調整的方式:

在/etc/rc.local中加入echo 65536 > /proc/sys/fs/file-max

或是

在sysctl.conf中加入fs.file-max=65536

不過單修改核心參數只能加大系統核心的上限,Linux本身還有針對每個user的每個程式本身的限制,因此還需要編輯/etc/security/limits.conf,並加入

* soft nofile 4096
* hard nofile 4096

修改完畢之後,重新開機即可!

星期五, 12月 11, 2009

[Linux] How to setup reposity on Centos and RHEL.

http://doc.linuxpk.com/58088.html

我寫下簡單的command

1. mkdir /centos.source/5.3/
mkdir /RHEL.source/5.2/
2. centos
15 5 * * * rsync -aqzH --delete msync.centos.org::CentOS/5.3 /cent.source/
RHEL
cp RHEL_source /RHEL.source/5.2/
3. centos
vim /etc/httpd/conf.d/cent.mirror.conf
content:
Alias /CentOS "/cent.source/"

Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all


RHEL
vim /etc/httpd/conf.d/RHEL.mirror.conf
content:
Alias /RHEL "/RHEL.source/"

Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all


4. RHEL
mv /RHEL.source/5.2/Server/repodata /RHEL.source/5.2/Server/backup_repodata
yum install createrepo 或 rpm -ivh createrepo-0.4.4-2.fc6.noarch.rpm
mkdir /RHEL.source/5.2/Server/repodata
cd /RHEL.source/5.2/Server/repodata
createrepo -g /RHEL.source/5.2/Server/backup_repodata/comps-rhel5-server-core.xml /RHEL.source/5.2/Server

5. centos
cd /etc/yum.repos.d/
vim CentOS-Base.repo
content:
[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
baseurl=http://xxxxxxx/CentOS/5.3/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5

RHEL
cd /etc/yum.repos.d/
vim rhel-5.2.repo
content:
[rhel-5.2]
name=Red Hat Enterprise Linux $releasever - $basearch - Debug
baseurl=http://xxxxxxx/RHEL/5.2/Server
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

6. wget http://xxxxxxx/CentOS/5.3/os/$basearch/
wget http://xxxxxxx/RHEL/5.2/Server

Done

星期四, 11月 12, 2009

[Linux] how to separate array in shell script.

通常linux based的系統,shell script中的array都是用空白,或是tab來當分隔符號,但如果要換成別的時候該怎麼辦呢?

在前面加IFS=,
就會是用逗號分隔嚕~~~~^^
例子如下

#!/bin/sh

IFS=,
string="aaa,bbb,ccc"
for str in $string
do
echo $str
done

星期二, 10月 20, 2009

[資訊] VISIO的樣板(template)哪裡找呢?

http://www.visiocafe.com/index.htm

常常做投影片,需要話架構圖,是不是都找不到適合的網路元件圖呢,這裡有~~~~
有DELL, HP等等,而且是免費的,常常來看看吧。

[Linux] shellscript如何用用array在for loop

sources=(`cat test.log`)
destinations=(`cat test.1.log`)

for ((i=0; i<${#sources[@]}; i++))
do
echo $i
#echo ${sources[$i]} ${destinations[$i]}
done

要同時跑兩個array的時候蠻方便的,還有要把cat的資料轉成array就是加括弧嚕。

星期日, 10月 18, 2009

[MSSQL] 變數不能在ALTER語法中

MSSQL的語法不是每一個statement都可以帶變數的
ALTER TABLE, ALTER PARTITION FUNCTION, ALTER PARTITION SCHEME, ALTER PROCEDURE.
都不行,所以必須把statement先組起來,用EXEC

EXEC('ALTER TABLE xxxx' + @test_table_name + ')

真是沒有好好學MSSQL,所以得花超多的時間發現為什麼。

[MSSQL] 如何從partition value找到使用哪一個filegroup

找了老半天,總算找到了,還真是困難。
http://social.msdn.microsoft.com/Forums/en/sqldatabaseengine/thread/0bb0ba4e-fc06-4d8b-a79d-fb275fac3da1

節錄裡面的SQL

@var int
set @var = object_id('dbo.t1')
select main.tabname, main.partition_id, main.partition_number, main.rows, main.name, main.data_space_id,
main.partition_scheme_id, main.partid, main.function_id, part.value, part.partition_id
from
(select object_name(a.object_id) tabname, a.partition_id, a.partition_number, a.rows,
c.name, c.data_space_id, d.partition_scheme_id, d.destination_id as partid, e.function_id
from sys.partitions a inner join sys.allocation_units b on a.hobt_id = b.container_id
inner join sys.data_spaces c on b.data_space_id = c.data_space_id
inner join sys.destination_data_spaces d on c.data_space_id = d.data_space_id
inner join sys.partition_schemes e on d.partition_scheme_id = e.data_space_id
where a.object_id = @var) main
left join
(select a.function_id, b.value, case when a.boundary_value_on_right = 0 then b.boundary_id else b.boundary_id + 1 end partition_id
from sys.partition_functions a inner join sys.partition_range_values b on a.function_id = b.function_id) part
on main.function_id = part.function_id and main.partition_number = part.partition_id
order by main.partid


為了這個我花了三四天吧....希望對於需要的人有幫助。