<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      skyfeathers

      導(dǎo)航

      [轉(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;

      posted on 2012-03-30 09:35  天羽  閱讀(1081)  評(píng)論(0)    收藏  舉報(bào)

      主站蜘蛛池模板: 精品国产中文字幕在线| 国产精品视频亚洲二区| 粉嫩av蜜臀一区二区三区| 国精偷拍一区二区三区| 山阳县| 97av| 午夜精品福利亚洲国产| 成人免费无遮挡在线播放| 国产精品一区二区小视频| 亚洲精品一区二区三区四区乱码| 中文午夜乱理片无码| 91久久天天躁狠狠躁夜夜| 5D肉蒲团之性战奶水欧美| 精品国产免费一区二区三区香蕉| 久久国产自偷自免费一区| 产综合无码一区| 国内自拍小视频在线看| 国产边摸边吃奶边叫做激情视频 | 中文字幕 日韩 人妻 无码| 色猫咪av在线观看| 一区二区三区激情都市| 人妻少妇久久中文字幕| 亚洲一区二区av在线| 男同精品视频免费观看网站| 久久午夜无码免费| 年日韩激情国产自偷亚洲| 日韩有码中文字幕av| 亚洲精品中文字幕二区| 国精偷拍一区二区三区| 波多野结衣的av一区二区三区 | 国产午夜在线观看视频播放| 老师破女学生处特级毛ooo片| 成人网站网址导航| 亚洲www永久成人网站| 自拍亚洲综合在线精品| 亚洲国产中文字幕在线视频综合| 亚洲一区av在线观看| 国内精品视频一区二区三区八戒 | 办公室强奷漂亮少妇视频| 娱乐| 玩两个丰满老熟女久久网|