1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
<?php
/* ************************************************************ */
/*
* Copyright (C) 2008 Volkan KIRIK
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* ************************************************************ */
/*
* SSH - SFTP (class.shell2.php)
* v 1.0 2007-08-13
* v 1.1 2008-05-19 -current-
*
* Author : Volkan KIRIK
*
* Changes in v1.1:
*
* * Added Disconnect function
* * Some function names changed:
* auth to auth_pwd
* send to send_file
* get to get_file
* output to get_output
*
* Changes in v1.0:
*
* * First release
*
*/
/* ************************************************************ */
class shell2 {
var $conn;
var $error;
var $stream;
function __destruct(){
$this->disconnect();
}
function login($user, $pass, $host, $port=22) {
if ($this->connect($host,$port)) {
if ($this->auth_pwd($user,$pass)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
function connect($host,$port=22) {
if ($this->conn = ssh2_connect($host, $port)) {
return true;
} else {
$this->error = '[x] Can not connected to '.$host.':'.$port;
return false;
}
}
function auth_pwd($u,$p) {
if (ssh2_auth_password($this->conn, $u, $p)) {
return true;
} else {
$this->error = 'Login Failed';
return false;
}
}
function send_file($localFile,$remoteFile,$permision) {
if (@ssh2_scp_send($this->conn, $localFile, $remoteFile, $permision)) {
return true;
} else {
$this->error = 'Can not transfer file';
return false;
}
}
function get_file($remoteFile,$localFile) {
if (ssh2_scp_recv($this->conn, $remoteFile, $localFile)) {
return true;
} else {
return false;
}
}
function exec_cmd($cmd) {
$this->stream=ssh2_exec($this->conn, $cmd);
stream_set_blocking( $this->stream, true );
}
function get_output() {
$line = '';
while ($get=fgets($this->stream)) {
$line.=$get;
}
return $line;
}
function disconnect() {
// 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);
unset($this->conn);
}
// return null always
return NULL;
}
}
?> |
Partager