SshTask.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /*
  3. * $Id: SshTask.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/Task.php';
  22. /**
  23. * Execute commands on a remote host using ssh.
  24. *
  25. * @author Johan Van den Brande <johan@vandenbrande.com>
  26. * @version $Id: SshTask.php 905 2010-10-05 16:28:03Z mrook $
  27. * @package phing.tasks.ext
  28. */
  29. class SshTask extends Task {
  30. private $host = "";
  31. private $port = 22;
  32. private $username = "";
  33. private $password = "";
  34. private $command = "";
  35. private $pubkeyfile = '';
  36. private $privkeyfile = '';
  37. private $privkeyfilepassphrase = '';
  38. public function setHost($host)
  39. {
  40. $this->host = $host;
  41. }
  42. public function getHost()
  43. {
  44. return $this->host;
  45. }
  46. public function setPort($port)
  47. {
  48. $this->port = $port;
  49. }
  50. public function getPort()
  51. {
  52. return $this->port;
  53. }
  54. public function setUsername($username)
  55. {
  56. $this->username = $username;
  57. }
  58. public function getUsername()
  59. {
  60. return $this->username;
  61. }
  62. public function setPassword($password)
  63. {
  64. $this->password = $password;
  65. }
  66. public function getPassword()
  67. {
  68. return $this->password;
  69. }
  70. /**
  71. * Sets the public key file of the user to scp
  72. */
  73. public function setPubkeyfile($pubkeyfile)
  74. {
  75. $this->pubkeyfile = $pubkeyfile;
  76. }
  77. /**
  78. * Returns the pubkeyfile
  79. */
  80. public function getPubkeyfile()
  81. {
  82. return $this->pubkeyfile;
  83. }
  84. /**
  85. * Sets the private key file of the user to scp
  86. */
  87. public function setPrivkeyfile($privkeyfile)
  88. {
  89. $this->privkeyfile = $privkeyfile;
  90. }
  91. /**
  92. * Returns the private keyfile
  93. */
  94. public function getPrivkeyfile()
  95. {
  96. return $this->privkeyfile;
  97. }
  98. /**
  99. * Sets the private key file passphrase of the user to scp
  100. */
  101. public function setPrivkeyfilepassphrase($privkeyfilepassphrase)
  102. {
  103. $this->privkeyfilepassphrase = $privkeyfilepassphrase;
  104. }
  105. /**
  106. * Returns the private keyfile passphrase
  107. */
  108. public function getPrivkeyfilepassphrase($privkeyfilepassphrase)
  109. {
  110. return $this->privkeyfilepassphrase;
  111. }
  112. public function setCommand($command)
  113. {
  114. $this->command = $command;
  115. }
  116. public function getCommand()
  117. {
  118. return $this->command;
  119. }
  120. public function init()
  121. {
  122. if (!function_exists('ssh2_connect')) {
  123. throw new BuildException("To use SshTask, you need to install the SSH extension.");
  124. }
  125. return TRUE;
  126. }
  127. public function main()
  128. {
  129. $this->connection = ssh2_connect($this->host, $this->port);
  130. if (is_null($this->connection)) {
  131. throw new BuildException("Could not establish connection to " . $this->host . ":" . $this->port . "!");
  132. }
  133. $could_auth = null;
  134. if ( $this->pubkeyfile ) {
  135. $could_auth = ssh2_auth_pubkey_file($this->connection, $this->username, $this->pubkeyfile, $this->privkeyfile, $this->privkeyfilepassphrase);
  136. } else {
  137. $could_auth = ssh2_auth_password($this->connection, $this->username, $this->password);
  138. }
  139. if (!$could_auth) {
  140. throw new BuildException("Could not authenticate connection!");
  141. }
  142. $stream = ssh2_exec($this->connection, $this->command);
  143. if (!$stream) {
  144. throw new BuildException("Could not execute command!");
  145. }
  146. stream_set_blocking( $stream, true );
  147. while( $buf = fread($stream,4096) ){
  148. print($buf);
  149. }
  150. fclose($stream);
  151. }
  152. }
  153. ?>