前几天发了重定向以及管道相关使用方法,今天这里发些很有趣的例子。通过重定向实现基于tcp/udp协议的软件通讯。
linux 设备里面有个比较特殊的文件:
/dev/[tcp|upd]/host/port 只要读取或者写入这个文件,相当于系统会尝试连接:host 这台机器,对应port端口。如果主机以及端口存在,就建立一个socket 连接。将在,/proc/self/fd目录下面,有对应的文件出现。
一、测试下:/dev/tcp/host/post文件
01 | [chengmo@centos5 shell]$ cat </dev/tcp/127.0.0.1/22 |
05 | [chengmo@centos5 shell]$ cat </dev/tcp/127.0.0.1/223 |
07 | - bash : /dev/tcp/127.0.0.1/223: 拒绝连接 |
10 | [chengmo@centos5 shell]$ exec 8<>/dev/tcp/127.0.0.1/22 |
11 | [chengmo@centos5 shell]$ ls -l /proc/self/fd/ |
13 | lrwx------ 1 chengmo chengmo 64 10-21 23:05 0 -> /dev/pts/0 |
14 | lrwx------ 1 chengmo chengmo 64 10-21 23:05 1 -> /dev/pts/0 |
15 | lrwx------ 1 chengmo chengmo 64 10-21 23:05 2 -> /dev/pts/0 |
16 | lr-x------ 1 chengmo chengmo 64 10-21 23:05 3 -> /proc/22185/fd |
17 | lrwx------ 1 chengmo chengmo 64 10-21 23:05 8 -> socket:[15067661] |
20 | [chengmo@centos5 shell]$ exec 8>&- |
22 | [chengmo@centos5 shell]$ ls -l /proc/self/fd/ |
24 | lrwx------ 1 chengmo chengmo 64 10-21 23:08 0 -> /dev/pts/0 |
25 | lrwx------ 1 chengmo chengmo 64 10-21 23:08 1 -> /dev/pts/0 |
26 | lrwx------ 1 chengmo chengmo 64 10-21 23:08 2 -> /dev/pts/0 |
27 | 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服务器头信息
07 | echo "usage:$0 host port" ; |
12 | exec 6<>/dev/tcp/$1/$2 2>/dev/null; |
16 | echo "open $1 $2 error!" ; |
21 | echo -e "HEAD / HTTP/1.1/n/n/n/n/n" >&6; |
脚本建立后:存为testhttphead.sh
运行结果:
01 | [chengmo@centos5 ~/shell]$ sh testhttphead.sh www.baidu.com 80 |
03 | Date: Thu, 21 Oct 2010 15:17:23 GMT |
06 | Content-Type: text/html;charset=gb2312 |
08 | Expires: Thu, 21 Oct 2010 15:17:23 GMT |
09 | Set-Cookie: BAIDUID=1C40B2F8C676180FD887379A6E286DC1:FG=1; expires=Thu, 21-Oct-40 15:17:23 GMT; path=/; domain=.baidu.com |
10 | P3P: CP= " OTI DSP COR IVA OUR IND COM " |
13 | [chengmo@centos5 ~/shell]$ sh testhttphead.sh 127.0.0.1 8080 |
14 | open 127.0.0.1 8080 error! |
突然
有个奇怪想法:
我们在windows时代就通过telnet 可以实现tcp/upd协议通讯,那么如果用传统方法怎么实现呢?
01 | [chengmo@centos5 ~/shell]$ echo -e "HEAD / HTTP/1.1/n/n/n/n/n" |telnet www.baidu.com 80 |
02 | Trying 220.181.6.175... |
03 | Connected to www.baidu.com. |
04 | Escape character is '^]' . |
05 | Connection closed by foreign host. |
08 | [chengmo@centos5 ~/shell]$ (telnet www.baidu.com 80)<<EOF |
13 | Trying 220.181.6.175... |
14 | Connected to www.baidu.com. |
15 | Escape character is '^]' . |
16 | Connection closed by foreign host. |
找到正确方法:
01 | [chengmo@centos5 shell]$ ( echo -e "HEAD / HTTP/1.1/n/n/n/n/n" ; sleep 2)|telnet www.baidu.com 80 |
02 | Trying 220.181.6.175... |
03 | Connected to www.baidu.com. |
04 | Escape character is '^]' . |
06 | Date: Thu, 21 Oct 2010 15:51:58 GMT |
09 | Content-Type: text/html;charset=gb2312 |
11 | Expires: Thu, 21 Oct 2010 15:51:58 GMT |
12 | Set-Cookie: BAIDUID=0B6A01ACECD5353E4247E088A8CB345A:FG=1; expires=Thu, 21-Oct-40 15:51:58 GMT; path=/; domain=.baidu.com |
13 | P3P: CP= " OTI DSP COR IVA OUR IND COM " |
是不是由于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状态
实例:
18 | while read -u 8 -d $ '/r' name; |
20 | if [ "${name}" == "END" -o "${name}" == "ERROR" ]; then |
29 | echo "usage:$0 host port [command]" ; |
36 | command = "${command=stats}" ; |
42 | exec 8<>/dev/tcp/${host}/${port}; |
45 | if [ "$?" != "0" ]; then |
46 | echo "open $host $port fail!" ; |
这是通过重定向,实现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类似程序实现,应该不困难,只是捕捉如发送解析的问题了。