Tuesday, September 29, 2009

Shell script 練習

看到Ols3前輩出的練習題,故嘗試寫看看,題目是這個樣子的:
# 把 以下資料檔 d1.txt d2.txt 予以整合
d1.txt
====================
abc:12
bob:25
cdc:36
OK:121
Tomy:99
Jack:108

d2.txt
====================
Jack:9
cdc:8
OK:11
Tomy:22
bob:5
abc:88

試寫一 script 來處理 d1.txt d2.txt 使其變成

abc:12:88
bob:25:5
cdc:36:8
Jack:108:9
OK:121:11
Tomy:99:22

My script:
#!/bin/bash

for VAR1 in `cat ./d1.txt | sort`
do
for VAR2 in `cat ./d2.txt | sort`
do
if [ `echo $VAR1 | cut -d: -f1` = `echo $VAR2 | cut -d: -f1` ]; then
echo $VAR1\:`echo $VAR2 | cut -d: -f2`
fi
done
done

References:

Thursday, September 24, 2009

Powerful web application and network load testing tool: curl-loader

找到了一個還蠻強大的web application與network load testing的工具: curl-loader
curl-loader是based on Linux並號稱可以提供類似SpirentAvalanche與IXIA loader的功能(IXIA用過,但是IXIA loader我可是沒用過 XD),同時間透過curl-loader可以產生上萬筆的clients來產生實際的HTTP或是FTP的流量,重點是透過它的configure file,curl-loader會自動的幫你bind IP,真是貼心的設計呀,使用之前最好是依照它的建議更改些Linux預設的參數,當然如果要產生big real flow那就要使用高檔點的機器囉,帽客實際上跑了一下,發現到當產生30K HTTP clients時,會將dual core(Intel C2D E4500)的CPU resource全部吃光光!

建議調整的參數:
ulimit -n 100000
echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse
echo 100000 > /proc/sys/fs/file-max
echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/ethX/rp_filter
echo "109568 109568 109568" > /proc/sys/net/ipv4/tcp_mem

Monday, September 21, 2009

Wednesday, September 16, 2009

Linux上常見的Load Balancer Project

常見的有:
Linux Virtual Server
Ultra Monkey
Red Hat Cluster Suite
High-Availability Linux

References:

Friday, September 11, 2009

OSX 10.5.x 重大更新通知

OSX剛剛提醒我目前有更新囉,這次的更新還不小,有iTunes, QuickTime, Java for MAC與Security update。

Wednesday, September 09, 2009

只印出本次登入所輸入的命令

Linux系統預設上可透過history(bash內建的指令)看到以前與本次登入所輸入的所有命令,而當使用者登出系統後系統會自動將本次登入時所輸入的所有命令新增到 ~/.bash_history中,至於最多紀錄多少筆則要取決於HISTSIZE/HISTFILESIZE設定為多少(兩者預設應該都是1000),所以排除舊的命令歷史記錄,新的紀錄應該會從1001開始,於是乎可以利用以下簡單的方法列印出目前登入所輸入的所有指令:

# history | while read cmd; do set -- $cmd; if [ $1 -gt 1000 ]; then echo "$1:$2";fi; done
1001:history
1002:set
1003:history
1004:ifconfig
1005:history

Note:
1. 預設上HISTFILESIZE是設定~/.bash_history可紀錄多少筆,預設是1000,假設今天設定成2000則代表~/.bash_history可存放2000筆記錄,於是乎上面的指令就必需改成這樣:
# history | while read cmd; do set -- $cmd; if [ $1 -gt 2000 ]; then echo "$1:$2";fi; done

2.若~/.bash_history未滿1000筆時,那就要特別注意判斷的條件了。

Wednesday, August 26, 2009

HTTP Server Benchmark Test Tool: WBox

WBox aims to help you having fun while testing HTTP related stuff. You can use it to perform many tasks, including the following:

- Benchmarking how much time it takes to generate content for your web application.
- Web server and web application stressing.
- Testing virtual domains configuration without the need to alter your local resolver.
- Check if your redirects are working correctly emitting the right HTTP code.
- Test if the HTTP compression is working and if it is actually serving pages faster.
- Use it as a configuration-less HTTP server to share files! (see the server mode documentation at the end of the Wbox tutorial in this page, but it's as simple as % wbox servermode webroot /tmp)

Wbox is free software under the GPL version 2 license and was written in ANSI C (POSIX runtime required) by Salvatore 'antirez' Sanfilippo

使用過後,個人感覺比ab強大好用!

Thursday, July 30, 2009

Deny SSH brute attack via iptables

iptables rules:
#iptables -A INPUT -p tcp --dport 22 -i ethx -m state --state NEW -m recent --set
#iptables -A INPUT -p tcp --dport 22 -i ethx -m state --state NEW -m recent --update --seconds 60 --hitcount 5 -j DROP
如此一來,本機於1分鐘內收到超過5個以上的SSH連線需求時,第5個SSH connection就會被drop掉.

緊接著用hping測試連續送出n個TCP port 22 SYN,看看第5個SSH connection是否會被drop:


Reference:
http://www.cyberciti.biz/tips/linux-unix-bsd-openssh-server-best-practices.html
http://tuxtraining.com/2009/06/23/stop-ssh-brute-force-attack-using-susefirewall

Thursday, July 23, 2009

Suspend Linux to swap

分享一個在console下將Linux系統休眠到swap的方法:

Tested on CentOS5.3:
Step1. 設定STD(suspend to disk)到swap:
[root@localhost ~]# fdisk -l
Disk /dev/hda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1          13      104391   83  Linux
/dev/hda2              14         842     6658942+  83  Linux
/dev/hda3             843         907      522112+  82  Linux swap / Solaris
/dev/hda4             908        1044     1100452+   5  Extended
/dev/hda5             908         972      522081   83  Linux
[root@localhost ~]# ls -ld /dev/hda3
brw-r----- 1 root disk 3, 3 Jul 23  2009 /dev/hda3

由此得知swap partition的major與minor ID是3.3,接著修改參數如下 (預設是0:0):
[root@localhost power]# echo "3:3" > /sys/power/resume 

Step2. 啟動休眠:
[root@localhost ~]# echo "disk" > /sys/power/state 

此時系統將立刻進入休眠狀態,並於下次重開機時回到剛剛的環境。

Step3. 設定永久suspend到swap:
修改/boot/grub/grub.conf並設定Kernel參數如下:
kernel /vmlinuz-2.6.18-128.2.1.el5 ro root=LABEL=/ rhgb quiet resume=/dev/hda3

Tuesday, July 14, 2009

善用tmpfs當Firefox的cache

現在的電腦記憶體很夠用, Redhat/CentOS預設又將/dev/shm當成tmpfs掛載起來, 所以也就別辜負了它的美意吧! 把它拿來當成Firefox cache的RAMDisk用吧!

至於如何將Firefox的cache指定到/dev/shm呢? 實作上非常簡單, 只要開啟Firefox並於網址列上輸入about:config, 然後點擊滑鼠右鍵->新增->字串後輸入: browser.cache.disk.parent_directory, 內容則輸入/dev/shm就完成了.


成功的話, 當使用Firefox瀏覽網頁時就會看到/dev/shm會新增Cache目錄.
當然系統重新開機後, cache就會消失了!
Note: Redhat/CentOS預設並不特別指定/dev/shm的大小為何, 記憶體剩下多少就允許/dev/shm使用, 如果想要改變此行為的話可開啟/dev/fstab,找到mount /dev/shm的那一列並修改如下:
tmpfs /dev/shm tmpfs defaults,size=100M 0 0
以上希望對大家使用Linux有幫助.

Thursday, July 02, 2009

Compress and encrypt directory in Linux

直接進入實作步驟~
Compress tools: tar
Data encrypt tools: AES Crypt (http://www.aescrypt.com/)

Objective-1, To backup /etc and encrypt it:
1. 壓縮並加密/etc:
# tar cvf - /etc | aescrypt -e -p 123456 -> etc.tar.aes
(-e: encrypt, -p password)

2. 測試etc.tar.aes是否有加密:
[root@localhost ~]# file etc.tar.aes
etc.tar.aes: data
[root@localhost ~]# tar tvf etc.tar.aes
tar: This does not look like a tar archive
tar: Skipping to next header
tar: Archive contains obsolescent base-64 headers
tar: Read 300 bytes from etc.tar.aes
tar: Error exit delayed from previous errors
測試結果發現到etc.tar.aes已經被AES加密了,故無法直接以tar觀看壓縮檔的內容。

Objective-2 To decrypt and uncompress data:
1. 將壓縮檔解密:
# aescrypt -d -p 123456 etc.tgz.aes 
解密後會新增一檔案為etc.tar
2. 解壓縮:
# tar xvf etc.tar
如此一來便可解開加密過後的壓縮檔了。

其它在Linux上常見的加密工具還有openssl與gpg。

Monday, June 29, 2009

20個Linux系統/網路偵測的好工具

發現到一篇介紹20個Linux系統/網路偵測工具的好網頁,挺多我沒有使用過的,所以特別收錄起來,FYI。

Sunday, June 21, 2009

Free online file conversion: Zamzar

很多人為了轉換檔案格式,總是會在電腦上裝載各種轉換的軟體,甚至有時收到了不能讀取的檔案時就更是令人感到心煩了,比如Office 2007的docx,像我的Linux與OSX都沒辦法讀取docx的文件,偏偏有人就是很喜歡寄給我但也總不能去請人家去轉換好再寄給我吧,此時只好自己開啟Windows的office 2007來看,挺浪費時間,嘿嘿~不過今天真是個好日子,讓我發現了一個能解決我這種困擾的服務,Zamzar!

Zamzar它是一個免費的線上格式轉換的服務,不論您使用那種OS(事實上跟OS一點關係都沒有),只要將檔案透過Web browser上傳後,選擇想轉換成的檔案格式,它就會幫你將檔案轉換好並透過mail告訴你檔案下載的連結,真的是非常之方便呀!目前檔案的上限為100MB,而能轉換的格式真是包山包海呀!


Wednesday, June 10, 2009

[Shell script] Remote Services watchdog

以下的script是針對偵測到對方的service沒有正常運作時則發mail給所指定的使用者,請參考:

#!/bin/bash

SRVLIST='192.168.0.100:80
192.168.0.100:53'
MAILACCOUNT="root@server1.example.com"

for SRV in `echo $SRVLIST`
do
 IP=`echo $SRV | cut -d: -f1`
 PORT=`echo $SRV | cut -d: -f2`
 nc -w 1 $IP $PORT > /dev/null 2>&1 || echo "Port $PORT on Server $IP is failed" | mail -s "Error: Service failed" $MAILACCOUNT
done

Monday, June 08, 2009

Install AWstats on CentOS 5.x

What's AWstats?
AWStats is a free powerful and featureful tool that generates advanced web, streaming, ftp or mail server statistics, graphically. This log analyzer works as a CGI or from command line and shows you all possible information your log contains, in few graphical web pages. It uses a partial information file to be able to process large log files, often and quickly. It can analyze log files from all major server tools like Apache log files (NCSA combined/XLF/ELF log format or common/CLF log format), WebStar, IIS (W3C log format) and a lot of other web, proxy, wap, streaming servers, mail servers and some ftp servers.
http://awstats.sourceforge.net/

安裝步驟:
1. 於rpmfind.net 搜尋並下載awstat*rpm for RHEL5.x (awstats-6.9-2.el5.rf.noarch.rpm)
2. # rpm -ivh awstat*rpm
3. 修改/etc/httpd/conf/httpd.conf:
AddHandler cgi-script .cgi .pl
4. 修改/etc/httpd/conf.d/awstats.conf :
Alias /awstats/icon/ /var/www/awstats/icon/
ScriptAlias /awstats/ /var/www/awstats/
        DirectoryIndex awstats.pl
        Options ExecCGI
        Order allow,deny
        Allow from all
#       order deny,allow
#       deny from all
#       allow from 127.0.0.1
5. 將/etc/awstats/awstats.localhost.localdomain.conf改成/etc/awstats/awstats.IPADDRESS.conf (e.g., IPADDRESS=192.168.0.1)

6.手動執行/etc/cron.hourly/00awstats
如此一來就馬上有資料產生了,之後就交給system crontable更新吧!

7. 開啟瀏覽器並輸入 http://192.168.0.1/awstats/awstats.pl,成功的話應該會看到以下的畫面:

Note: awstats.IPADDRESS.conf 有許多參數可調整,請參考裡面的說明。

Sunday, June 07, 2009

帽客剛整理好的桌面

剛整理好,乾淨了一些,拍個照紀錄一下。


Saturday, June 06, 2009

[Shell script] Services watchdog

這隻script的功能就是當偵測到所指定的port於本機沒有啟動的話,就會將它自動重啟,放在cron table裡定時檢查吧!

#!/bin/bash

SRVLIST='80:/etc/init.d/httpd
25:/etc/init.d/postfix
22:/etc/init.d/sshd
110:/etc/init.d/dovecot'

for SRV in `echo $SRVLIST`
do
 nc -w 1 localhost `echo $SRV | cut -d: -f1` > /dev/null 2>&1 && echo "Port $SRV fine" || `echo $SRV|cut -d: -f2` restart
done


[Shell script] Monitor process cpu loading and count

1. 當process CPU loading超過90%, 就先行刪除:
#!/bin/bash
CPULOADING=90
ps auxh | \
while read pro
do
set -- $pro
if [ `echo $3 | cut -d"." -f1` -gt $CPULOADING ]; then
kill $2
fi
done

2.當process數目超過50時, 就先行刪除:
#!/bin/bash
PSCOUNT=50
ps axh -o "cmd" | sort | uniq -d -c | \
while read ps
do
set -- $ps
if [ $1 -gt $PSCOUNT ]; then
killall $2
fi
done

Reference: Linux網路安全管理與監控

Friday, June 05, 2009

[Shell script] Monitor Disk space

#!/bin/bash
USAGE_SPACE=20

df | grep '[0-9]%' | egrep 'sd|hd' | \
while read line 
do
# if [ `echo $line | awk -F" " '{ print $5 }' | cut -d% -f1` -gt "$USAGE_SPACE" ]; then
# echo "Disk space of `echo $line | awk -F" " '{ print $6}'` belongs to `hostname` is over $USAGE_SPACE%" | mail -s "HD DISK is not enough" $USER
# fi

set -- $line
if [ `echo $5 | cut -d% -f1` -gt $USAGE_SPACE ]; then
echo "Disk space of `echo $6` belongs to `hostname` is over $USAGE_SPACE%" | mail -s "HD DISK is not enough" $USER
fi
done

Remark的部份是小弟以前的做法,利用awk去filter出硬碟使用的百分比與目錄名稱,但拜讀了新書"Linux 網路安全管理與監控",學到了可透過set --$line方便取出某行的資料為引數使用,好用!