PHP

2011-06-07 3 views
52

के माध्यम से एसएसएच कमांड को निष्पादित करने के लिए मैं PHP के माध्यम से एसएसएच को देख रहा हूं। इस बारे में जाने का सबसे अच्छा/सबसे सुरक्षित तरीका क्या है? मुझे पता है कि मैं कर सकता हूं:PHP

shell_exec("SSH [email protected] mkdir /testing"); 

कुछ भी बेहतर है? ऐसा लगता है कि 'शरारती' :)।

उत्तर

16

क्या आपके पास एसएसएच 2 एक्सटेंशन उपलब्ध है?

डॉक्स: http://www.php.net/manual/en/function.ssh2-exec.php

$connection = ssh2_connect('shell.example.com', 22); 
ssh2_auth_password($connection, 'username', 'password'); 

$stream = ssh2_exec($connection, '/usr/local/bin/php -i'); 
+1

खूनी SSH2 मॉड्यूल, जा रहा त्रुटि लॉग में निम्न हो रही प्राप्त नहीं कर सकता: PHP चेतावनी: PHP स्टार्टअप: SSH2: प्रारंभ करने में मॉड्यूल मॉड्यूल मॉड्यूल एपीआई = 20,050,922 पीएचपी संकलित साथ संकलित असमर्थ मॉड्यूल एपीआई = 20090626 के साथ इन विकल्पों को से मेल खाने की आवश्यकता है 0 – Justin

+0

क्या ओएस और संस्करण, और PHP संस्करण क्या है? –

+2

यह भी ध्यान दें, ssh2 मॉड्यूल के लिए आपको सही तरीके से कार्य करने के लिए OpenSSL और libssh2 होना चाहिए। यदि आप मैन्युअल रूप से PHP बना रहे हैं, तो आपको यह सुनिश्चित करना होगा कि आपके पास दो आवश्यक मॉड्यूल के संगत संस्करण हैं। यह त्रुटि संस्करणों में मेल नहीं खाती है। –

2

ssh2 कार्यों का प्रयोग करें। किसी भी निष्पादन() कॉल के माध्यम से आप जो कुछ भी करेंगे, सीधे इन कार्यों का उपयोग करके किया जा सकता है, जिससे आप बहुत सारे कनेक्शन और खोल आमंत्रणों को बचा सकते हैं।

10

मुझे php में ssh2 के साथ कठिन समय था क्योंकि आउटपुट स्ट्रीम कभी-कभी काम करती है और कभी-कभी ऐसा नहीं होती है। मैं सिर्फ अपनी लिब पेस्ट कर रहा हूं जो मेरे लिए बहुत अच्छी तरह से काम करता है।

<?php 

class Components_Ssh { 

    private $host; 

    private $user; 

    private $pass; 

    private $port; 

    private $conn = false; 

    private $error; 

    private $stream; 

    private $stream_timeout = 100; 

    private $log; 

    private $lastLog; 

    public function __construct ($host, $user, $pass, $port, $serverLog) { 
     $this->host = $host; 
     $this->user = $user; 
     $this->pass = $pass; 
     $this->port = $port; 
     $this->sLog = $serverLog; 

     if ($this->connect()->authenticate()) { 
      return true; 
     } 
    } 

    public function isConnected() { 
     return (boolean) $this->conn; 
    } 

    public function __get ($name) { 
     return $this->$name; 
    } 

    public function connect() { 
     $this->logAction ("Connecting to {$this->host}"); 
     if ($this->conn = ssh2_connect ($this->host, $this->port)) { 
      return $this; 
     } 
     $this->logAction ("Connection to {$this->host} failed"); 
     throw new Exception ("Unable to connect to {$this->host}"); 
    } 

    public function authenticate() { 
     $this->logAction ("Authenticating to {$this->host}"); 
     if (ssh2_auth_password ($this->conn, $this->user, $this->pass)) { 
      return $this; 
     } 
     $this->logAction ("Authentication to {$this->host} failed"); 
     throw new Exception ("Unable to authenticate to {$this->host}"); 
    } 

    public function sendFile ($localFile, $remoteFile, $permision = 0644) { 
     if (! is_file ($localFile)) throw new Exception ("Local file {$localFile} does not exist"); 
     $this->logAction ("Sending file $localFile as $remoteFile"); 

     $sftp = ssh2_sftp ($this->conn); 
     $sftpStream = @fopen ('ssh2.sftp://' . $sftp . $remoteFile, 'w'); 
     if (! $sftpStream) { 
      // if 1 method failes try the other one 
      if (! @ssh2_scp_send ($this->conn, $localFile, $remoteFile, $permision)) { 
       throw new Exception ("Could not open remote file: $remoteFile"); 
      } 
      else { 
       return true; 
      } 
     } 

     $data_to_send = @file_get_contents ($localFile); 

     if (@fwrite ($sftpStream, $data_to_send) === false) { 
      throw new Exception ("Could not send data from file: $localFile."); 
     } 

     fclose ($sftpStream); 

     $this->logAction ("Sending file $localFile as $remoteFile succeeded"); 
     return true; 
    } 

    public function getFile ($remoteFile, $localFile) { 
     $this->logAction ("Receiving file $remoteFile as $localFile"); 
     if (ssh2_scp_recv ($this->conn, $remoteFile, $localFile)) { 
      return true; 
     } 
     $this->logAction ("Receiving file $remoteFile as $localFile failed"); 
     throw new Exception ("Unable to get file to {$remoteFile}"); 
    } 

    public function cmd ($cmd, $returnOutput = false) { 
     $this->logAction ("Executing command $cmd"); 
     $this->stream = ssh2_exec ($this->conn, $cmd); 

     if (FALSE === $this->stream) { 
      $this->logAction ("Unable to execute command $cmd"); 
      throw new Exception ("Unable to execute command '$cmd'"); 
     } 
     $this->logAction ("$cmd was executed"); 

     stream_set_blocking ($this->stream, true); 
     stream_set_timeout ($this->stream, $this->stream_timeout); 
     $this->lastLog = stream_get_contents ($this->stream); 

     $this->logAction ("$cmd output: {$this->lastLog}"); 
     fclose ($this->stream); 
     $this->log .= $this->lastLog . "\n"; 
     return ($returnOutput) ? $this->lastLog : $this; 
    } 

    public function shellCmd ($cmds = array()) { 
     $this->logAction ("Openning ssh2 shell"); 
     $this->shellStream = ssh2_shell ($this->conn); 

     sleep (1); 
     $out = ''; 
     while ($line = fgets ($this->shellStream)) { 
      $out .= $line; 
     } 

     $this->logAction ("ssh2 shell output: $out"); 

     foreach ($cmds as $cmd) { 
      $out = ''; 
      $this->logAction ("Writing ssh2 shell command: $cmd"); 
      fwrite ($this->shellStream, "$cmd" . PHP_EOL); 
      sleep (1); 
      while ($line = fgets ($this->shellStream)) { 
       $out .= $line; 
       sleep (1); 
      } 
      $this->logAction ("ssh2 shell command $cmd output: $out"); 
     } 

     $this->logAction ("Closing shell stream"); 
     fclose ($this->shellStream); 
    } 

    public function getLastOutput() { 
     return $this->lastLog; 
    } 

    public function getOutput() { 
     return $this->log; 
    } 

    public function disconnect() { 
     $this->logAction ("Disconnecting from {$this->host}"); 
     // if disconnect function is available call it.. 
     if (function_exists ('ssh2_disconnect')) { 
      ssh2_disconnect ($this->conn); 
     } 
     else { // if no disconnect func is available, close conn, unset var 
      @fclose ($this->conn); 
      $this->conn = false; 
     } 
     // return null always 
     return NULL; 
    } 

    public function fileExists ($path) { 
     $output = $this->cmd ("[ -f $path ] && echo 1 || echo 0", true); 
     return (bool) trim ($output); 
    } 
} 
+1

धन्यवाद! इसने थोड़ा सा मदद की। मैं सार्वजनिक/निजी कुंजी प्रमाणीकरण का उपयोग करने के लिए इसे फिर से लिखता हूं लेकिन नींद (1) मेरी उत्पादकता को मार देती है। अगर मेरे पास एक काम के साथ आने का समय है तो मैं आपको बता दूंगा। धन्यवाद! – jbrahy

62

मैं phpseclib, a pure PHP SSH implementation का प्रयोग करेंगे: कोड में छोटे विसंगतियां हैं अगर यह क्योंकि मैं इसे एक रूपरेखा में खामियों को दूर लेकिन आप इसे पोर्टिंग ठीक होना चाहिए है। एक उदाहरण:

<?php 
include('Net/SSH2.php'); 

$ssh = new Net_SSH2('www.domain.tld'); 
if (!$ssh->login('username', 'password')) { 
    exit('Login Failed'); 
} 

echo $ssh->exec('pwd'); 
echo $ssh->exec('ls -la'); 
?> 
+1

क्या ये असीमित हो सकते हैं? मुझे एक साथ कई प्रणालियों से बात करने की ज़रूरत है - क्या मैं कई एसएसएच सत्र शुरू कर सकता हूं और फिर उन सभी पर "चयन" (या मतदान) कर सकता हूं? .. –

+2

किंडा। असल में, आप सवाल जवाब के बजाय एक नए के रूप में बेहतर होंगे लेकिन जो भी हो। आप प्रत्येक $ ssh उदाहरण और उनके माध्यम से चक्र के लिए '$ ssh-> setTimeout (...) 'कर सकते हैं। इंटरेक्टिव मोड भी आपके लिए बेहतर हो सकता है क्योंकि वहां पढ़ने और लिखना थोड़े अलग हैं। इसी प्रकार, आप '$ ssh-> exec()' 'ssh-> read()'/'$ ssh-> लिखने()' के साथ 'ssh-> exec()' का उपयोग करने के लिए सक्षम PTY() कर सकते हैं। – neubert

+0

ध्यान दें कि यह लाइब्रेरी रीडलिंक का समर्थन नहीं करती है() – Alexander

संबंधित मुद्दे