<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[实践OK]通过shell脚本重定向实现监控memcache状态]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Mon, 01 Nov 2010 04:33:44 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	背景：有时PHP在操作memcache时会出现set后删除不了疑问题（匪夷所思），需要用shell下对memcache进行调试，这样操作相对方便些。<br/>flush_all 全清：<br/>flush_all<br/>注：flush并不会将items删除，只是将所有的items标记为expired，因此这时memcache依旧占用所有内存。<br/>用status查看其确实还占用内存。<br/>stats<br/>STAT total_items 18375<br/><br/>shell下的使用方法:<br/>先set 几个<br/>[root@host5-7 ~]# telnet 192.168.5.21 19867<br/>Trying 192.168.5.21...<br/>Connected to 192.168.5.21 (192.168.5.21).<br/>Escape character is &#039;^]&#039;.<br/>set test1 0 0 3<br/>123<br/>STORED<br/>set test2 0 0 3<br/>345<br/>STORED<br/>set test3 0 0 3<br/>567<br/>STORED<br/>set test4 0 0 3<br/>789<br/>STORED<br/>quit<br/>Connection closed by foreign host.<br/>实践如下：<br/><textarea name="code" class="C" rows="15" cols="100">
[root@localhost config]# telnet 10.70.3*.1 11211
Trying 10.70.33.1...
Connected to 10.70.33.1.
Escape character is &#039;^]&#039;.
set test1 0 0 3
123
STORED
set test2 0 0 3
345
STORED
set test2 0 0 3
567
STORED
set test4 0 0 3
789
STORED
get test1
VALUE test1 0 3
123
END
delete test1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
DELETED
get test1
END

[root@localhost config]# telnet 10.70.3*.2 11211
Trying 10.70.33.2...
Connected to 10.70.33.2.
Escape character is &#039;^]&#039;.
set test1 0 0 3
123
STORED
get test1
VALUE test1 0 3
123
END
delete test1
DELETED
get test1
END
</textarea><br/><br/><br/>添加，删除，修改示例：<br/>telnet 192.168.1.100 11211 <br/>add name 0 60 5&nbsp;&nbsp;&nbsp;&nbsp; [说明 add 是指令名&nbsp;&nbsp;name 是key的名字 (是以key/value存放), 0 标志, 60 表示数据存放 60s&nbsp;&nbsp; 5表示 放入多大数据 ], 如果一个key已经存在，再放入是失败的.<br/>get name [获取 name的值]<br/>//更新<br/>set name 0 60 5&nbsp;&nbsp; [如果 name 这个key存在,就是更新, 如果key不存在，就是添加]<br/>//删除<br/>delete key值<br/><br/><br/>本质shell的telnet 的Memcache通讯返回，如下：<br/><div class="code">(echo &quot;stats&quot;;sleep 2)&#124;telnet 172.*.*.* 12000</div><br/>Escape character is &#039;^]&#039;.<br/>STAT pid 13574<br/>STAT uptime 8897547<br/>根据这个原理写了监控Memcache的shell如下：<br/><div class="code">#!/bin/sh<br/> <br/>#通过传入ip 以及端口，发送指令获得返回数据<br/>#copyright chengmo qq:8292669<br/> <br/>#函数往往放到最上面<br/>function sendmsg()<br/>&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;msg=$1;<br/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&nbsp;&quot;$1&quot;&gt;&amp;8;<br/>&nbsp;&nbsp;&nbsp;&nbsp;getout;<br/>&#125;<br/>#向socket通道发送指令，并且调用获得返回参数<br/> <br/>function getout()<br/>&#123;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;#read 命令 -u 从打开文件描述符 8 读取数据，-d读取数据忽略掉:&#92;r换行符<br/>&nbsp;&nbsp;&nbsp;&nbsp;while read -u 8 -d $&#039;&#92;r&#039; name;<br/>&nbsp;&nbsp;&nbsp;&nbsp;do <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if &#91; &quot;$&#123;name&#125;&quot; == &quot;END&quot;&nbsp;&nbsp;-o &quot;$&#123;name&#125;&quot; == &quot;ERROR&quot; &#93;;then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fi;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $name;<br/>&nbsp;&nbsp;&nbsp;&nbsp;done<br/>&#125;<br/>#由于：memcached每次通讯完毕，会返回：END或者ERROR(出错），通过判断是否是&quot;END&quot;觉得读取是不是结束，否则循环不会停止<br/> <br/>if &#91; $# -lt 2 &#93;;then<br/>&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;usage:$0 host port &#91;command&#93;&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;exit 1;<br/>fi;<br/> <br/>&#91;&#91; $# -gt 2 &#93;&#93;&amp;&amp;command=$3;<br/> <br/>#设置默认值 如果command为定义则为：stats<br/>command=&quot;$&#123;command=stats&#125;&quot;;<br/>host=&quot;$1&quot;;<br/>port=&quot;$2&quot;;<br/> <br/> <br/> <br/>exec 8&lt;&gt;/dev/tcp/$&#123;host&#125;/$&#123;port&#125;;<br/>#打开通向通道是8<br/> <br/>if &#91; &quot;$?&quot; != &quot;0&quot; &#93;;then<br/>&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;open $host&nbsp;&nbsp;$port fail!&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;exit 1;<br/>fi<br/> <br/>sendmsg &quot;$command&quot;;<br/>#发送指定命令<br/> <br/> <br/>sendmsg &quot;quit&quot;;<br/>#发送退出通向命令<br/> <br/> <br/>exec 8&lt;&amp;-;<br/>exec 8&gt;&amp;-;<br/>#关闭socket通道<br/> <br/>exit 0;</div><br/><br/><br/>这是通过重定向，实现socket通讯中，发送然后获取返回的例子。其实，上面代码看似一次只能发送一段。时间上。我们可以反复调用：sendmsg ，捕捉输出数据。实现连续的，读与写操作。<br/><br/> <br/><br/>其它实现方法：<br/><br/>其实通过：telnet也可以实现的。<br/><br/>[chengmo@centos5 shell]$ (echo &quot;stats&quot;;sleep 2)&#124;telnet 127.0.0.1 11211<br/><br/>通过nc命令实现：<br/><br/>[chengmo@centos5 shell]$ (echo &quot;stats&quot;)&#124;nc 127.0.0.1 11211<br/><br/>不需要加延迟，直接打开通道<br/><br/>来源：http://www.cnblogs.com/chengmo/archive/2010/10/22/1858302.html<br/><br/><br/><div class="code">&#91;chengmo@centos5 shell&#93;$ (echo -e &quot;HEAD / HTTP/1.1&#92;n&#92;n&#92;n&#92;n&#92;n&quot;;sleep 2)&#124;telnet www.baidu.com 80<br/>Trying 220.181.6.175...<br/>Connected to www.baidu.com.<br/>Escape character is &#039;^&#93;&#039;.<br/>HTTP/1.1 200 OK<br/>Date: Thu, 21 Oct 2010 15:51:58 GMT<br/>Server: BWS/1.0<br/>Content-Length: 6218<br/>Content-Type: text/html;charset=gb2312<br/>Cache-Control: private<br/>Expires: Thu, 21 Oct 2010 15:51:58 GMT<br/>Set-Cookie: BAIDUID=0B6A01ACECD5353E4247E088A8CB345A:FG=1; expires=Thu, 21-Oct-40 15:51:58 GMT; path=/; domain=.baidu.com<br/>P3P: CP=&quot; OTI DSP COR IVA OUR IND COM &quot;<br/>Connection: Keep-Alive<br/>#成功了！加入sleep 居然可以了，sleep 改成1秒也可以</div><br/><br/><br/>[chengmo@centos5&nbsp;&nbsp;shell]$ cat&lt;/dev/tcp/127.0.0.1/22<br/>SSH-2.0-OpenSSH_5.1<br/>#我的机器shell端口是：22<br/>#实际:/dev/tcp根本没有这个目录，这是属于特殊设备<br/>[chengmo@centos5&nbsp;&nbsp;shell]$ cat&lt;/dev/tcp/127.0.0.1/223<br/>-bash: connect: 拒绝连接<br/>-bash: /dev/tcp/127.0.0.1/223: 拒绝连接<br/>#223接口不存在,打开失败<br/> <br/><br/><div class="code">&#91;chengmo@centos5&nbsp;&nbsp;shell&#93;$ exec 8&lt;&gt;/dev/tcp/127.0.0.1/22<br/>&#91;chengmo@centos5&nbsp;&nbsp;shell&#93;$ ls -l /proc/self/fd/<br/>总计 0<br/>lrwx------ 1 chengmo chengmo 64 10-21 23:05 0 -&gt; /dev/pts/0<br/>lrwx------ 1 chengmo chengmo 64 10-21 23:05 1 -&gt; /dev/pts/0<br/>lrwx------ 1 chengmo chengmo 64 10-21 23:05 2 -&gt; /dev/pts/0<br/>lr-x------ 1 chengmo chengmo 64 10-21 23:05 3 -&gt; /proc/22185/fd<br/>lrwx------ 1 chengmo chengmo 64 10-21 23:05 8 -&gt; socket:&#91;15067661&#93;<br/> <br/>#文件描述符8，已经打开一个socket通讯通道，这个是一个可以读写socket通道,因为用：&quot;&lt;&gt;&quot;打开<br/>&#91;chengmo@centos5&nbsp;&nbsp;shell&#93;$ exec 8&gt;&amp;-<br/>#关闭通道<br/>&#91;chengmo@centos5&nbsp;&nbsp;shell&#93;$ ls -l /proc/self/fd/<br/>总计 0<br/>lrwx------ 1 chengmo chengmo 64 10-21 23:08 0 -&gt; /dev/pts/0<br/>lrwx------ 1 chengmo chengmo 64 10-21 23:08 1 -&gt; /dev/pts/0<br/>lrwx------ 1 chengmo chengmo 64 10-21 23:08 2 -&gt; /dev/pts/0<br/>lr-x------ 1 chengmo chengmo 64 10-21 23:08 3 -&gt; /proc/22234/fd</div><br/><br/><br/>通过重定向读取远程web服务器头信息<br/><br/><br/><div class="code">#!/bin/sh<br/>#testhttphead.sh<br/>#实现通过主机名，端口读取web 服务器header信息<br/>#copyright chengmo,qq:8292669<br/> <br/>if(($#&lt;2));then<br/>&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;usage:$0 host port&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;exit 1;<br/>fi<br/>#如果参数缺失，退出程序，返回状态1<br/> <br/>exec 6&lt;&gt;/dev/tcp/$1/$2 2&gt;/dev/null;<br/>#打开host的port 可读写的socket连接，与文件描述符6连接<br/> <br/>if(($?!=0));then<br/>&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;open $1 $2 error!&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;exit 1;<br/>fi<br/>#如果打开失败，$?返回不为0，终止程序<br/> <br/>echo -e &quot;HEAD / HTTP/1.1&#92;n&#92;n&#92;n&#92;n&#92;n&quot;&gt;&amp;6;<br/>#将HEAD 信息，发送给socket连接<br/> <br/>cat&lt;&amp;6;<br/>#从socket读取返回信息，显示为标准输出<br/> <br/>exec 6&lt;&amp;-;<br/>exec 6&gt;&amp;-;<br/>#关闭socket的输入，输出<br/> <br/>exit 0;</div><br/><br/>linux 设备里面有个比较特殊的文件:<br/><br/>/dev/[tcp&#124;upd]/host/port 只要读取或者写入这个文件，相当于系统会尝试连接:host 这台机器，对应port端口。如果主机以及端口存在，就建立一个socket 连接。将在，/proc/self/fd目录下面，有对应的文件出现。 <br/><br/>[chengmo@centos5&nbsp;&nbsp;shell]$ cat&lt;/dev/tcp/127.0.0.1/22<br/>SSH-2.0-OpenSSH_5.1<br/>#我的机器shell端口是：22<br/>#实际:/dev/tcp根本没有这个目录，这是属于特殊设备<br/>[chengmo@centos5&nbsp;&nbsp;shell]$ cat&lt;/dev/tcp/127.0.0.1/223<br/>-bash: connect: 拒绝连接<br/>-bash: /dev/tcp/127.0.0.1/223: 拒绝连接<br/>#223接口不存在,打开失败<br/> <br/>[chengmo@centos5&nbsp;&nbsp;shell]$ exec 8&lt;&gt;/dev/tcp/127.0.0.1/22<br/>[chengmo@centos5&nbsp;&nbsp;shell]$ ls -l /proc/self/fd/<br/>总计 0<br/>lrwx------ 1 chengmo chengmo 64 10-21 23:05 0 -&gt; /dev/pts/0<br/>lrwx------ 1 chengmo chengmo 64 10-21 23:05 1 -&gt; /dev/pts/0<br/>lrwx------ 1 chengmo chengmo 64 10-21 23:05 2 -&gt; /dev/pts/0<br/>lr-x------ 1 chengmo chengmo 64 10-21 23:05 3 -&gt; /proc/22185/fd<br/>lrwx------ 1 chengmo chengmo 64 10-21 23:05 8 -&gt; socket:[15067661]<br/> <br/>#文件描述符8，已经打开一个socket通讯通道，这个是一个可以读写socket通道,因为用：&quot;&lt;&gt;&quot;打开<br/>[chengmo@centos5&nbsp;&nbsp;shell]$ exec 8&gt;&amp;-<br/>#关闭通道<br/>[chengmo@centos5&nbsp;&nbsp;shell]$ ls -l /proc/self/fd/<br/>总计 0<br/>lrwx------ 1 chengmo chengmo 64 10-21 23:08 0 -&gt; /dev/pts/0<br/>lrwx------ 1 chengmo chengmo 64 10-21 23:08 1 -&gt; /dev/pts/0<br/>lrwx------ 1 chengmo chengmo 64 10-21 23:08 2 -&gt; /dev/pts/0<br/>lr-x------ 1 chengmo chengmo 64 10-21 23:08 3 -&gt; /proc/22234/fd<br/><br/>从时间服务器读取时间：<br/><br/>[chengmo@centos5 html]$ cat&lt;/dev/tcp/time-b.nist.gov/13<br/><br/>55491 10-10-22 11:33:49 17 0 0 596.3 UTC(NIST) *<br/><br/>上面这条语句使用重定向输入语句就可以了。<br/>
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]通过shell脚本重定向实现监控memcache状态]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>