<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[实践OK]php中的ssh2模块学习及直接通过读取id_rsa认证读取公钥的脚本的方法。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Mon, 29 Nov 2010 04:49:57 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	http://blog.sina.com.cn/s/blog_48c95a1901018nq1.html<br/><br/>libssh2：（configure: error: The required libssh2 library was not found.&nbsp;&nbsp;You can obtain that package from http://sourceforge.net/projects/libssh2/，可通过yum install libssh2-devel.x86_64 解决，不用下源码包）<br/>https://www.libssh2.org/download/<br/><br/>ssh2：<br/>http://pecl.php.net/package/ssh2<br/><br/>读取公钥的脚本：<br/><textarea name="code" class="php" rows="15" cols="100">
#!/usr/bin/php
&lt;?php
$server = [&quot;192.168.1.2&quot;, &quot;192.168.1.20&quot;, &quot;192.168.1.200&quot;];
 
$server = array_map(function($k)&#123;
 
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;%s%s%s%s&quot;, str_repeat(&quot;=&quot;, 30), $k, str_repeat(&quot;=&quot;, 30), PHP_EOL);
 
&nbsp;&nbsp;&nbsp;&nbsp;$connection = ssh2_connect($k, 22);
 
&nbsp;&nbsp;&nbsp;&nbsp;$sshDir = $_SERVER[&quot;HOME&quot;] . &quot;/.ssh/&quot;;
 
&nbsp;&nbsp;&nbsp;&nbsp;ssh2_auth_pubkey_file($connection, &#039;shuhai&#039;, $sshDir.&#039;id_rsa.pub&#039;, $sshDir.&#039;id_rsa&#039;);
 
 
&nbsp;&nbsp;&nbsp;&nbsp;ssh2_exec($connection, &#039;echo &quot;eoe!@#admin&quot; &#124; sudo -s&#039;);
 
&nbsp;&nbsp;&nbsp;&nbsp;$stream = ssh2_exec($connection, &#039;whoami&#039;);
&nbsp;&nbsp;&nbsp;&nbsp;stream_set_blocking( $stream, true );
&nbsp;&nbsp;&nbsp;&nbsp;echo stream_get_contents($stream);
 
&nbsp;&nbsp;&nbsp;&nbsp;return;
 
&nbsp;&nbsp;&nbsp;&nbsp;$stream = ssh2_exec($connection, &#039;php -v&#039;);
&nbsp;&nbsp;&nbsp;&nbsp;stream_set_blocking( $stream, true );
&nbsp;&nbsp;&nbsp;&nbsp;echo stream_get_contents($stream);
 
&nbsp;&nbsp;&nbsp;&nbsp;$stream = ssh2_exec($connection, &#039;mysql --version&#039;);
&nbsp;&nbsp;&nbsp;&nbsp;stream_set_blocking( $stream, true );
&nbsp;&nbsp;&nbsp;&nbsp;echo stream_get_contents($stream);
 
&#125;, $server);
</textarea><br/><br/>摘自：http://www.4wei.cn/archives/1002295<br/><br/>php中的ssh2模块学习<br/><br/>1、&nbsp;&nbsp;前言<br/>为了能够让管理员直接选择好若干主机及机房然后直接将命令推送到各个主机上去执行。现整理了一下php里面的ssh2模块。方便使用。<br/>2、&nbsp;&nbsp;完整的类代码如下<br/>&lt;?php<br/>// ssh protocols<br/>// note: once openShell method is used, cmdExec does not work<br/> class ssh2 &#123;<br/>&nbsp;&nbsp; private $host = &#039;host&#039;;<br/>&nbsp;&nbsp;private $user = &#039;user&#039;;<br/>&nbsp;&nbsp;private $port = &#039;22&#039;;<br/>&nbsp;&nbsp;private $password = &#039;password&#039;;<br/>&nbsp;&nbsp;private $con = null;<br/>&nbsp;&nbsp;private $shell_type = &#039;xterm&#039;;<br/>&nbsp;&nbsp;private $shell = null;<br/>&nbsp;&nbsp;private $log = &#039;&#039;;<br/>&nbsp;&nbsp;function __construct($host=&#039;&#039;, $port=&#039;&#039;&nbsp;&nbsp;) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp; if( $host!=&#039;&#039; ) $this-&gt;host&nbsp;&nbsp;= $host;<br/>&nbsp;&nbsp;&nbsp;&nbsp; if( $port!=&#039;&#039; ) $this-&gt;port&nbsp;&nbsp;= $port;<br/>&nbsp;&nbsp;&nbsp;&nbsp; $this-&gt;con&nbsp;&nbsp;= ssh2_connect($this-&gt;host, $this-&gt;port);<br/>&nbsp;&nbsp;&nbsp;&nbsp; if( !$this-&gt;con ) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $this-&gt;log .= &quot;Connection failed !&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp; &#125;<br/>&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;function authPassword( $user = &#039;&#039;, $password = &#039;&#039; ) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp; if( $user!=&#039;&#039; ) $this-&gt;user&nbsp;&nbsp;= $user;<br/>&nbsp;&nbsp;&nbsp;&nbsp; if( $password!=&#039;&#039; ) $this-&gt;password&nbsp;&nbsp;= $password;<br/>&nbsp;&nbsp;&nbsp;&nbsp; if( !ssh2_auth_password( $this-&gt;con, $this-&gt;user, $this-&gt;password ) ) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $this-&gt;log .= &quot;Authorization failed !&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp; &#125;<br/>&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;function openShell( $shell_type = &#039;&#039; ) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if ( $shell_type != &#039;&#039; ) $this-&gt;shell_type = $shell_type;<br/>&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;shell = ssh2_shell( $this-&gt;con,&nbsp;&nbsp;$this-&gt;shell_type );<br/>&nbsp;&nbsp;&nbsp;&nbsp;if( !$this-&gt;shell ) $this-&gt;log .= &quot; Shell connection failed !&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;stream_set_blocking( $this-&gt;shell, true );<br/>&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;function writeShell( $command = &#039;&#039; ) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;fwrite($this-&gt;shell, $command.&quot;&#92;n&quot;);<br/>&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;function cmdExec( ) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$argc = func_num_args();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$argv = func_get_args();<br/>&nbsp;&nbsp;&nbsp;&nbsp;$cmd = &#039;&#039;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;for( $i=0; $i&lt;$argc ; $i++) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if( $i != ($argc-1) ) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$cmd .= $argv[$i].&quot; &amp;&amp; &quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;else&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$cmd .= $argv[$i];<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;echo $cmd;<br/>&nbsp;&nbsp;&nbsp;&nbsp;$stream = ssh2_exec( $this-&gt;con, $cmd );<br/>&nbsp;&nbsp;&nbsp;&nbsp;stream_set_blocking( $stream, true );<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return stream_get_contents($stream);<br/>&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;function getLog() &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp; return $this-&gt;log;<br/>&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;function getResult()&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$contents=&#039;&#039;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;while (!feof($this-&gt;shell)) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$contents.=fgets($this-&gt;shell);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return $contents;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&#125;<br/>?&gt;<br/>3、&nbsp;&nbsp;几点说明<br/>ssh2_exec()一次只能執行一個指令，也就是說，在執行完ssh2_exec()之後，PHP就會將連線中斷，下一個ssh2_exec()執行 時，會重新Login進去執行，也就是說指令之間是不會有關聯性的，這對網路設備來說是非常不合理的，因為有用過網路設備的都知道，要完成一項作業可能會 需要很多道指令，每個指令可能都有階層式關係，我目前的解法是在每個指令後用換行符號接起來，eg.&quot;config firewall policy &#92;n edit 1&#92;n&quot;。（多个指令的时候可以这样解决）<br/>不過因為ssh2_shell()不會主動中斷連線，使用時 (Class裡面的openShell跟writeShell)，記得要把logout的指令一並執行，不然程式會hang住<br/>（要将logout指令跑一下表示用户正常退出掉）<br/>4、&nbsp;&nbsp;调用示例<br/>include_once &#039;ssh2.php&#039;;<br/>$shell = new ssh2(&quot;192.168.0.100&quot;);<br/>$shell-&gt;authPassword(&quot;root&quot;,&quot;123456&quot;);<br/>$shell-&gt;openShell(&quot;xterm&quot;);<br/>$result=$shell-&gt;cmdExec(&quot;puppetrun -d --host 123456.puppet.test.com&nbsp;&nbsp;-t abc &gt;&gt; /dev/null&quot;);<br/>$shell-&gt;writeShell(&quot;exit&quot;);<br/>echo $result;<br/><br/>来源：http://blog.chinaunix.net/u2/84280/showart_2386961.html
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]php中的ssh2模块学习及直接通过读取id_rsa认证读取公钥的脚本的方法。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>