[轉(zhuǎn)載]php中的ssh2
1、 前言
為了能夠讓管理員直接選擇好若干主機(jī)及機(jī)房然后直接將命令推送到各個(gè)主機(jī)上去執(zhí)行。現(xiàn)整理了一下php里面的ssh2模塊。方便使用。
2、 完整的類代碼如下
<?php
// ssh protocols
// note: once openShell method is used, cmdExec does not work
class ssh2 {
private $host = 'host';
private $user = 'user';
private $port = '22';
private $password = 'password';
private $con = null;
private $shell_type = 'xterm';
private $shell = null;
private $log = '';
function __construct($host='', $port='' ) {
if( $host!='' ) $this->host = $host;
if( $port!='' ) $this->port = $port;
$this->con = ssh2_connect($this->host, $this->port);
if( !$this->con ) {
$this->log .= "Connection failed !";
}
}
function authPassword( $user = '', $password = '' ) {
if( $user!='' ) $this->user = $user;
if( $password!='' ) $this->password = $password;
if( !ssh2_auth_password( $this->con, $this->user, $this->password ) ) {
$this->log .= "Authorization failed !";
}
}
function openShell( $shell_type = '' ) {
if ( $shell_type != '' ) $this->shell_type = $shell_type;
$this->shell = ssh2_shell( $this->con, $this->shell_type );
if( !$this->shell ) $this->log .= " Shell connection failed !";
stream_set_blocking( $this->shell, true );
}
function writeShell( $command = '' ) {
fwrite($this->shell, $command."\n");
}
function cmdExec( ) {
$argc = func_num_args();
$argv = func_get_args();
$cmd = '';
for( $i=0; $i<$argc ; $i++) {
if( $i != ($argc-1) ) {
$cmd .= $argv[$i]." && ";
}else{
$cmd .= $argv[$i];
}
}
echo $cmd;
$stream = ssh2_exec( $this->con, $cmd );
stream_set_blocking( $stream, true );
return stream_get_contents($stream);
}
function getLog() {
return $this->log;
}
function getResult(){
$contents='';
while (!feof($this->shell)) {
$contents.=fgets($this->shell);
}
return $contents;
}
}
?>
3、 幾點(diǎn)說(shuō)明
ssh2_exec() 一次只能執(zhí)行一個(gè)指令,也就是說(shuō),在執(zhí)行完ssh2_exec()之後,PHP就會(huì)將連線中斷,下一個(gè)ssh2_exec()執(zhí)行 時(shí),會(huì)重新Login進(jìn)去執(zhí)行,也就是說(shuō)指令之間是不會(huì)有關(guān)聯(lián)性的,這對(duì)網(wǎng)路設(shè)備來(lái)說(shuō)是非常不合理的,因?yàn)橛杏眠^(guò)網(wǎng)路設(shè)備的都知道,要完成一項(xiàng)作業(yè)可能會(huì) 需要很多道指令,每個(gè)指令可能都有階層式關(guān)係,我目前的解法是在每個(gè)指令後用換行符號(hào)接起來(lái),eg."config firewall policy \n edit 1\n"。(多個(gè)指令的時(shí)候可以這樣解決)
不過(guò)因?yàn)閟sh2_shell()不會(huì)主動(dòng)中斷連線,使用時(shí) (Class裡面的openShell跟writeShell),記得要把logout的指令一並執(zhí)行,不然程式會(huì)hang住
(要將logout指令跑一下表示用戶正常退出掉)
4、 調(diào)用示例
include_once 'ssh2.php';
$shell = new ssh2("192.168.0.100");
$shell->authPassword("root","123456");
$shell->openShell("xterm");
$result=$shell->cmdExec("puppetrun -d --host 123456.puppet.test.com -t abc >> /dev/null");
$shell->writeShell("exit");
echo $result;
為了能夠讓管理員直接選擇好若干主機(jī)及機(jī)房然后直接將命令推送到各個(gè)主機(jī)上去執(zhí)行。現(xiàn)整理了一下php里面的ssh2模塊。方便使用。
2、 完整的類代碼如下
<?php
// ssh protocols
// note: once openShell method is used, cmdExec does not work
class ssh2 {
private $host = 'host';
private $user = 'user';
private $port = '22';
private $password = 'password';
private $con = null;
private $shell_type = 'xterm';
private $shell = null;
private $log = '';
function __construct($host='', $port='' ) {
if( $host!='' ) $this->host = $host;
if( $port!='' ) $this->port = $port;
$this->con = ssh2_connect($this->host, $this->port);
if( !$this->con ) {
$this->log .= "Connection failed !";
}
}
function authPassword( $user = '', $password = '' ) {
if( $user!='' ) $this->user = $user;
if( $password!='' ) $this->password = $password;
if( !ssh2_auth_password( $this->con, $this->user, $this->password ) ) {
$this->log .= "Authorization failed !";
}
}
function openShell( $shell_type = '' ) {
if ( $shell_type != '' ) $this->shell_type = $shell_type;
$this->shell = ssh2_shell( $this->con, $this->shell_type );
if( !$this->shell ) $this->log .= " Shell connection failed !";
stream_set_blocking( $this->shell, true );
}
function writeShell( $command = '' ) {
fwrite($this->shell, $command."\n");
}
function cmdExec( ) {
$argc = func_num_args();
$argv = func_get_args();
$cmd = '';
for( $i=0; $i<$argc ; $i++) {
if( $i != ($argc-1) ) {
$cmd .= $argv[$i]." && ";
}else{
$cmd .= $argv[$i];
}
}
echo $cmd;
$stream = ssh2_exec( $this->con, $cmd );
stream_set_blocking( $stream, true );
return stream_get_contents($stream);
}
function getLog() {
return $this->log;
}
function getResult(){
$contents='';
while (!feof($this->shell)) {
$contents.=fgets($this->shell);
}
return $contents;
}
}
?>
3、 幾點(diǎn)說(shuō)明
ssh2_exec() 一次只能執(zhí)行一個(gè)指令,也就是說(shuō),在執(zhí)行完ssh2_exec()之後,PHP就會(huì)將連線中斷,下一個(gè)ssh2_exec()執(zhí)行 時(shí),會(huì)重新Login進(jìn)去執(zhí)行,也就是說(shuō)指令之間是不會(huì)有關(guān)聯(lián)性的,這對(duì)網(wǎng)路設(shè)備來(lái)說(shuō)是非常不合理的,因?yàn)橛杏眠^(guò)網(wǎng)路設(shè)備的都知道,要完成一項(xiàng)作業(yè)可能會(huì) 需要很多道指令,每個(gè)指令可能都有階層式關(guān)係,我目前的解法是在每個(gè)指令後用換行符號(hào)接起來(lái),eg."config firewall policy \n edit 1\n"。(多個(gè)指令的時(shí)候可以這樣解決)
不過(guò)因?yàn)閟sh2_shell()不會(huì)主動(dòng)中斷連線,使用時(shí) (Class裡面的openShell跟writeShell),記得要把logout的指令一並執(zhí)行,不然程式會(huì)hang住
(要將logout指令跑一下表示用戶正常退出掉)
4、 調(diào)用示例
include_once 'ssh2.php';
$shell = new ssh2("192.168.0.100");
$shell->authPassword("root","123456");
$shell->openShell("xterm");
$result=$shell->cmdExec("puppetrun -d --host 123456.puppet.test.com -t abc >> /dev/null");
$shell->writeShell("exit");
echo $result;
posted on 2012-03-30 09:35 天羽 閱讀(1081) 評(píng)論(0) 收藏 舉報(bào)
浙公網(wǎng)安備 33010602011771號(hào)