前幾天發了重定向以及管道相關使用方法,今天這裡發些很有趣的例子。通過重定向實現基於tcp/udp協議的軟體通訊。
linux 裝置裡面有個比較特殊的檔案:
/dev/[tcp|upd]/host/port 只要讀取或者寫入這個檔案,相當於系統會嘗試連線:host 這臺機器,對應port埠。如果主機以及埠存在,就建立一個socket 連線。將在,/proc/self/fd目錄下面,有對應的檔案出現。
一、測試下:/dev/tcp/host/post檔案
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
[chengmo@centos5 shell]$ cat</dev/tcp/127.0.0.1/22 SSH-2.0-OpenSSH_5.1 #我的機器shell埠是:22 #實際:/dev/tcp根本沒有這個目錄,這是屬於特殊裝置 [chengmo@centos5 shell]$ cat</dev/tcp/127.0.0.1/223 -bash: connect: 拒絕連線 -bash: /dev/tcp/127.0.0.1/223: 拒絕連線 #223介面不存在,開啟失敗 [chengmo@centos5 shell]$ exec 8<>/dev/tcp/127.0.0.1/22 [chengmo@centos5 shell]$ ls -l /proc/self/fd/ 總計 0 lrwx------ 1 chengmo chengmo 64 10-21 23:05 0 -> /dev/pts/0 lrwx------ 1 chengmo chengmo 64 10-21 23:05 1 -> /dev/pts/0 lrwx------ 1 chengmo chengmo 64 10-21 23:05 2 -> /dev/pts/0 lr-x------ 1 chengmo chengmo 64 10-21 23:05 3 -> /proc/22185/fd lrwx------ 1 chengmo chengmo 64 10-21 23:05 8 -> socket:[15067661] #檔案描述符8,已經開啟一個socket通訊通道,這個是一個可以讀寫socket通道,因為用:"<>"開啟 [chengmo@centos5 shell]$ exec 8>&- #關閉通道 [chengmo@centos5 shell]$ ls -l /proc/self/fd/ 總計 0 lrwx------ 1 chengmo chengmo 64 10-21 23:08 0 -> /dev/pts/0 lrwx------ 1 chengmo chengmo 64 10-21 23:08 1 -> /dev/pts/0 lrwx------ 1 chengmo chengmo 64 10-21 23:08 2 -> /dev/pts/0 lr-x------ 1 chengmo chengmo 64 10-21 23:08 3 -> /proc/22234/fd |
從時間伺服器讀取時間:
[chengmo@centos5 html]$ cat</dev/tcp/time-b.nist.gov/13
55491 10-10-22 11:33:49 17 0 0 596.3 UTC(NIST) *
上面這條語句使用重定向輸入語句就可以了。
二、通過重定向讀取遠端web伺服器頭資訊
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#!/bin/sh #testhttphead.sh #實現通過主機名,埠讀取web 伺服器header資訊 #copyright chengmo,qq:8292669 if(($#<2));then echo "usage:$0 host port"; exit 1; fi #如果引數缺失,退出程式,返回狀態1 exec 6<>/dev/tcp/$1/$2 2>/dev/null; #開啟host的port 可讀寫的socket連線,與檔案描述符6連線 if(($?!=0));then echo "open $1 $2 error!"; exit 1; fi #如果開啟失敗,$?返回不為0,終止程式 echo -e "HEAD / HTTP/1.1\n\n\n\n\n">&6; #將HEAD 資訊,傳送給socket連線 cat<&6; #從socket讀取返回資訊,顯示為標準輸出 exec 6<&-; exec 6>&-; #關閉socket的輸入,輸出 exit 0; |
指令碼建立後:存為testhttphead.sh
執行結果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[chengmo@centos5 ~/shell]$ sh testhttphead.sh www.baidu.com 80 HTTP/1.1 200 OK Date: Thu, 21 Oct 2010 15:17:23 GMT Server: BWS/1.0 Content-Length: 6218 Content-Type: text/html;charset=gb2312 Cache-Control: private Expires: Thu, 21 Oct 2010 15:17:23 GMT Set-Cookie: BAIDUID=1C40B2F8C676180FD887379A6E286DC1:FG=1; expires=Thu, 21-Oct-40 15:17:23 GMT; path=/; domain=.baidu.com P3P: CP=" OTI DSP COR IVA OUR IND COM " Connection: Keep-Alive [chengmo@centos5 ~/shell]$ sh testhttphead.sh 127.0.0.1 8080 open 127.0.0.1 8080 error! |
突然有個奇怪想法:
我們在windows時代就通過telnet 可以實現tcp/upd協議通訊,那麼如果用傳統方法怎麼實現呢?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[chengmo@centos5 ~/shell]$ echo -e "HEAD / HTTP/1.1\n\n\n\n\n"|telnet www.baidu.com 80 Trying 220.181.6.175... Connected to www.baidu.com. Escape character is '^]'. Connection closed by foreign host. #直接給傳送,失敗 [chengmo@centos5 ~/shell]$ (telnet www.baidu.com 80)<<EOF HEAD / HTTP/1.1 EOF Trying 220.181.6.175... Connected to www.baidu.com. Escape character is '^]'. Connection closed by foreign host. #重定向輸入,還是失敗? |
找到正確方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[chengmo@centos5 shell]$ (echo -e "HEAD / HTTP/1.1\n\n\n\n\n";sleep 2)|telnet www.baidu.com 80 Trying 220.181.6.175... Connected to www.baidu.com. Escape character is '^]'. HTTP/1.1 200 OK Date: Thu, 21 Oct 2010 15:51:58 GMT Server: BWS/1.0 Content-Length: 6218 Content-Type: text/html;charset=gb2312 Cache-Control: private Expires: Thu, 21 Oct 2010 15:51:58 GMT Set-Cookie: BAIDUID=0B6A01ACECD5353E4247E088A8CB345A:FG=1; expires=Thu, 21-Oct-40 15:51:58 GMT; path=/; domain=.baidu.com P3P: CP=" OTI DSP COR IVA OUR IND COM " Connection: Keep-Alive #成功了!加入sleep 居然可以了,sleep 改成1秒也可以 |
是不是由於sleep後,echo會推出2秒發給通道:telnet呢?推論可以從這2個方面推翻:
一個方面:通過()括的資料是一對命令,會作為一個子命令執行,一起執行完程式結束。每個命令echo語句,就直接傳送到螢幕(也就是標準輸出),只要有標準輸出了,就會通過通道馬上傳個:telnet ,如果接下來命令還有輸出,會注意傳給telnet ,直到()內所有命令執行完,與通道連線就斷開了。
再一個方面:如果說是起到推遲傳送的話,什麼時候有資料過來,發給telnet,什麼時候telnet命令啟動。跟你推遲一點還是早一點傳送過來。沒有關係。
這種型別命令,看出sleep,其實就是保持通道跟telnet 連線2秒鐘。 通道連線著了,telnet終端輸入也還在,因此可以保持從baidu伺服器獲得資料。
所以,延遲多久,還是跟伺服器處理速度有關係。
如果通過echo 向telnet傳送資料,保持通道聯通,使用sleep是個很好方法。
通過重定向給telnet輸入引數這種方法,我還想不到怎麼樣實現延遲輸入。有知道朋友,可以指點指點.
區別:
telnet與echo 實現 http訪問,與通過開啟讀寫socket是不一樣的,開啟socket通道,是可以進行交換處理的。傳入命令,活動結果,再傳入命令,再獲得結果。telnet通過echo 就不能這樣處理了
三、通過shell指令碼重定向實現監控memcache狀態
例項:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#!/bin/sh #通過傳入ip 以及埠,傳送指令獲得返回資料 #copyright chengmo qq:8292669 #函式往往放到最上面 function sendmsg() { msg=$1; echo "$1">&8; getout; } #向socket通道傳送指令,並且呼叫獲得返回引數 function getout() { #read 命令 -u 從開啟檔案描述符 8 讀取資料,-d讀取資料忽略掉:\r換行符 while read -u 8 -d $'\r' name; do if [ "${name}" == "END" -o "${name}" == "ERROR" ];then break; fi; echo $name; done } #由於:memcached每次通訊完畢,會返回:END或者ERROR(出錯),通過判斷是否是"END"覺得讀取是不是結束,否則迴圈不會停止 if [ $# -lt 2 ];then echo "usage:$0 host port [command]"; exit 1; fi; [[ $# -gt 2 ]]&&command=$3; #設定預設值 如果command為定義則為:stats command="${command=stats}"; host="$1"; port="$2"; exec 8<>/dev/tcp/${host}/${port}; #開啟通向通道是8 if [ "$?" != "0" ];then echo "open $host $port fail!"; exit 1; fi sendmsg "$command"; #傳送指定命令 sendmsg "quit"; #傳送退出通向命令 exec 8<&-; exec 8>&-; #關閉socket通道 exit 0; |
這是通過重定向,實現socket通訊中,傳送然後獲取返回的例子。其實,上面程式碼看似一次只能傳送一段。時間上。我們可以反覆呼叫:sendmsg ,捕捉輸出資料。實現連續的,讀與寫操作。
其它實現方法:
其實通過:telnet也可以實現的。
[chengmo@centos5 shell]$ (echo “stats”;sleep 2)|telnet 127.0.0.1 11211
通過nc命令實現:
[chengmo@centos5 shell]$ (echo “stats”)|nc 127.0.0.1 11211
不需要加延遲,直接開啟通道
第二個程式裡面,看到shell完全可以處理互動設計了。如果按照這樣,登陸ftp,pop3,stmp都可以類似實現。這些,我們通過shell socket類似程式實現,應該不困難,只是捕捉如傳送解析的問題了。