<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dipplum.com &#187; 计算机</title>
	<atom:link href="http://dipplum.com/category/computer/feed/" rel="self" type="application/rss+xml" />
	<link>http://dipplum.com</link>
	<description>Be  the change you wanna see in the world</description>
	<lastBuildDate>Sat, 12 Nov 2011 07:38:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>自动连接ssh -D的launchd配置</title>
		<link>http://dipplum.com/2010/09/12/launchd-plist-for-ssh-d/</link>
		<comments>http://dipplum.com/2010/09/12/launchd-plist-for-ssh-d/#comments</comments>
		<pubDate>Sat, 11 Sep 2010 18:05:41 +0000</pubDate>
		<dc:creator>li</dc:creator>
				<category><![CDATA[开源软件]]></category>
		<category><![CDATA[autossh]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[upstart]]></category>

		<guid isPermaLink="false">http://dipplum.com/?p=889</guid>
		<description><![CDATA[背景 与Ubuntu的upstart类似，Mac OS上提供了launchd作为后台服务的管理程序。ssh -D命令可以为用户提供sock5代理，但是每次访问网络之前，都要打开Terminal手动运行ssh命令是一件很烦的事情。加上网络条件不好，ssh频繁退出，就更烦了。Linux下有autossh+upstart/init可以选择，Mac OS下就需要借助launchd了 配置Mac到远程ssh服务器的自动登录 为了将ssh -D变成launchd管理的后台服务，配置ssh自动登录是必须的。Mac下过程与Linux不太一样，首先打开Terminal，生成用户ssh证书： sudo su ssh-keygen cat /var/root/.ssh/id_rsa.pub 然后把公钥上传到ssh服务器。注意：Mac OS下root的HOME被设置在了/var/root下。 ssh EXAMPLE.COM mkdir .ssh scp id_rsa.pub EXAMPLE.com:.ssh/authorized_keys &#160; # 测试公钥认证是否成功 ssh -o BatchMode=yes EXAMPLE.COM 生成launchd格式的plist文件 将下述ssh-d.plist文件放在/Library/LaunchDaemon/目录下： &#60;?xml version=&#34;1.0&#34; encoding=&#34;UTF-8&#34;?&#62; &#60;!DOCTYPE plist PUBLIC &#34;-//Apple Computer//DTD PLIST 1.0//EN&#34; &#34;http://www.apple.com/DTDs/PropertyList-1.0.dtd&#34;&#62; &#60;plist version=&#34;1.0&#34;&#62; &#60;dict&#62; &#60;key&#62;Label&#60;/key&#62; &#60;string&#62;localdomain.localhost.ssh-d&#60;/string&#62; &#60;key&#62;ProgramArguments&#60;/key&#62; &#60;array&#62; &#60;string&#62;/usr/bin/ssh&#60;/string&#62; &#60;string&#62;-o&#60;/string&#62; &#60;string&#62;BatchMode=yes&#60;/string&#62; &#60;string&#62;-o&#60;/string&#62; &#60;string&#62;CheckHostIP=no&#60;/string&#62; &#60;string&#62;-o&#60;/string&#62; [...]]]></description>
			<content:encoded><![CDATA[<h4>背景</h4>
<p>与Ubuntu的upstart类似，Mac OS上提供了launchd作为后台服务的管理程序。ssh -D命令可以为用户提供sock5代理，但是每次访问网络之前，都要打开Terminal手动运行ssh命令是一件很烦的事情。加上网络条件不好，ssh频繁退出，就更烦了。Linux下有autossh+upstart/init可以选择，Mac OS下就需要借助launchd了</p>
<h4>配置Mac到远程ssh服务器的自动登录</h4>
<p>为了将ssh -D变成launchd管理的后台服务，配置ssh自动登录是必须的。Mac下过程与Linux不太一样，首先打开Terminal，生成用户ssh证书：<span id="more-889"></span></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">su</span>
<span style="color: #c20cb9; font-weight: bold;">ssh-keygen</span>
<span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>root<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>id_rsa.pub</pre></div></div>

<p>然后把公钥上传到ssh服务器。注意：Mac OS下root的HOME被设置在了/var/root下。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ssh</span> EXAMPLE.COM <span style="color: #c20cb9; font-weight: bold;">mkdir</span> .ssh
<span style="color: #c20cb9; font-weight: bold;">scp</span> id_rsa.pub EXAMPLE.com:.ssh<span style="color: #000000; font-weight: bold;">/</span>authorized_keys
&nbsp;
<span style="color: #666666; font-style: italic;"># 测试公钥认证是否成功</span>
<span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-o</span> <span style="color: #007800;">BatchMode</span>=<span style="color: #c20cb9; font-weight: bold;">yes</span> EXAMPLE.COM</pre></div></div>

<h4>生成launchd格式的plist文件</h4>
<p>将下述ssh-d.plist文件放在/Library/LaunchDaemon/目录下：</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE plist PUBLIC &quot;-//Apple Computer//DTD PLIST 1.0//EN&quot; &quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plist</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Label<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>localdomain.localhost.ssh-d<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>ProgramArguments<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/usr/bin/ssh<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-o<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>BatchMode=yes<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-o<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CheckHostIP=no<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-o<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>StrictHostKeyChecking=no<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-D<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1080<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-i<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/var/root/.ssh/id_rsa<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-vvvNnT<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>root@EXAMPLE.com<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>KeepAlive<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;true</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>RunAtLoad<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;true</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>StandardOutPath<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/var/log/ssh-d-out.log<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>StandardErrorPath<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/var/log/ssh-d-err.log<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plist<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>然后运行下面的命令后台启动ssh -D：</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">launchctl load <span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>LaunchDaemon<span style="color: #000000; font-weight: bold;">/</span>ssh-d.plist</pre></div></div>

<p>如果出错需要调试，ssh进程的标准输出和标准错误输出保存在/var/log目录下的ssh-d-out.log和ssh-d-err.log文件中。如果一切正常，则配置完毕，以后Mac启动后，ssh -D进程会自动启动，ssh退出后，launchd也会自动生成一个新的进程。</p>
<h4>与autossh集成</h4>
<p>如果通过fink和MacPorts或者其他方法安装了autossh，可以修改上述脚本中的ssh程序的路径为autossh的路径。并将autossh的参数按格式加入plist文件，这样，也能享受到autossh提供的隧道假死后自动重启功能。</p>
<h4>延伸阅读</h4>
<ul>
<li><a href="http://aa.cs.uit.no/blog/20050528-email">POP and SMTP over ssh with launchd</a></li>
<li><a href="http://www.devdaily.com/mac-os-x/launchd-examples-launchd-plist-file-examples-mac">launchd plist example files</a></li>
<li><a href="http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html">launchd.plist(5) Mac OS X Manual Page</a></li>
<li><a href="http://autoproxy2pac.appspot.com">autoproxy2pac</a> (与ssh -D配合使用)</li>
</ul</p>
]]></content:encoded>
			<wfw:commentRss>http://dipplum.com/2010/09/12/launchd-plist-for-ssh-d/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>autossh在Ubuntu上的配置</title>
		<link>http://dipplum.com/2010/09/11/autossh-ubuntu-upstart-script/</link>
		<comments>http://dipplum.com/2010/09/11/autossh-ubuntu-upstart-script/#comments</comments>
		<pubDate>Sat, 11 Sep 2010 15:05:11 +0000</pubDate>
		<dc:creator>li</dc:creator>
				<category><![CDATA[开源软件]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[upstart]]></category>

		<guid isPermaLink="false">http://dipplum.com/?p=886</guid>
		<description><![CDATA[背景 ssh除了可以提供远程登录服务之外，还可以建立主机之间的网络隧道，尤其是可以提供SOCKS代理（传说中的ssh -D）。但用ssh命令建立的隧道可能会受网络的影响而中断，不能为用户提供持续的服务。autossh正好是解决上述问题的工具：为用户提供可靠的ssh隧道服务。 在使用autossh之前，我的做法是：首先，做一个shell脚本检查相应的ssh进程是否存在，如不存在，则重新启动ssh隧道；然后，将上述脚本加入系统crontab，定时执行，例如1分钟1次。但是，用shell脚本检查有一个缺点：有时候，ssh进程虽然并没有退出，但ssh隧道已经不能正常转发报文了，shell脚本难以发现这类情况。为了避免ssh进程存在、隧道假死的问题，可以采用定时重启ssh进程并重新建立ssh隧道的方法。但重启间隔不好设置：间隔过短，导致ssh隧道用户频频掉线，影响用户使用；间隔过长，一旦隧道假死，在重启ssh服务之前会有较长的服务中断间隔。 autossh对ssh隧道的监控则更加有效。首先，在开始执行时，autossh首先创建ssh子进程，建立隧道。同时autossh作为父进程，随时监控ssh进程是否退出，一旦退出则立即启动新的ssh隧道。autossh这种做法比crontab定时检查在失效恢复速度上更具有优势。其次，autossh还会定期检查ssh隧道是否能够正确传输数据，如发现隧道假死，也会强制重启ssh，建立新的ssh隧道。 Ubuntu上的autossh autossh在Ubuntu上的安装很简单，运行下面的命令即可: apt-get install autossh 然而，Ubuntu下的autossh并没有包含相关的后台服务脚本。用户如果希望开机自动运行autossh需要自己写daemon脚本。Ubuntu下写daemon脚本有两个选择，一种是写传统的/etc/init.d下的脚本，令一种就是Ubuntu特有的upstart脚本。 upstart是Ubuntu设计用来替换传统的SysV init的软件。upstart的daemon脚本除了更加简洁之外，还支持服务的自动唤醒（respawn）。这样autossh本身如果出错退出了，也会立即被upstart唤醒。避免autossh出错退出导致ssh隧道无法访问。 我写的autossh的upstart脚本文件如下（文件名是/etc/init/autossh.conf）： # autossh &#160; description &#34;autossh daemon&#34; &#160; start on runlevel [2345] stop on runlevel [!2345] &#160; respawn respawn limit 5 60 # respawn max 5 times in 60 seconds &#160; script export AUTOSSH_PIDFILE=/var/run/autossh.pid export AUTOSSH_PORT=10007:7 export AUTOSSH_POLL=60 export AUTOSSH_FIRST_POLL=30 autossh -4 [...]]]></description>
			<content:encoded><![CDATA[<h4>背景</h4>
<p><a href="http://www.openssh.com/">ssh</a>除了可以提供远程登录服务之外，还可以建立主机之间的网络隧道，尤其是可以提供SOCKS代理（传说中的ssh -D）。但用ssh命令建立的隧道可能会受网络的影响而中断，不能为用户提供持续的服务。<a href="http://www.harding.motd.ca/autossh/">autossh</a>正好是解决上述问题的工具：为用户提供可靠的ssh隧道服务。</p>
<p>在使用autossh之前，我的做法是：首先，做一个shell脚本检查相应的ssh进程是否存在，如不存在，则重新启动ssh隧道；然后，将上述脚本加入系统crontab，定时执行，例如1分钟1次。但是，用shell脚本检查有一个缺点：有时候，ssh进程虽然并没有退出，但ssh隧道已经不能正常转发报文了，shell脚本难以发现这类情况。为了避免ssh进程存在、隧道假死的问题，可以采用定时重启ssh进程并重新建立ssh隧道的方法。但重启间隔不好设置：间隔过短，导致ssh隧道用户频频掉线，影响用户使用；间隔过长，一旦隧道假死，在重启ssh服务之前会有较长的服务中断间隔。</p>
<p>autossh对ssh隧道的监控则更加有效。首先，在开始执行时，autossh首先创建ssh子进程，建立隧道。同时autossh作为父进程，随时监控ssh进程是否退出，一旦退出则立即启动新的ssh隧道。autossh这种做法比crontab定时检查在失效恢复速度上更具有优势。其次，autossh还会定期检查ssh隧道是否能够正确传输数据，如发现隧道假死，也会强制重启ssh，建立新的ssh隧道。<span id="more-886"></span></p>
<h4>Ubuntu上的autossh</h4>
<p>autossh在Ubuntu上的安装很简单，运行下面的命令即可:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> autossh</pre></div></div>

<p>然而，Ubuntu下的autossh并没有包含相关的后台服务脚本。用户如果希望开机自动运行autossh需要自己写daemon脚本。Ubuntu下写daemon脚本有两个选择，一种是写传统的/etc/init.d下的脚本，令一种就是Ubuntu特有的upstart脚本。</p>
<p><a href="http://upstart.ubuntu.com/">upstart</a>是Ubuntu设计用来替换传统的<a href="http://en.wikipedia.org/wiki/Init">SysV init</a>的软件。upstart的daemon脚本除了更加简洁之外，还支持服务的自动唤醒（respawn）。这样autossh本身如果出错退出了，也会立即被upstart唤醒。避免autossh出错退出导致ssh隧道无法访问。</p>
<p>我写的autossh的upstart脚本文件如下（文件名是/etc/init/autossh.conf）：</p>

<div class="wp_syntax"><div class="code"><pre class="upstart" style="font-family:monospace;"># autossh
&nbsp;
description	&quot;autossh daemon&quot;
&nbsp;
start on runlevel [2345]
stop on runlevel [!2345]
&nbsp;
respawn
respawn limit 5 60 # respawn max 5 times in 60 seconds
&nbsp;
script
    export AUTOSSH_PIDFILE=/var/run/autossh.pid
    export AUTOSSH_PORT=10007:7
    export AUTOSSH_POLL=60
    export AUTOSSH_FIRST_POLL=30
    autossh -4 -N example.com -D 1080 -o BatchMode=yes -o StrictHostKeyChecking=no
end script</pre></div></div>

<p>这样重启之后，autossh就会自动启动。手动启动、停止、重启autossh服务可以通过运行start、stop、restart等命令完成。</p>
<h4>其他</h4>
<p>1. ssh自动登录</p>
<p>autossh在后台执行ssh命令建立隧道时，需要设置好配置好本机的ssh证书，以及服务器上的authorized_keys文件，以实现ssh的自动登录。否则autossh将无法登录远程主机并建立隧道。相关说明参见<a href="http://www.google.com.hk/search?q=ssh+authorized_keys">网络上的教程</a>。</p>
<p>2. echo服务</p>
<p>上述autossh脚本中的AUTOSSH_PORT参数，是autossh用于判断ssh隧道健康状况专用的隧道链接，参数意义是：建立本地端口10007到远程服务器端口7的隧道，其他参数说明初次启动ssh时，在30秒后判断隧道链接情况，之后每60秒检查一次。</p>
<p>其中，远程服务器端口7，运行的是标准的<a href="http://en.wikipedia.org/wiki/Echo_Protocol">TCP Echo服务</a>。在Ubuntu上echo服务可以通过openbsd-inetd来实现，首先在远程服务器（<strong>注意</strong>：不是autossh所在的ssh客户端机器）上安装软件：</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> openbsd-inetd</pre></div></div>

<p>安装之后，inetd默认没有把echo服务激活，需要手动在/etc/inetd.conf配置文件中增加下面一行：</p>

<div class="wp_syntax"><div class="code"><pre class="inetd" style="font-family:monospace;">echo		stream	tcp	nowait	root	internal</pre></div></div>

<p>重启inetd后，远程服务器的echo服务就正式上线了，可以供autossh判断网络隧道连通情况了。</p>
]]></content:encoded>
			<wfw:commentRss>http://dipplum.com/2010/09/11/autossh-ubuntu-upstart-script/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>用crond运行WordPress中的计划任务</title>
		<link>http://dipplum.com/2010/09/10/wodpress-use-cron/</link>
		<comments>http://dipplum.com/2010/09/10/wodpress-use-cron/#comments</comments>
		<pubDate>Fri, 10 Sep 2010 13:49:05 +0000</pubDate>
		<dc:creator>li</dc:creator>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://dipplum.com/?p=881</guid>
		<description><![CDATA[WordPress站点运行时需要偶尔在后台定时运行一些任务，如定时发布文章，清空Cache的内容，同步别的站点的RSS Feed等。传统来说，计划任务可以通过Linux系统下的crond实现，但考虑到不是所有的网站托管服务都能让用户设定crontab，因此Wordpress中自带了一套计划任务引擎，姑且称之为WP-Cron，这是Wordpress自带的一套函数，和单独的wp-cron插件不是一回事。 WP-Cron的原理是这样的：虽然用户不能通过crond设置计划任务，但网站随时可能会有用户在点击，只要在用户点击的时候，判断当时是否有需要运行的后台任务，如果则在后台运行该任务，就可以实现类似crond的效果。WP-Cron的实现还是比较轻量级的，虽然后台任务是被用户的访问激活的，但后台任务的执行并不会影响用户的访问速度，因为Wordpress在处理用户请求时，发起单独的一个HTTP请求访问wp-cron.php页面后，并不等待wp-cron.php将后台任务执行完毕，也不继续接收由wp-cron.php返回的数据，而是立即处理用户的访问请求。相关细节可以参见wp-includes/cron.php文件。 虽然WP-Cron有着这么多优点，我还是想把它禁用，原因有2: 首先，我的可以直接使用系统的crond，而WP-Cron最多只是没有时的凑合方案(Workaround)；其次，WP-Cron最大的缺点是不能保证任务定时执行，当网站没有用户访问时，WP-Cron就不起作用了。 禁用WP-Cron很简单，编辑wp-config.php，加入一下一行: define&#40;'DISABLE_WP_CRON', true&#41;; 然后在你后台系统的crontab文件里，增加下面的内容： */15 * * * * YOUR_USER /usr/bin/wget -q --post-data '' http://YOUR_SITE/wp-cron.php?doing_wp_cron -O /dev/null 上面配置指示，每15分钟，调用wget命令访问站点的wp-cron.php文件，post-data参数指示wget用POST方法，而不是GET方法，这样可以避免WP-Super-Cache这样的缓存插件影响后台任务的正确执行，但post-data传输的数据必须是空内容，否则wp-cron.php会拒绝执行。 相关阅读： WordPress Support: Can I disable cron job? Run wordpress from real cron job (Page 1) Run wordpress from real cron job (Page 2)]]></description>
			<content:encoded><![CDATA[<p>WordPress站点运行时需要偶尔在后台定时运行一些任务，如定时发布文章，清空Cache的内容，同步别的站点的RSS Feed等。传统来说，计划任务可以通过Linux系统下的crond实现，但考虑到不是所有的网站托管服务都能让用户设定crontab，因此Wordpress中自带了一套计划任务引擎，姑且称之为<a href="http://codex.wordpress.org/Category:WP-Cron_Functions">WP-Cron</a>，这是Wordpress自带的一套函数，和单独的<a href="http://wordpress.org/extend/plugins/wp-cron/">wp-cron插件</a>不是一回事。<span id="more-881"></span></p>
<p>WP-Cron的原理是这样的：虽然用户不能通过crond设置计划任务，但网站随时可能会有用户在点击，只要在用户点击的时候，判断当时是否有需要运行的后台任务，如果则在后台运行该任务，就可以实现类似crond的效果。WP-Cron的实现还是比较轻量级的，虽然后台任务是被用户的访问激活的，但后台任务的执行并不会影响用户的访问速度，因为Wordpress在处理用户请求时，发起单独的一个HTTP请求访问wp-cron.php页面后，并不等待wp-cron.php将后台任务执行完毕，也不继续接收由wp-cron.php返回的数据，而是立即处理用户的访问请求。相关细节可以参见wp-includes/cron.php文件。</p>
<p>虽然WP-Cron有着这么多优点，我还是想把它禁用，原因有2: 首先，我的可以直接使用系统的crond，而WP-Cron最多只是没有时的凑合方案(Workaround)；其次，WP-Cron最大的缺点是不能保证任务定时执行，当网站没有用户访问时，WP-Cron就不起作用了。</p>
<p>禁用WP-Cron很简单，编辑wp-config.php，加入一下一行:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DISABLE_WP_CRON'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>然后在你后台系统的crontab文件里，增加下面的内容：</p>

<div class="wp_syntax"><div class="code"><pre class="crontab" style="font-family:monospace;">*/15 * * * * YOUR_USER /usr/bin/wget -q --post-data '' http://YOUR_SITE/wp-cron.php?doing_wp_cron -O /dev/null</pre></div></div>

<p>上面配置指示，每15分钟，调用wget命令访问站点的wp-cron.php文件，post-data参数指示wget用POST方法，而不是GET方法，这样可以避免WP-Super-Cache这样的缓存插件影响后台任务的正确执行，但post-data传输的数据必须是空内容，否则wp-cron.php会拒绝执行。</p>
<p>相关阅读：</p>
<ul>
<li><a href="http://wordpress.org/support/topic/can-i-disable-cron-job">WordPress Support: Can I disable cron job?</a></li>
<li><a href="http://blog.dreamdevil.com/index.php/2010/01/29/run-wordpress-tasks-from-real-cron-job/">Run wordpress from real cron job (Page 1)</a></li>
<li><a href="http://blog.dreamdevil.com/index.php/2010/01/29/run-wordpress-tasks-from-real-cron-job/2/">Run wordpress from real cron job (Page 2)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://dipplum.com/2010/09/10/wodpress-use-cron/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>用fail2ban监控nginx日志</title>
		<link>http://dipplum.com/2010/09/09/fail2ban-nginx-log/</link>
		<comments>http://dipplum.com/2010/09/09/fail2ban-nginx-log/#comments</comments>
		<pubDate>Thu, 09 Sep 2010 08:20:26 +0000</pubDate>
		<dc:creator>li</dc:creator>
				<category><![CDATA[开源软件]]></category>
		<category><![CDATA[fail2ban]]></category>
		<category><![CDATA[nginx]]></category>

		<guid isPermaLink="false">http://dipplum.com/2010/09/09/fail2ban-nginx-log/</guid>
		<description><![CDATA[背景 fail2ban是一款日志扫描软件, 尝试从日志中发现恶意的攻击行为, 尤其是用户名密码的失败尝试, 并可以通过iptables防火墙封禁恶意用户的IP, 以防止进一步的攻击. 最近在nginx服务器的日志中发现了很多可疑的请求, 看起来像是试图从Web服务器上发现漏洞页面: 221.204.246.105 - - [08/Sep/2010:06:45:13 +0000] &#34;GET /dbzhedit/ewebeditor.asp HTTP/1.1&#34; 404 5748 &#34;-&#34; &#34;Mozilla/4.0&#34; 221.204.246.105 - - [08/Sep/2010:06:45:14 +0000] &#34;GET /edit/ewebeditor.asp HTTP/1.1&#34; 404 5744 &#34;-&#34; &#34;Mozilla/4.0&#34; 221.204.246.105 - - [08/Sep/2010:06:45:15 +0000] &#34;GET /ugvbadmin/edit/ewebeditor.asp HTTP/1.1&#34; 404 5754 &#34;-&#34; &#34;Mozilla/4.0&#34; 222.189.228.42 - - [08/Sep/2010:18:10:50 +0000] &#34;GET /piqmUserReg.asp HTTP/1.1&#34; 404 5790 &#34;-&#34; [...]]]></description>
			<content:encoded><![CDATA[<h4>背景</h4>
<p><a href="http://www.fail2ban.org/">fail2ban</a>是一款日志扫描软件, 尝试从日志中发现恶意的攻击行为, 尤其是用户名密码的失败尝试, 并可以通过iptables防火墙封禁恶意用户的IP, 以防止进一步的攻击.</p>
<p>最近在nginx服务器的日志中发现了很多可疑的请求, 看起来像是试图从Web服务器上发现漏洞页面:</p>

<div class="wp_syntax"><div class="code"><pre class="syslog" style="font-family:monospace;">221.204.246.105 - - [08/Sep/2010:06:45:13 +0000] &quot;GET /dbzhedit/ewebeditor.asp HTTP/1.1&quot; 404 5748 &quot;-&quot; &quot;Mozilla/4.0&quot;
221.204.246.105 - - [08/Sep/2010:06:45:14 +0000] &quot;GET /edit/ewebeditor.asp HTTP/1.1&quot; 404 5744 &quot;-&quot; &quot;Mozilla/4.0&quot;
221.204.246.105 - - [08/Sep/2010:06:45:15 +0000] &quot;GET /ugvbadmin/edit/ewebeditor.asp HTTP/1.1&quot; 404 5754 &quot;-&quot; &quot;Mozilla/4.0&quot;
222.189.228.42 - - [08/Sep/2010:18:10:50 +0000] &quot;GET /piqmUserReg.asp HTTP/1.1&quot; 404 5790 &quot;-&quot; &quot;Mozilla/4.0&quot;
222.189.228.42 - - [08/Sep/2010:18:10:51 +0000] &quot;GET /UserReg.asp HTTP/1.1&quot; 404 5786 &quot;-&quot; &quot;Mozilla/4.0&quot;
222.189.228.42 - - [08/Sep/2010:18:10:52 +0000] &quot;GET /ioifupfile_flash.asp HTTP/1.1&quot; 404 5795 &quot;-&quot; &quot;Mozilla/4.0&quot;
222.189.228.42 - - [08/Sep/2010:18:10:53 +0000] &quot;GET /upfile_flash.asp HTTP/1.1&quot; 404 5791 &quot;-&quot; &quot;Mozilla/4.0&quot;
222.189.228.42 - - [08/Sep/2010:18:10:53 +0000] &quot;GET /admin/zhmuupfile_flash.asp HTTP/1.1&quot; 404 5801 &quot;-&quot; &quot;Mozilla/4.0&quot;
222.189.228.42 - - [08/Sep/2010:18:10:54 +0000] &quot;GET /admin/upfile_flash.asp HTTP/1.1&quot; 404 5797 &quot;-&quot; &quot;Mozilla/4.0&quot;
222.189.228.42 - - [08/Sep/2010:18:10:54 +0000] &quot;GET /admins/xvmbupfile_flash.asp HTTP/1.1&quot; 404 5802 &quot;-&quot; &quot;Mozilla/4.0&quot;</pre></div></div>

<h4>安装fail2ban</h4>
<p>我觉得可以用fail2ban扫描日志中上述攻击, 并且封禁恶意用户. 首先安装fail2ban, 在Ubuntu/Debian下用apt-get一次搞定:<span id="more-876"></span></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> fail2ban</pre></div></div>

<h4>配置fail2ban的nginx过滤规则</h4>
<p>从攻击行为特征来看, 这是短时间连续导致服务器发送HTTP 404文件未找到错误码, 下面是用于发现上述攻击的fail2ban filter规则, 在/etc/fail2ban/filter.d/目录下建立nginx.conf文件保存下面的内容:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>Definition<span style="">&#93;</span></span>
&nbsp;
<span style="color: #000099;">failregex</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> &amp;lt</span><span style="color: #666666; font-style: italic;">;HOST&amp;gt; -.*- .*HTTP/1.* 404 .*$</span>
<span style="color: #000099;">ignoreregex</span> <span style="color: #000066; font-weight:bold;">=</span></pre></div></div>

<h4>测试fail2ban过滤规则</h4>
<p>在正式激活改过滤规则之前, 可以首先用fail2ban-regex测试规则的有效性:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/nginx.conf</span>
Running tests
=============
&nbsp;
Use regex <span style="color: #c20cb9; font-weight: bold;">file</span> : <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>fail2ban<span style="color: #000000; font-weight: bold;">/</span>filter.d<span style="color: #000000; font-weight: bold;">/</span>nginx.conf
Use log <span style="color: #c20cb9; font-weight: bold;">file</span>   : <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>access.log
&nbsp;
Results
=======
&nbsp;
Failregex
<span style="color: #000000; font-weight: bold;">|</span>- Regular expressions:
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;</span>lt;HOST<span style="color: #000000; font-weight: bold;">&amp;</span>gt; -.<span style="color: #000000; font-weight: bold;">*</span>-.<span style="color: #000000; font-weight: bold;">*</span>HTTP<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1</span>.<span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">404</span> .<span style="color: #000000; font-weight: bold;">*</span>$
<span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">`</span>- Number of matches:
   <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000;">1304</span> match<span style="color: #7a0874; font-weight: bold;">&#40;</span>es<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
Ignoreregex
<span style="color: #000000; font-weight: bold;">|</span>- Regular expressions:
<span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">`</span>- Number of matches:
&nbsp;
Summary
=======
&nbsp;
Addresses found:
<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
    222.189.228.42 <span style="color: #7a0874; font-weight: bold;">&#40;</span>Wed Sep 08 <span style="color: #000000;">18</span>:<span style="color: #000000;">10</span>:<span style="color: #000000;">50</span> <span style="color: #000000;">2010</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
    222.189.228.42 <span style="color: #7a0874; font-weight: bold;">&#40;</span>Wed Sep 08 <span style="color: #000000;">18</span>:<span style="color: #000000;">10</span>:<span style="color: #000000;">51</span> <span style="color: #000000;">2010</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
    222.189.228.42 <span style="color: #7a0874; font-weight: bold;">&#40;</span>Wed Sep 08 <span style="color: #000000;">18</span>:<span style="color: #000000;">10</span>:<span style="color: #000000;">52</span> <span style="color: #000000;">2010</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
    222.189.228.42 <span style="color: #7a0874; font-weight: bold;">&#40;</span>Wed Sep 08 <span style="color: #000000;">18</span>:<span style="color: #000000;">10</span>:<span style="color: #000000;">52</span> <span style="color: #000000;">2010</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
    ...
&nbsp;
Date template hits:
...
XXXX hit<span style="color: #7a0874; font-weight: bold;">&#40;</span>s<span style="color: #7a0874; font-weight: bold;">&#41;</span>: Day<span style="color: #000000; font-weight: bold;">/</span>MONTH<span style="color: #000000; font-weight: bold;">/</span>Year:Hour:Minute:Second
...
&nbsp;
Success, the total number of match is YYYY
&nbsp;
However, look at the above section <span style="color: #ff0000;">'Running tests'</span> <span style="color: #c20cb9; font-weight: bold;">which</span> could contain important
information.</pre></div></div>

<h4>激活fail2ban过滤规则</h4>
<p>从测试结果可以看出, 恶意攻击节点的IP地址和攻击时间都能够正确发现, 因此可以进一步修改fail2ban的配置文件激活上述规则. 下面是我的/etc/fail2ban/jail.local配置文件内容:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>DEFAULT<span style="">&#93;</span></span>
<span style="color: #000099;">ignoreip</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 127.0.0.1</span>
<span style="color: #000099;">bantime</span>  <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 3600</span>
<span style="color: #000099;">maxretry</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 6</span>
<span style="color: #000099;">destemail</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> root</span>
<span style="color: #000099;">action</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> %<span style="">&#40;</span>action_mwl<span style="">&#41;</span>s</span>
&nbsp;
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>nginx<span style="">&#93;</span></span>
<span style="color: #000099;">enabled</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> true</span>
<span style="color: #000099;">port</span>	<span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> http,https</span>
<span style="color: #000099;">filter</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> nginx</span>
<span style="color: #000099;">logpath</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">  /var/log/nginx/access.log</span></pre></div></div>

<p>上述配置设置fail2ban用nginx过滤规则监控nginx的access.log文件, 如果发现恶意攻击, 除了在iptables防火墙中封禁该客户端IP之外, 还将发送邮件包含该IP地址的whois信息给root. 用下面的命令激活上述配置:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">fail2ban-client reload</pre></div></div>

<p>从/var/log/fail2ban.log日志文件中可以看到上述nginx规则激活的信息:</p>

<div class="wp_syntax"><div class="code"><pre class="syslog" style="font-family:monospace;">2010-09-09 08:00:54,810 fail2ban.server : INFO   Changed logging target to /var/log/fail2ban.log for Fail2ban v0.8.4
2010-09-09 08:00:54,810 fail2ban.jail   : INFO   Creating new jail 'nginx'
2010-09-09 08:00:54,811 fail2ban.jail   : INFO   Jail 'nginx' uses poller
2010-09-09 08:00:54,812 fail2ban.filter : INFO   Added logfile = /var/log/nginx/access.log
2010-09-09 08:00:54,813 fail2ban.filter : INFO   Set maxRetry = 50
2010-09-09 08:00:54,815 fail2ban.filter : INFO   Set findtime = 600
2010-09-09 08:00:54,815 fail2ban.actions: INFO   Set banTime = 3600
...
2010-09-09 08:00:54,970 fail2ban.jail   : INFO   Jail 'nginx' started</pre></div></div>

<h4>测试fail2ban的效果</h4>
<p>可以用下面的命令模拟攻击者连续访问不存在的URL, 看看fail2ban的效果:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">true</span> ; <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>127.0.0.10<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">404</span> ; <span style="color: #000000; font-weight: bold;">done</span>
<span style="color: #666666; font-style: italic;"># type Ctrl-C when you stuck at &quot;Connecting to 127.0.0.10:80... &quot;</span></pre></div></div>

<p>看fail2ban的日志是否记录了上述攻击:</p>

<div class="wp_syntax"><div class="code"><pre class="syslog" style="font-family:monospace;">#   grep Ban /var/log/fail2ban.log
2010-09-09 08:06:09,338 fail2ban.actions: WARNING [nginx-fnf] Ban 127.0.0.10</pre></div></div>

<p>用iptables命令看fail2ban添加的IP封禁规则:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># iptables -L</span>
Chain INPUT <span style="color: #7a0874; font-weight: bold;">&#40;</span>policy ACCEPT<span style="color: #7a0874; font-weight: bold;">&#41;</span>
target     prot opt <span style="color: #7a0874; font-weight: bold;">source</span>               destination
fail2ban-nginx  tcp  <span style="color: #660033;">--</span>  anywhere             anywhere            multiport dports www,https
&nbsp;
Chain FORWARD <span style="color: #7a0874; font-weight: bold;">&#40;</span>policy ACCEPT<span style="color: #7a0874; font-weight: bold;">&#41;</span>
target     prot opt <span style="color: #7a0874; font-weight: bold;">source</span>               destination
&nbsp;
Chain OUTPUT <span style="color: #7a0874; font-weight: bold;">&#40;</span>policy ACCEPT<span style="color: #7a0874; font-weight: bold;">&#41;</span>
target     prot opt <span style="color: #7a0874; font-weight: bold;">source</span>               destination
&nbsp;
Chain fail2ban-nginx <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span> references<span style="color: #7a0874; font-weight: bold;">&#41;</span>
target     prot opt <span style="color: #7a0874; font-weight: bold;">source</span>               destination
DROP       all  <span style="color: #660033;">--</span>  127.0.0.10           anywhere
RETURN     all  <span style="color: #660033;">--</span>  anywhere             anywhere</pre></div></div>

<p>fail2ban发来的邮件看起来像是这样的:</p>

<div class="wp_syntax"><div class="code"><pre class="email" style="font-family:monospace;">Hi,
&nbsp;
The IP 222.169.224.226 has just been banned by Fail2Ban after
7 attempts against ssh.
&nbsp;
Here are more information about 222.169.224.226:
&nbsp;
% [whois.apnic.net node-3]
% Whois data copyright terms    http://www.apnic.net/db/dbcopyright.html
&nbsp;
inetnum:        222.168.0.0 - 222.169.255.255
netname:        CHINANET-JL
descr:          CHINANET Jilin province network
descr:          Jilin Telecom Corporation
...
&nbsp;
Lines containing IP:222.169.224.226 in /var/log/auth.log
&nbsp;
Sep  9 02:30:14 localhost sshd[24401]: Did not receive identification string from 222.169.224.226
Sep  9 02:34:59 localhost sshd[24511]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=222.169.224.226  user=root
Sep  9 02:35:01 localhost sshd[24511]: Failed password for root from 222.169.224.226 port 36724 ssh2
Sep  9 02:35:03 localhost sshd[24515]: Invalid user fluffy from 222.169.224.226
Sep  9 02:35:03 localhost sshd[24515]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=222.169.224.226
Sep  9 02:35:05 localhost sshd[24515]: Failed password for invalid user fluffy from 222.169.224.226 port 36927 ssh2
Sep  9 02:35:06 localhost sshd[24519]: Invalid user admin from 222.169.224.226
Sep  9 02:35:06 localhost sshd[24519]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=222.169.224.226
Sep  9 02:35:09 localhost sshd[24519]: Failed password for invalid user admin from 222.169.224.226 port 37140 ssh2
Sep  9 02:35:10 localhost sshd[24521]: Invalid user test from 222.169.224.226
Sep  9 02:35:10 localhost sshd[24521]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=222.169.224.226
Sep  9 02:35:12 localhost sshd[24521]: Failed password for invalid user test from 222.169.224.226 port 37391 ssh2</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://dipplum.com/2010/09/09/fail2ban-nginx-log/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WP-Mobile-Pack在Multisite模式下的修正</title>
		<link>http://dipplum.com/2010/09/08/fix-wordpress-mobile-pack-with-multisite/</link>
		<comments>http://dipplum.com/2010/09/08/fix-wordpress-mobile-pack-with-multisite/#comments</comments>
		<pubDate>Wed, 08 Sep 2010 11:46:26 +0000</pubDate>
		<dc:creator>li</dc:creator>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wp-plugin]]></category>

		<guid isPermaLink="false">http://dipplum.com/2010/09/08/fix-wordpress-mobile-pack-with-multisite/</guid>
		<description><![CDATA[WordPress Mobile Pack插件为Wordpress站点提供适合移动设备浏览的页面. WPMP支持3种检测模式: 1. 根据浏览器User-Agent决定博客输出, 2. 为博客单独设置一个Mobile域名, 根据用户访问的域名决定输出, 3. 上述基于浏览器和基于域名的结合. 从3.0版本开始, WordPress支持Multisite模模式, 也就是原先的Wordpress-MU的延续. 在Multisite模式下, 可以用同一套Wordpress软件和数据库同时管理多个Blog. 通过domain_mapping插件, 还可以为每个Blog设置多个域名. domain_mapping插件可以为每个Blog设置一个主域名, 并且将其他域名的访问请求通过&#8221;Permanent Redirect&#8221;的方法重定向到主域名上, 据说这样可以提高站点的Pagerank. Anyway, 这个功能一般推荐选上, 如下图所示: 不过, domain_mapping插件的Permanent Redirect功能和WPMP的Mobile域名功能冲突, 当用户访问WPMP给站点设置的Mobile域名的时候, 会被domain_mapping插件重定向到主域名. 解决WPMP插件和domain_mapping插件之间的冲突, 需要首先修改domain_mapping.php中redirect_to_mapped_domain函数的实现, 或者下载我已经改好的文件: function redirect_to_mapped_domain&#40;&#41; &#123; global $current_blog, $wpdb; if &#40; !isset&#40; $_SERVER&#91; 'HTTPS' &#93; &#41; &#41; $_SERVER&#91; 'HTTPS' &#93; = &#34;off&#34;; $protocol = &#40; [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://wordpress.org/extend/plugins/wordpress-mobile-pack/">WordPress Mobile Pack</a>插件为Wordpress站点提供适合移动设备浏览的页面. WPMP支持3种检测模式:</p>
<p>1. 根据浏览器User-Agent决定博客输出,<br />
2. 为博客单独设置一个Mobile域名, 根据用户访问的域名决定输出,<br />
3. 上述基于浏览器和基于域名的结合.</p>
<p>从3.0版本开始, WordPress支持Multisite模模式, 也就是原先的Wordpress-MU的延续. 在Multisite模式下, 可以用同一套Wordpress软件和数据库同时管理多个Blog. 通过<a href="http://plugins.svn.wordpress.org/wordpress-mu-domain-mapping/trunk/">domain_mapping插件</a>, 还可以为每个Blog设置多个域名. domain_mapping插件可以为每个Blog设置一个主域名, 并且将其他域名的访问请求通过&#8221;Permanent Redirect&#8221;的方法重定向到主域名上, 据说这样可以提高站点的Pagerank. Anyway, 这个功能一般推荐选上, 如下图所示: <span id="more-863"></span></p>
<p><img src="http://dipplum.com/files/2010/09/domain_mapping.jpeg" alt="domain_mappging setting" width="539" height="448" /></p>
<p>不过, domain_mapping插件的Permanent Redirect功能和WPMP的Mobile域名功能冲突, 当用户访问WPMP给站点设置的Mobile域名的时候, 会被domain_mapping插件重定向到主域名.</p>
<p>解决WPMP插件和domain_mapping插件之间的冲突, 需要首先修改domain_mapping.php中redirect_to_mapped_domain函数的实现, 或者下载我已经改好的文件:</p>
Note: There is a file embedded within this post, please visit this post to download the file.

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> redirect_to_mapped_domain<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$current_blog</span><span style="color: #339933;">,</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span> <span style="color: #0000ff;">'HTTPS'</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
                <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span> <span style="color: #0000ff;">'HTTPS'</span> <span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;off&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$protocol</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'on'</span> <span style="color: #339933;">==</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTPS'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'https://'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'http://'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> domain_mapping_siteurl<span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">false</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$urltmp</span> <span style="color: #339933;">=</span> untrailingslashit<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$protocol</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$current_blog</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>domain <span style="color: #339933;">.</span> <span style="color: #000088;">$current_blog</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>path <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$is_alias</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wpmp_switcher_domains'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$is_alias</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$current_blog</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>domain <span style="color: #339933;">==</span> wpmp_switcher_domains<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mobile'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$current_blog</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>domain <span style="color: #339933;">==</span> wpmp_switcher_domains<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'desktop'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$url</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$url</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$urltmp</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #000088;">$is_alias</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$redirect</span> <span style="color: #339933;">=</span> get_site_option<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'dm_301_redirect'</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'301'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'302'</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'VHOST'</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">constant</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;VHOST&quot;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'yes'</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'SUBDOMAIN_INSTALL'</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">constant</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'SUBDOMAIN_INSTALL'</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span> <span style="color: #0000ff;">'REQUEST_URI'</span> <span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$current_blog</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>path<span style="color: #339933;">,</span> <span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span> <span style="color: #0000ff;">'REQUEST_URI'</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Location: <span style="color: #006699; font-weight: bold;">{$url}</span>{<span style="color: #006699; font-weight: bold;">$_SERVER</span>[ 'REQUEST_URI' ]}&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #000088;">$redirect</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #990000;">exit</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>除此之外, WPMP插件自身在Mobile域名模式下也有一些bug, 导致用户不能在Mobile站点和普通站点之间进行正确的切换. 下载下面的文件, 替换wordpress-mobile-pack插件子目录plugins/wpmp_switcher/下的wpmp_switcher.php文件, 可以解决上述bug. 具体修改参见如下patch：</p>
Note: There is a file embedded within this post, please visit this post to download the file.

<div class="wp_syntax"><div class="code"><pre class="diff" style="font-family:monospace;">diff --git a/wp-content/plugins/wordpress-mobile-pack/plugins/wpmp_switcher/wpmp_switcher.php b/wp-content/plugins/wordpress-mobile-pack/plugins/wpmp_switcher/wpmp_switcher.php
index ed<span style="color: #440088;">46c88</span>..2949ebb <span style="">100755</span>
<span style="color: #888822;">--- a/wp-content/plugins/wordpress-mobile-pack/plugins/wpmp_switcher/wpmp_switcher.php</span>
<span style="color: #888822;">+++ b/wp-content/plugins/wordpress-mobile-pack/plugins/wpmp_switcher/wpmp_switcher.php</span>
<span style="color: #440088;">@@ -92,12 +92,10 @@ function wpmp_switcher_init<span style="">&#40;</span><span style="">&#41;</span> <span style="">&#123;</span></span>
       wpmp_switcher_mobile_interstitial<span style="">&#40;</span><span style="">&#41;</span>;
       break;
     case WPMP_SWITCHER_REDIRECT_TO_MOBILE:
<span style="color: #991111;">-      wpmp_switcher_set_cookie<span style="">&#40;</span>'mobile'<span style="">&#41;</span>;</span>
       $target_url = &quot;http://&quot; . wpmp_switcher_domains<span style="">&#40;</span>'mobile', true<span style="">&#41;</span> . wpmp_switcher_current_path_plus_cgi<span style="">&#40;</span><span style="">&#41;</span>;
       header<span style="">&#40;</span>&quot;Location: $target_url&quot;<span style="">&#41;</span>;
       exit;
     case WPMP_SWITCHER_REDIRECT_TO_DESKTOP:
<span style="color: #991111;">-      wpmp_switcher_set_cookie<span style="">&#40;</span>'desktop'<span style="">&#41;</span>;</span>
       $target_url = &quot;http://&quot; . wpmp_switcher_domains<span style="">&#40;</span>'desktop', true<span style="">&#41;</span> . wpmp_switcher_current_path_plus_cgi<span style="">&#40;</span><span style="">&#41;</span>;
       header<span style="">&#40;</span>&quot;Location: $target_url&quot;<span style="">&#41;</span>;
       exit;
<span style="color: #440088;">@@ -373,13 +371,13 @@ function wpmp_switcher_outcome_process<span style="">&#40;</span>$switcher_mode, $desktop_domain, $mobile_</span>
     case 'browserdomain':
       if <span style="">&#40;</span>$desktop_domain<span style="">&#41;</span> <span style="">&#123;</span>
         if <span style="">&#40;</span>$desktop_browser<span style="">&#41;</span> <span style="">&#123;</span>
<span style="color: #991111;">-          if <span style="">&#40;</span><span style="">&#40;</span>$mobile_cookie &amp;&amp; !$cgi<span style="">&#41;</span> || $cgi == 'mobile'<span style="">&#41;</span> <span style="">&#123;</span></span>
<span style="color: #00b000;">+          if <span style="">&#40;</span>$mobile_cookie &amp;&amp; !$cgi<span style="">&#41;</span> <span style="">&#123;</span></span>
             return WPMP_SWITCHER_REDIRECT_TO_MOBILE;
           <span style="">&#125;</span> else <span style="">&#123;</span>
             return WPMP_SWITCHER_DESKTOP_PAGE;
           <span style="">&#125;</span>
         <span style="">&#125;</span> else <span style="">&#123;</span>
<span style="color: #991111;">-          if <span style="">&#40;</span><span style="">&#40;</span>$desktop_cookie &amp;&amp; !$cgi<span style="">&#41;</span> || $cgi == 'desktop'<span style="">&#41;</span> <span style="">&#123;</span></span>
<span style="color: #00b000;">+          if <span style="">&#40;</span>$cgi || $desktop_cookie<span style="">&#41;</span> <span style="">&#123;</span></span>
             return WPMP_SWITCHER_DESKTOP_PAGE;
           <span style="">&#125;</span> else <span style="">&#123;</span>
             if <span style="">&#40;</span>$mobile_cookie<span style="">&#41;</span> <span style="">&#123;</span>
<span style="color: #440088;">@@ -474,7 +472,7 @@ function wpmp_switcher_is_cgi_parameter_present<span style="">&#40;</span><span style="">&#41;</span> <span style="">&#123;</span></span>
&nbsp;
 function wpmp_switcher_link<span style="">&#40;</span>$type, $label<span style="">&#41;</span> <span style="">&#123;</span>
   $cookie = WPMP_SWITCHER_COOKIE_VAR . &quot;=$type;path=/;expires=Tue, 01-01-<span style="">2030</span> 00:00:00 GMT&quot;;
<span style="color: #991111;">-  $target_url = &quot;http://&quot; . wpmp_switcher_domains<span style="">&#40;</span>'desktop', true<span style="">&#41;</span> . wpmp_switcher_current_path_plus_cgi<span style="">&#40;</span>'', $type<span style="">&#41;</span>;</span>
<span style="color: #00b000;">+  $target_url = &quot;http://&quot; . wpmp_switcher_domains<span style="">&#40;</span>$type, true<span style="">&#41;</span> . wpmp_switcher_current_path_plus_cgi<span style="">&#40;</span>'', $type<span style="">&#41;</span>;</span>
   if <span style="">&#40;</span>$target_url<span style="">&#41;</span> <span style="">&#123;</span>
     return &quot;&lt;a onclick='document.cookie=\&quot;$cookie\&quot;;' href='$target_url'&gt;$label&lt;/a&gt;&quot;;
   <span style="">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://dipplum.com/2010/09/08/fix-wordpress-mobile-pack-with-multisite/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>删除Mac OS X上的MySQL</title>
		<link>http://dipplum.com/2010/09/07/uninstall-mysql-macosx/</link>
		<comments>http://dipplum.com/2010/09/07/uninstall-mysql-macosx/#comments</comments>
		<pubDate>Tue, 07 Sep 2010 06:58:07 +0000</pubDate>
		<dc:creator>li</dc:creator>
				<category><![CDATA[开源软件]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://dipplum.com/2010/09/07/uninstall-mysql-macosx/</guid>
		<description><![CDATA[MySQL提供的Mac OS X上的安装包不能卸载，需要手动删除。到网上搜到前辈总结的过程，稍微改了改，应该可以直接运行。 #!/bin/sh &#160; sudo killall /usr/local/mysql/bin/mysqld &#91; -e /usr/local/mysql &#93; &#38;&#38; sudo rm /usr/local/mysql sudo rm -rf /usr/local/mysql* sudo rm -rf /Library/StartupItems/MySQLCOM sudo rm -rf /Library/PreferencePanes/My* sudo rm -rf /Library/Receipts/mysql* sudo rm -rf /Library/Receipts/MySQL* sudo sed -i -e '/MYSQLCOM=-YES-/ d' /etc/hostconfig echo done !]]></description>
			<content:encoded><![CDATA[<p>MySQL提供的Mac OS X上的安装包不能卸载，需要手动删除。到网上搜到<a href="http://steveno.wordpress.com/2009/03/26/uninstall-mysql-on-mac-os-x/">前辈总结的过程</a>，稍微改了改，应该可以直接运行。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">killall</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>mysql<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>mysqld
<span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-e</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>mysql <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>mysql
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>mysql<span style="color: #000000; font-weight: bold;">*</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> <span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>StartupItems<span style="color: #000000; font-weight: bold;">/</span>MySQLCOM
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> <span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>PreferencePanes<span style="color: #000000; font-weight: bold;">/</span>My<span style="color: #000000; font-weight: bold;">*</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> <span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>Receipts<span style="color: #000000; font-weight: bold;">/</span>mysql<span style="color: #000000; font-weight: bold;">*</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> <span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>Receipts<span style="color: #000000; font-weight: bold;">/</span>MySQL<span style="color: #000000; font-weight: bold;">*</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-i</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'/MYSQLCOM=-YES-/ d'</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>hostconfig
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000; font-weight: bold;">done</span> <span style="color: #000000; font-weight: bold;">!</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://dipplum.com/2010/09/07/uninstall-mysql-macosx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress 3.0 Update Network超时</title>
		<link>http://dipplum.com/2010/09/01/wordpress-3-update-network-timeout/</link>
		<comments>http://dipplum.com/2010/09/01/wordpress-3-update-network-timeout/#comments</comments>
		<pubDate>Wed, 01 Sep 2010 11:26:34 +0000</pubDate>
		<dc:creator>li</dc:creator>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://dipplum.com/?p=853</guid>
		<description><![CDATA[最近将Blog升级到Wordpress 3.0. 新版本支持multisite模式, 我正好可以把几个Blog合并管理. 激活多站点模式后, WordPress管理菜单中新出现了Super Admin菜单, 下面包含Update菜单项, 在Wordpress升级后, 可以通过Update Network功能同时更新当前站点下的所有Blog. 不过最近使用Update Network功能从来没有成功过, 猜测是超时的问题. 我服务器使用的是nginx + php5-fpm, nginx的error.log中相关错误是ms-upgrade-network.php脚本产生的upstream timed out: 2010/08/18 08:39:37 [error] 1962#0: *39882 upstream timed out (110: Connection timed out) while reading upstream, client: 192.168.1.1 server: example.com, request: &#34;GET /wp-admin/ms-upgrade-network.php?action=upgrade HTTP/1.0&#34;, upstream: &#34;fastcgi://127.0.0.1:9000&#34;, host: &#34;example.com&#34;, referrer: &#34;http://example.com/wp-admin/ms-upgrade-network.php&#34; 解决方法包括两方面. 首先增加php脚本的时间限制, 相关参数主要有: max_execution_time, max_input_time. 默认是30还是60秒, [...]]]></description>
			<content:encoded><![CDATA[<p>最近将Blog升级到Wordpress 3.0. 新版本支持<a href="http://codex.wordpress.org/Create_A_Network">multisite</a>模式, 我正好可以把几个Blog合并管理. 激活多站点模式后, WordPress管理菜单中新出现了Super Admin菜单, 下面包含Update菜单项, 在Wordpress升级后, 可以通过<a href="http://codex.wordpress.org/Super_Admin_Update_SubPanel">Update Network</a>功能同时更新当前站点下的所有Blog.</p>
<p>不过最近使用Update Network功能从来没有成功过, 猜测是超时的问题. 我服务器使用的是nginx + php5-fpm, nginx的error.log中相关错误是ms-upgrade-network.php脚本产生的upstream timed out: <span id="more-853"></span></p>

<div class="wp_syntax"><div class="code"><pre class="syslog" style="font-family:monospace;">2010/08/18 08:39:37 [error] 1962#0: *39882 upstream timed out (110: Connection timed out) while reading upstream, client: 192.168.1.1 server: example.com, request: &quot;GET /wp-admin/ms-upgrade-network.php?action=upgrade HTTP/1.0&quot;, upstream: &quot;fastcgi://127.0.0.1:9000&quot;, host: &quot;example.com&quot;, referrer: &quot;http://example.com/wp-admin/ms-upgrade-network.php&quot;</pre></div></div>

<p>解决方法包括两方面. 首先增加php脚本的时间限制, 相关参数主要有: <a href="http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time">max_execution_time</a>, <a href="http://www.php.net/manual/en/info.configuration.php#ini.max-input-time">max_input_time</a>. 默认是30还是60秒, 对于Update Network来说可能不够, 所以要修改php-fpm的配置(/etc/php5/fpm/php.ini):</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000099;">max_execution_time</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 300</span>
<span style="color: #000099;">max_input_time</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 600</span></pre></div></div>

<p>还有就是增加nginx等待的超时限制, 参见<a href="http://wiki.nginx.org/NginxHttpFcgiModule">Nginx的文档</a>, 主要有fastcgi_read_timeout, fastcgi_send_timeout, 可能和上面的php参数是对应的(read对应execution, send对应input):</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">location ~ \.<span style="color: #202020;">php</span>$ <span style="color: #009900;">&#123;</span>
	fastcgi_pass   127.0.0.1<span style="color: #339933;">:</span><span style="color: #0000dd;">9000</span><span style="color: #339933;">;</span>
        ...
        <span style="color: #202020;">fastcgi_read_timeout</span> <span style="color: #0000dd;">300</span><span style="color: #339933;">;</span>
        fastcgi_send_timeout <span style="color: #0000dd;">600</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>修改完成之后重启nginx和php5-fpm服务器, Update Network可以正常结束.</p>
]]></content:encoded>
			<wfw:commentRss>http://dipplum.com/2010/09/01/wordpress-3-update-network-timeout/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>在Ubuntu上安装L2TP/IPsec VPN服务器</title>
		<link>http://dipplum.com/2010/08/30/ubuntu-install-l2tp-server/</link>
		<comments>http://dipplum.com/2010/08/30/ubuntu-install-l2tp-server/#comments</comments>
		<pubDate>Mon, 30 Aug 2010 12:52:37 +0000</pubDate>
		<dc:creator>li</dc:creator>
				<category><![CDATA[开源软件]]></category>
		<category><![CDATA[install]]></category>
		<category><![CDATA[ipsec]]></category>
		<category><![CDATA[l2tp]]></category>
		<category><![CDATA[openswan]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://dipplum.com/2010/08/30/ubuntu-install-l2tp-server/</guid>
		<description><![CDATA[最近用VPN翻墙比较流行, 我也赶了一下潮流: 买了一个Linode的VPS, 安装了最新的Ubuntu Lucid (10.04), 并在上面配置了L2TP服务器. 以下虽然是在10.04上的配置过程, 但应该对其他版本的Ubuntu同样适用. 2010-09-01: Ubuntu ppa没有karmic上的openswan包, 只能自己编译了, 或是下载我预编译的版本, 参照后面说明. 1. 安装OpenSWAN L2TP(Layer 2 Tunneling Protocol)顾名思义, 是2层隧道协议. 这个协议的认证方式不是非常安全, 因此实际使用中, 往往将L2TP和IPsec结合, 客户端和服务器之间, 首先通过IPsec生成安全信道, 之后再进行L2TP协议的交互. 因此, 安装L2TP服务器, 首先需要安装IPsec软件. 目前在Linux下, 有FreeSWAN, OpenSWAN, StrongSWAN三款IPsec协议的实现. 这里我用的是OpenSWAN. 目前Ubuntu Lucid中官方自带的OpenSWAN版本是2.6.23. 可惜的是, 这个版本的OpenSWAN有bug, 无法和L2TP服务器配合使用, 必须至少升级到2.6.24以后. 因此需要安装Ubuntu提供的非官方OpenSWAN升级版. Ubuntu Karmic 9.10 安装过程 ppa中没有Ubuntu Karmic的openswan, 安装过程有不一样. 首先下载我预编译的openswan的deb包, 然后安装: sudo dpkg -i [...]]]></description>
			<content:encoded><![CDATA[<p>最近用VPN翻墙比较流行, 我也赶了一下潮流: 买了一个<a href="http://www.linode.com/?r=44cf208b8ccc0279cecdc4b9f4cdbfdc6f497a9f">Linode的VPS</a>, 安装了最新的Ubuntu Lucid (10.04), 并在上面配置了L2TP服务器.</p>
<p>以下虽然是在10.04上的配置过程, 但应该对其他版本的Ubuntu同样适用.</p>
<p><strong>2010-09-01</strong>: Ubuntu ppa没有karmic上的openswan包, 只能自己编译了, 或是下载我预编译的版本, 参照后面说明.</p>
<h4>1. 安装OpenSWAN</h4>
<p>L2TP(<a href="http://en.wikipedia.org/wiki/Layer_2_Tunneling_Protocol">Layer 2 Tunneling Protocol</a>)顾名思义, 是2层隧道协议. 这个协议的认证方式不是非常安全, 因此实际使用中, 往往将L2TP和IPsec结合, 客户端和服务器之间, 首先通过IPsec生成安全信道, 之后再进行L2TP协议的交互.</p>
<p>因此, 安装L2TP服务器, 首先需要安装IPsec软件. 目前在Linux下, 有FreeSWAN, OpenSWAN, StrongSWAN三款IPsec协议的实现. 这里我用的是<a href="http://www.openswan.org/">OpenSWAN</a>.</p>
<p>目前Ubuntu Lucid中官方自带的OpenSWAN版本是<a href="http://packages.ubuntu.com/lucid/openswan">2.6.23</a>. 可惜的是, 这个版本的OpenSWAN有bug, 无法和L2TP服务器配合使用, 必须<a href="http://lists.openswan.org/pipermail/announce/2010-January/000036.html">至少升级到2.6.24以后</a>. 因此需要安装Ubuntu提供的非官方OpenSWAN升级版.   <span id="more-841"></span>  </p>
<h5>Ubuntu Karmic 9.10 安装过程</h5>
<p>ppa中没有Ubuntu Karmic的openswan, 安装过程有不一样. 首先下载我预编译的openswan的deb包, 然后安装: Note: There is a file embedded within this post, please visit this post to download the file.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">dpkg</span> <span style="color: #660033;">-i</span> openswan<span style="color: #000000; font-weight: bold;">*</span>i386.deb
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> <span style="color: #660033;">-f</span></pre></div></div>

<p>安装完成后请直接跳过到步骤2: 配置OpenSWAN.</p>
<h5>1.1. 首先添加非官方的OpenSWAN apt源:</h5>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> python-software-properties
<span style="color: #c20cb9; font-weight: bold;">sudo</span> add-apt-repository ppa:openswan<span style="color: #000000; font-weight: bold;">/</span>ppa
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> update</pre></div></div>

<h5>1.2. 然后检查OpenSWAN的版本, 至少应该是2.6.24</h5>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># apt-cache policy openswan</span>
openswan:
  Installed: <span style="color: #7a0874; font-weight: bold;">&#40;</span>none<span style="color: #7a0874; font-weight: bold;">&#41;</span>
  Candidate: <span style="color: #000000;">1</span>:2.6.28-1xelerance3
  Version table:
     <span style="color: #000000;">1</span>:2.6.28-1xelerance3 <span style="color: #000000;">0</span>
        <span style="color: #000000;">500</span> http:<span style="color: #000000; font-weight: bold;">//</span>ppa.launchpad.net<span style="color: #000000; font-weight: bold;">/</span>openswan<span style="color: #000000; font-weight: bold;">/</span>ppa<span style="color: #000000; font-weight: bold;">/</span>ubuntu<span style="color: #000000; font-weight: bold;">/</span> lucid<span style="color: #000000; font-weight: bold;">/</span>main Packages</pre></div></div>

<h5>1.3. 安装</h5>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> openswan</pre></div></div>

<p>安装过程中如问到: Use an X.509 certificate for this host, 回答NO.</p>
<h4>2. 配置OpenSWAN</h4>
<h5>2.0. 设置命令行变量</h5>
<p>在后面的配置过程中, 有一些配置参数可能需要修改. 本文假设L2TP服务器的IP地址是192.168.1.1, VPN网络前缀是10.1.1.0/24, 用户名为user, 密码为1234. 在完成下面的配置之前, 应该首先把这些参数修改成正确的参数. 后面的命令将使用bash的变量替换规则吧配置文件中出现的参数名替换出下面设置的值:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">SERVER</span>=192.168.1.1
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">NET</span>=10.1.1
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">USER1</span>=user
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PASS1</span>=<span style="color: #000000;">1234</span></pre></div></div>

<h5>2.1. 配置sysctl参数</h5>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>sysctl.conf <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>sysctl.conf.bak
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">bash</span> <span style="color: #660033;">-c</span> <span style="color: #ff0000;">&quot;cat &gt; /etc/sysctl.conf &lt;&lt;EOF
net.ipv4.ip_forward=1
net.ipv4.conf.all.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
EOF
&quot;</span></pre></div></div>

<p>上述参数重启后生效, 如果想不重启即时生效, 需要继续完成步骤2:</p>
<h5>2.2. 加载sysctl参数</h5>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> sysctl <span style="color: #660033;">-p</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">bash</span> <span style="color: #660033;">-c</span> <span style="color: #ff0000;">'for each in /proc/sys/net/ipv4/conf/*
do
    echo 0 &gt; $each/accept_redirects
    echo 0 &gt; $each/send_redirects
done
'</span></pre></div></div>

<h5>2.3. 生成iptables配置</h5>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>rc.local <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>rc.local.bak
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">bash</span> <span style="color: #660033;">-c</span> <span style="color: #ff0000;">&quot;cat &gt; /etc/rc.local &lt;&lt;EOF
#!/bin/sh -e
iptables -t nat -A POSTROUTING -s <span style="color: #007800;">$NET</span>.0/24 -o eth0 -j MASQUERADE
exit 0
EOF
&quot;</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chmod</span> +x <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>rc.local</pre></div></div>

<p>类似的, 上述参数重启后生效. 如果想不重启即时生效, 需要继续完成步骤4:</p>
<p><strong>注意:</strong> /etc/rc.local文件中引用了$NET参数的值, 检查一下文件输出是否符合预期.</p>
<h5>2.4. 加载iptables配置</h5>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>rc.local</pre></div></div>

<h5>2.5. 配置ipsec.secrets</h5>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>ipsec.secrets <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>ipsec.secrets.bak
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">bash</span> <span style="color: #660033;">-c</span> <span style="color: #ff0000;">&quot;cat &gt; /etc/ipsec.secrets &lt;&lt;EOF
<span style="color: #007800;">$SERVER</span> %any: PSK <span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #007800;">$PASS1</span><span style="color: #000099; font-weight: bold;">\&quot;</span>
EOF
&quot;</span></pre></div></div>

<p><strong>注意:</strong> /etc/ipsec.secrets文件中引用了$SERVER, $PASS1参数的值, 检查一下文件输出是否符合预期.</p>
<h5>2.6. 配置ipsec.conf</h5>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>ipsec.conf <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>ipsec.conf.bak
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">bash</span> <span style="color: #660033;">-c</span> <span style="color: #ff0000;">&quot;cat &gt; /etc/ipsec.conf &lt;&lt;EOF
version 2.0
config setup
    nat_traversal=yes
    virtual_private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12
    oe=off
    protostack=netkey
&nbsp;
conn L2TP-PSK-NAT
    rightsubnet=vhost:%priv
    also=L2TP-PSK-noNAT
&nbsp;
conn L2TP-PSK-noNAT
    authby=secret
    pfs=no
    auto=add
    keyingtries=3
    rekey=no
    ikelifetime=8h
    keylife=1h
    type=transport
    left=<span style="color: #007800;">$SERVER</span>
    leftprotoport=17/1701
    right=%any
    rightprotoport=17/%any
EOF
&quot;</span></pre></div></div>

<p><strong>注意:</strong> /etc/ipsec.conf文件中引用了$SERVER参数的值, 检查一下文件输出是否符合预期.</p>
<h5>2.7. 激活ipsec服务</h5>
<p>运行下面的命令, 让ipsec服务可以开机自动启动: </p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> update-rc.d ipsec defaults</pre></div></div>

<h4>3. 安装xl2tpd</h4>
<p>L2TP服务器软件, 选择与OpenSWAN同样是xelerance公司支持的xl2tpd软件. 该软件可以直接用Ubuntu自带的版本, 安装过程相对简单:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> xl2tpd</pre></div></div>

<h4>4. 配置xl2tpd</h4>
<h5>4.0. 设置命令行变量</h5>
<p>配置xl2tpd的过程中同样需要替换配置文件中的部分参数, 参数和OpenSWAN命令行变量一样.</p>
<h5>4.1. 配置xl2tpd.conf</h5>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>xl2tpd<span style="color: #000000; font-weight: bold;">/</span>xl2tpd.conf <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>xl2tpd<span style="color: #000000; font-weight: bold;">/</span>xl2tpd.conf.bak
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">bash</span> <span style="color: #660033;">-c</span> <span style="color: #ff0000;">&quot;cat &gt; /etc/xl2tpd/xl2tpd.conf &lt;&lt;EOF
[global]
ipsec saref = yes
&nbsp;
[lns default]
ip range = <span style="color: #007800;">$NET</span>.2-<span style="color: #007800;">$NET</span>.255
local ip = <span style="color: #007800;">$NET</span>.1
refuse chap = yes
refuse pap = yes
require authentication = yes
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes
EOF
&quot;</span></pre></div></div>

<p><strong>注意:</strong> /etc/xl2tpd/xl2tpd.conf文件中引用了$NET参数的值, 检查一下文件输出是否符合预期.</p>
<h5>4.2. 配置options.xl2tpd</h5>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>ppp<span style="color: #000000; font-weight: bold;">/</span>options.xl2tpd <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>ppp<span style="color: #000000; font-weight: bold;">/</span>options.xl2tpd.bak
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">bash</span> <span style="color: #660033;">-c</span> <span style="color: #ff0000;">&quot;cat &gt; /etc/ppp/options.xl2tpd &lt;&lt;EOF
require-mschap-v2
ms-dns 8.8.8.8
ms-dns 8.8.4.4
asyncmap 0
auth
crtscts
lock
hide-password
modem
debug
name l2tpd
proxyarp
lcp-echo-interval 30
lcp-echo-failure 4
EOF
&quot;</span></pre></div></div>

<h5>4.3. 配置chap-secrets</h5>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>ppp<span style="color: #000000; font-weight: bold;">/</span>chap-secrets <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>ppp<span style="color: #000000; font-weight: bold;">/</span>chap-secrets.bak
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">bash</span> <span style="color: #660033;">-c</span> <span style="color: #ff0000;">&quot;cat &gt; /etc/ppp/chap-secrets &lt;&lt;EOF
# user      server      password    ip
<span style="color: #007800;">$USER1</span>       l2tpd       <span style="color: #007800;">$PASS1</span>       *
EOF
&quot;</span></pre></div></div>

<p><strong>注意:</strong> /etc/ppp/chap-secrets文件中引用了$USER1, $PASS1参数的值, 检查一下文件输出是否符合预期.</p>
<h4>5. 启动L2TP服务器</h4>
<p>至此, L2TP/IPsec服务器配置完成. 重启之后, 就可以使用了. 如果不希望重启, 可以运行下面的命令, 手动启动服务:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> invoke-rc.d xl2tpd restart
<span style="color: #c20cb9; font-weight: bold;">sudo</span> invoke-rc.d ipsec restart</pre></div></div>

<p>运行ipsec verify命令, 验证IPsec服务器的运行状态. 典型的检查结果如下: </p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># sudo ipsec verify</span>
Checking your system to see <span style="color: #000000; font-weight: bold;">if</span> IPsec got installed and started correctly:
Version check and ipsec on-path                             	<span style="color: #7a0874; font-weight: bold;">&#91;</span>OK<span style="color: #7a0874; font-weight: bold;">&#93;</span>
Linux Openswan U2.6.28<span style="color: #000000; font-weight: bold;">/</span>K2.6.32.16-linode28 <span style="color: #7a0874; font-weight: bold;">&#40;</span>netkey<span style="color: #7a0874; font-weight: bold;">&#41;</span>
Checking <span style="color: #000000; font-weight: bold;">for</span> IPsec support <span style="color: #000000; font-weight: bold;">in</span> kernel                        	<span style="color: #7a0874; font-weight: bold;">&#91;</span>OK<span style="color: #7a0874; font-weight: bold;">&#93;</span>
NETKEY detected, testing <span style="color: #000000; font-weight: bold;">for</span> disabled ICMP send_redirects   	<span style="color: #7a0874; font-weight: bold;">&#91;</span>OK<span style="color: #7a0874; font-weight: bold;">&#93;</span>
NETKEY detected, testing <span style="color: #000000; font-weight: bold;">for</span> disabled ICMP accept_redirects 	<span style="color: #7a0874; font-weight: bold;">&#91;</span>OK<span style="color: #7a0874; font-weight: bold;">&#93;</span>
Checking that pluto is running                              	<span style="color: #7a0874; font-weight: bold;">&#91;</span>OK<span style="color: #7a0874; font-weight: bold;">&#93;</span>
Pluto listening <span style="color: #000000; font-weight: bold;">for</span> IKE on udp <span style="color: #000000;">500</span>                          	<span style="color: #7a0874; font-weight: bold;">&#91;</span>OK<span style="color: #7a0874; font-weight: bold;">&#93;</span>
Pluto listening <span style="color: #000000; font-weight: bold;">for</span> NAT-T on udp <span style="color: #000000;">4500</span>                       	<span style="color: #7a0874; font-weight: bold;">&#91;</span>OK<span style="color: #7a0874; font-weight: bold;">&#93;</span>
Two or <span style="color: #c20cb9; font-weight: bold;">more</span> interfaces found, checking IP forwarding        	<span style="color: #7a0874; font-weight: bold;">&#91;</span>OK<span style="color: #7a0874; font-weight: bold;">&#93;</span>
Checking NAT and MASQUERADEing                              
Checking <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #ff0000;">'ip'</span> <span style="color: #7a0874; font-weight: bold;">command</span>                                   	<span style="color: #7a0874; font-weight: bold;">&#91;</span>OK<span style="color: #7a0874; font-weight: bold;">&#93;</span>
Checking <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #ff0000;">'iptables'</span> <span style="color: #7a0874; font-weight: bold;">command</span>                             	<span style="color: #7a0874; font-weight: bold;">&#91;</span>OK<span style="color: #7a0874; font-weight: bold;">&#93;</span>
Opportunistic Encryption Support                            	<span style="color: #7a0874; font-weight: bold;">&#91;</span>DISABLED<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<h4>6. 测试L2TP服务器</h4>
<p>完成前面的步骤之后, 就可以用iPhone添加L2TP VPN连接, 测试L2TP服务器的有效性了. 按照本文的配置参数, 服务器的IP地址是192.168.1.1, 用户名是user, L2TP的密码和IPsec的预共享密钥都是1234.</p>
]]></content:encoded>
			<wfw:commentRss>http://dipplum.com/2010/08/30/ubuntu-install-l2tp-server/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Bambook内测体验</title>
		<link>http://dipplum.com/2010/08/19/bambook-first-impression/</link>
		<comments>http://dipplum.com/2010/08/19/bambook-first-impression/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 06:27:57 +0000</pubDate>
		<dc:creator>li</dc:creator>
				<category><![CDATA[数码产品]]></category>
		<category><![CDATA[bambook]]></category>
		<category><![CDATA[ebook]]></category>
		<category><![CDATA[kindle]]></category>
		<category><![CDATA[qidian]]></category>
		<category><![CDATA[snda]]></category>

		<guid isPermaLink="false">http://dipplum.com/2010/08/19/bambook-first-impression/</guid>
		<description><![CDATA[前几天在起点的官方网站上看到Bambook的内测消息, 看看自己的帐号好像还符合盛大对内测用户的资质要求: &#34;注册一年以上, 余额至少50元&#34;, 于是就申请了Bambook的内测. 据说这次Bambook的内测只发3500个内测邀请, 因此申请了以后我也没在意, 总认为人会很多, 肯定抽不上. 8月6号申请之后, 也一直关注邮箱和Bambook的官方网站, 看到盛大9号已经开始发送邀请码了, 而我直到12号也没收到, 因此更加放弃了. 阴差阳错, 我12号中午整理Gmail的垃圾邮件箱时, 翻出了Bambook的邀请码. 怎么就进垃圾邮件箱了呢, 也不知道多少Gmail用户没有收到邀请码. 根据盛大后来修改的规定, 邀请码24小时有效, 但是我的邀请信是9号发出的, 貌似已经过期了. 不过, 我抱着试一试的心态, 用&#34;过期&#34;的邀请码竟然还可以下单, 就这样上了贼船. 1. 网上的评价 下单之后关注了一下Bambook官方论坛的内容, 结果有一点失望. 另一方面, 一些内测用户在官方论坛对Bambook的产品质量和宣传手法提出质疑; 另一方面正赶上Bambook论坛改版, 把用户意见全隐藏了, 给人感觉很不好. 对Bambook大部分的质疑都可以从这里看到: 主要是怀疑Bambook内测时间过于仓促, 似乎是在比着Amazon的Kindle 3发布, 而产品功能还没有完善; 而Bambook的广告词却给人相当的误导, 例如太阳能充电模块、3G模块, 内测时并不随机附送, 对电子书格式(ePub, PDF)的支持相当初级等等. 无论如何, 既然下单了, 那就眼见为实好了. 2. 物流 Bambook的配送过程比较让人失望, 我12号中午下单, 17号早上才收到: &#34;订单审核&#34;1天, [...]]]></description>
			<content:encoded><![CDATA[<p>前几天在起点的官方网站上看到Bambook的内测消息, 看看自己的帐号好像还符合盛大对内测用户的资质要求: &quot;注册一年以上, 余额至少50元&quot;, 于是就申请了Bambook的内测. </p>
<p>据说这次Bambook的内测只发3500个内测邀请, 因此申请了以后我也没在意, 总认为人会很多, 肯定抽不上. 8月6号申请之后, 也一直关注邮箱和<a href="http://bambook.sdo.com">Bambook的官方网站</a>, 看到盛大9号已经开始发送邀请码了, 而我直到12号也没收到, 因此更加放弃了. </p>
<p>阴差阳错, 我12号中午整理Gmail的垃圾邮件箱时, 翻出了Bambook的邀请码. 怎么就进垃圾邮件箱了呢, 也不知道多少Gmail用户没有收到邀请码. 根据盛大后来修改的规定, 邀请码24小时有效, 但是我的邀请信是9号发出的, 貌似已经过期了. </p>
<p>不过, 我抱着试一试的心态, 用&quot;过期&quot;的邀请码竟然还可以下单, 就这样上了贼船.<span id="more-838"></span></p>
<h4>1. 网上的评价</h4>
<p>下单之后关注了一下<a href="http://bambookbbs.sdo.com">Bambook官方论坛</a>的内容, 结果有一点失望. 另一方面, 一些内测用户在官方论坛对Bambook的产品质量和宣传手法提出质疑; 另一方面正赶上Bambook论坛改版, 把用户意见全隐藏了, 给人感觉很不好.</p>
<p>对Bambook大部分的质疑都可以从<a href="http://bit.ly/baTzqL">这里</a>看到: 主要是怀疑Bambook内测时间过于仓促, 似乎是在比着Amazon的Kindle 3发布, 而产品功能还没有完善; 而Bambook的广告词却给人相当的误导, 例如太阳能充电模块、3G模块, 内测时并不随机附送, 对电子书格式(ePub, PDF)的支持相当初级等等. </p>
<p>无论如何, 既然下单了, 那就眼见为实好了.</p>
<h4>2. 物流</h4>
<p>Bambook的配送过程比较让人失望, 我12号中午下单, 17号早上才收到: &quot;订单审核&quot;1天, &quot;个性化配置&quot;1天, &quot;配送&quot;花了1天, 就连到了小区也要等1天半快2天. 对比淘宝商家的发货速度, 这样的Case最多2天就能搞定.</p>
<h4>3. 包装和外观</h4>
<p align="center"><a href="http://dipplum.com/files/2010/08/bambookpackage.jpg" rel="shadowbox[sbpost-838];player=img;"><img style="border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px" title="bambook-package" border="0" alt="bambook-package" src="http://dipplum.com/files/2010/08/bambookpackage_thumb.jpg" width="164" height="244" /></a><a href="http://dipplum.com/files/2010/08/bambookfront.jpg" rel="shadowbox[sbpost-838];player=img;"><img style="border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px" title="bambook-front" border="0" alt="bambook-front" src="http://dipplum.com/files/2010/08/bambookfront_thumb.jpg" width="164" height="244" /></a><a href="http://dipplum.com/files/2010/08/bambookback.jpg" rel="shadowbox[sbpost-838];player=img;"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="bambook-back" border="0" alt="bambook-back" src="http://dipplum.com/files/2010/08/bambookback_thumb.jpg" width="164" height="244" /></a>&#160;&#160; </p>
<p>Bambook的盒子设计很典雅, 并没有因为内测就随便整一个包装盒. Bambook的整体造型很像Kindle 2, 大小也差不多, 这两款产品的皮套都可以通用. 不过新的Kindle 3又要比Kindle 2小不少, 比Bambook有一定优势.</p>
<p>Bambook键盘是像手机那样的数字小键盘, 不是用Kindle那样的全键盘. 窃以为这个设计很中国很合理, 符合中文用户的要求: 如果是输入中文, 按键大且手感好的小键盘输入速度不一定比按键都挤在一起的全键盘慢.</p>
<p>不过Bambook的按键手感还是不佳, 涩涩的, 还不如大部分手机的键盘手感好. 前几天有幸摸了一下Kindle 2, 除了按键超级小之外, 手感也是不咋地. 键盘手感差不知道是不是电子阅读器的通病.</p>
<h4>4. E-ink屏幕</h4>
<p>Bambook用的是6吋的E-ink屏幕, 和Kindle 1/2/3一般大. 看多了背光屏, 再看自然光照明的屏幕反而有虚幻的感觉. 虽然屏幕看起来确实很柔和, 不刺眼, 但Bambook的手感和纸质书差的还是很远的. 总之, 在使用过程中, 你绝不会忘了手上拿的是一台计算机, 而不是一本书. 一方面是前面提到的键盘和外壳的手感, 没有拿书、翻书那样举重若轻的感觉; 另一方面则是E-ink翻页的感觉.</p>
<p>相比背光屏, E-ink屏有一个缺点: 屏幕刷新很慢, 翻页需要约1秒钟来刷新屏幕, 期间还可能会有闪屏(全黑/全白闪烁). Bambook自然也有这个问题, 但也有不小的改进. Bambook提供两种阅读模式: 质量优先、速度优先. 顾名思义, &quot;速度优先&quot;模式下牺牲了阅读质量, 据我观察, 主要是关闭了字体抗锯齿效果, 增加了笔画的颗粒感, 其提速效果也是很明显的.</p>
<p>虽然Bambook的翻页速度经过优化有不少的提升, 应该说也不弱于其他的电子书阅读器. 但遗憾的是, 以我自己的观感, 即使在&quot;速度优先&quot;模式下, 翻页等待时间依然会影响阅读的流畅性.</p>
<p>另外, 6吋屏还是太小, 只有32开纸的一半不到. 所以Bambook只适合看些纯文本的小说. 图文混排的书, 或是预先排版好的文档, 用Bambook看不太适合. 毕竟Bambook的屏幕分辨率只有800&#215;600. 如果要看教材或是论文这种类型的电子书, 尤其是大量扫描的PDF, 至少也要用9.7吋的Kindle DX, 其实9.7吋屏也就才将将32开纸那么大, 看那些16开的书也挺费劲的.</p>
<h4>5. Bambook软件</h4>
<p>Bambook中的操作系统基于Google Android手机操作系统, 而Anroid内核是基于Linux的. 因此, Bambook用到了一部分开源的代码, 包括使用GPL协议的Linux内核及相关GNU软件的开源代码. 不过目前Bambook暂时还没有公布这部分GPL代码, <a href="http://bambookbbs.sdo.com/show.aspx?id=2074&amp;page=14">据说</a>最近将会公布.</p>
<p>Bambook软件部分还比较简陋, 除了最核心的看书、买书功能之外, 啥功能都缺: 字典、笔记、浏览器, 等等. 这点还是与Kindle有比较大的差距. 不过好在还比较稳定, 用了两天没有遇到死机和显而易见的Bug. </p>
<p>软件的易用性也有较大的改善空间. 例如:</p>
<p>1. 如果长时间不访问网络, Bambook会自动关闭Wifi模块以降低耗电量. 这本来是很好的设计, 但Bambook可以软件关闭Wifi模块, 却不能软件开启, 必须用户手动开关一次面板上的Wifi开关, 才能重新激活无线, 这就有点傻了. </p>
<p>2. 书架的查书功能也不太实用, 100多本书只能按照更新的先后顺序一页一页翻着找, 实在是不太方便. 不过在起点订阅VIP章节的用户或许有这种排序方法就足够了. </p>
<p>3. 还有就是字体的设置. Bambook可以更换字体, 这个设计对于不喜欢Bambook默认自带的黑体字的人来说是一个福音. 我还是比较习惯黑体的. 字体可以调整大小, 不过对我来说, 最小的字体都嫌大, 一页没几个字, 需要频繁翻页. 与Kindle不同, Bambook目前没有公布平台的SDK, 也<a href="http://bambookbbs.sdo.com/show.aspx?id=2074&amp;page=13">没有公布的计划</a>, 因此不会有什么第三方扩展可以用.</p>
<h4>6. 盛大云梯 </h4>
<p>Bambook本身自带Wifi模块, 其实接不接电脑完全没关系. 不过盛大还是提供了Bambook的PC端软件: 盛大云梯.&#160; 盛大云梯包含两个部分: Bambook格式的电子书制作工具, 和Bambook书籍管理.</p>
<p>Bambook的电子书格式的后缀名是snb, 估计是盛大自己设计的一种格式, 除此之外, Bambook不支持其他任何电子书格式. 是的, 包括TXT, PDF, Bambook都不能直接阅读. 云梯中自带的制作工具可以将常用的电子书格式转换成snb格式, 但是除了txt之外, 其他格式的转换还不尽如人意. 其实, 像pdf, epub这种格式的电子书, 就Bambook就应该直接支持, 而不是转一道手再支持.</p>
<p>Bambook的PC客户端的书籍管理功能还比较简单, 基本上所有的管理功能在Bambook上都可以直接完成, 除了一项: 自制书籍上传. 自制书籍就是用前面提到的制作工具生成的snb格式的电子书, 这些文件不能够直接拷贝到Bambook上, 因为Bambook接上电脑以后, 并不会认成一个USB磁盘, 所以只能通过云梯客户端上传到Bambook.</p>
<h4>7. 与起点的集成</h4>
<p>与起点中文网的集成, 是Bambook最值得称道的特点. 应该说Bambook对盈利模式还是有所考虑, 并能贯彻始终且体现在产品的功能上的. 前面提到Bambook一切多余功能都欠奉, 但与起点的集成度确实非常高.</p>
<p>Bambook在下单购买的时候, 就可以绑定盛大的帐号. 一旦绑定了盛大帐号, Bambook就会自动将用户书架中所有的书, 包括已经购买了的VIP章节的内容, 都同步到Bambook上. 用户可以在起点上完成的所有互动, 几乎都能用Bambook完成: 比如投月票, 推荐票, 比如写书评, 看书评等等. 为此, Bambook面板上甚至专门设置了一个&quot;互动&quot;键.</p>
<p>据我的观察, Bambook上保存的应该是VIP章节的文本信息, 而不是图片, 否则占用的空间不会这么小, 也不应该能能在不连网的时候更换字体和调节字体大小. 因此, 如果Bambook与服务器的通信方式一旦被破解, 或者Bambook的存储卡内容被破解, 用户将有可能直接获取VIP章节的文字, 给盗版造成极大的便利. 不知道盛大在这方面加密的力度有多大, 有没有采用Amazon在azw格式中使用的DRM机制. 不过我猜测是没有, 不然Bambook上的闪存设备不会羞羞答答的不让用户访问.</p>
<h4>总结 </h4>
<p>罗嗦了这么多, 下面总结下我的大体感受.</p>
<p>首先, 作为一款&quot;内测&quot;产品, Bambook的质量还算过得去, 不太让人失望, 却也并没有给人耳目一新的感觉. </p>
<p>我认为从产品设计来看, Bambook最大的优点的目标用户群定位非常明确的: 就是起点VIP订阅的死忠用户. Bambook的核心功能也都是针对这一用户群的. 因此Bambook的盈利模式和市场份额也是不用担心的: 只要起点VIP用户有使用电子阅读器的需求, Bambook就会是他的最佳选择, 没有之一. 短期内不会有其他公司能够在盈利模式和目标用户群上能够威胁Bambook.</p>
<p>对于没有在起点上看书消费习惯和意愿的用户来说, 买Bambook就纯属多余了. Bambook对其他电子书格式的支持不完善, 看自制书籍也不是很方便. 唯一的优点就是价格. 不算税的话, 只比不带3G模块的Kindle 3/3G版略贵. 只是不知道正式发行的时候, 还能不能保持998元的价位.</p>
]]></content:encoded>
			<wfw:commentRss>http://dipplum.com/2010/08/19/bambook-first-impression/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Python调用搜狗云输入法的小例子</title>
		<link>http://dipplum.com/2010/01/22/sogou-cloud-ime-python/</link>
		<comments>http://dipplum.com/2010/01/22/sogou-cloud-ime-python/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 17:52:20 +0000</pubDate>
		<dc:creator>li</dc:creator>
				<category><![CDATA[中文化]]></category>
		<category><![CDATA[cloud]]></category>
		<category><![CDATA[ime]]></category>
		<category><![CDATA[pinyin]]></category>
		<category><![CDATA[sogou]]></category>

		<guid isPermaLink="false">http://dipplum.com/2010/01/22/sogou-cloud-ime-python/</guid>
		<description><![CDATA[写了个Python的小例子，调用搜狗云输入法获取汉字。感谢xzap在搜狗论坛发布的bash脚本，但觉得bash还是略微晦涩了点，用python说明搜狗云输入法的API更加清晰一点。 #!/usr/bin/python from urllib import urlopen from cgi import parse_qs &#160; url = 'http://web.pinyin.sogou.com/web_ime/get_ajax/%s.key' &#160; py = raw_input&#40;'input pinyin: '&#41; res = urlopen&#40;url % py&#41;.read&#40;&#41; dic = parse_qs&#40;res&#41; lst = eval&#40;dic&#91;'ime_query_res'&#93;&#91;0&#93;&#41;.split&#40;'\t '&#41; words = map&#40;lambda x: x.split&#40;'\xef\xbc\x9a'&#41;&#91;0&#93;, lst&#41; counts = map&#40;lambda x: x.split&#40;'\xef\xbc\x9a'&#41;&#91;1&#93;, lst&#41; &#160; for i in range&#40;len&#40;words&#41;&#41;: print words&#91;i&#93;, counts&#91;i&#93; &#160; exit 运行该脚本的输出结果如下，每行前面是候选词/句，后面是该候选词/句消耗的英文单词字母个数： [...]]]></description>
			<content:encoded><![CDATA[<p>写了个Python的小例子，调用搜狗云输入法获取汉字。感谢xzap在搜狗论坛发布的<a href="http://pinyin.sogou.com/bbs/redirect.php?tid=140527&amp;goto=lastpost" target="_blank">bash脚本</a>，但觉得bash还是略微晦涩了点，用python说明搜狗云输入法的API更加清晰一点。</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python                                                                                                                                           </span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">urllib</span> <span style="color: #ff7700;font-weight:bold;">import</span> urlopen
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">cgi</span> <span style="color: #ff7700;font-weight:bold;">import</span> parse_qs
&nbsp;
url = <span style="color: #483d8b;">'http://web.pinyin.sogou.com/web_ime/get_ajax/%s.key'</span>
&nbsp;
py = <span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'input pinyin: '</span><span style="color: black;">&#41;</span>
res = urlopen<span style="color: black;">&#40;</span>url <span style="color: #66cc66;">%</span> py<span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
dic = parse_qs<span style="color: black;">&#40;</span>res<span style="color: black;">&#41;</span>
lst = <span style="color: #008000;">eval</span><span style="color: black;">&#40;</span>dic<span style="color: black;">&#91;</span><span style="color: #483d8b;">'ime_query_res'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\t</span> '</span><span style="color: black;">&#41;</span>
words = <span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x: x.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\x</span>ef<span style="color: #000099; font-weight: bold;">\x</span>bc<span style="color: #000099; font-weight: bold;">\x</span>9a'</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, lst<span style="color: black;">&#41;</span>
counts = <span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x: x.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\x</span>ef<span style="color: #000099; font-weight: bold;">\x</span>bc<span style="color: #000099; font-weight: bold;">\x</span>9a'</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, lst<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>words<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> words<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>, counts<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>
&nbsp;
exit</pre></div></div>

<p>运行该脚本的输出结果如下，每行前面是候选词/句，后面是该候选词/句消耗的英文<del datetime="2010-07-02T08:58:07+00:00">单词</del>字母个数：</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>sogou.py
input pinyin: nihaosgysrf
你好搜狗云输入法 <span style="color: #000000;">11</span>
你好是 <span style="color: #000000;">6</span>
倪豪士 <span style="color: #000000;">6</span>
你好 <span style="color: #000000;">5</span>
拟好 <span style="color: #000000;">5</span>
倪浩 <span style="color: #000000;">5</span>
泥蒿 <span style="color: #000000;">5</span>
倪昊 <span style="color: #000000;">5</span>
倪皓 <span style="color: #000000;">5</span>
匿号 <span style="color: #000000;">5</span>
你 <span style="color: #000000;">2</span>
拟 <span style="color: #000000;">2</span>
呢 <span style="color: #000000;">2</span>
尼 <span style="color: #000000;">2</span>
泥 <span style="color: #000000;">2</span>
逆 <span style="color: #000000;">2</span>
妮 <span style="color: #000000;">2</span>
腻 <span style="color: #000000;">2</span>
倪 <span style="color: #000000;">2</span>
伱 <span style="color: #000000;">2</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://dipplum.com/2010/01/22/sogou-cloud-ime-python/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

