-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh.class.php
76 lines (65 loc) · 1.49 KB
/
ssh.class.php
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
<?php
die("");
require_once('func.inc.php');
class fake_user
{
public $isset = true;
public function get_login()
{
return "picque_j";
}
public function get_passwd($type)
{
return "";
}
}
class EpitechSSH
{
CONST EPITECH_SSH_ADDR = "sshd.mikkl.fr";
CONST ERR_USER_NOTSET = -42;
CONST ERR_NOT_SET = -41;
CONST ERR_SSH_CONNECT_FAILED = -40;
CONST ERR_SSH_AUTH_FAILED = -39;
CONST ERR_SSH_EXEC_FAILED = -38;
public $isset = false;
public $error = NULL;
private $user = NULL;
private $connection = NULL;
public function __construct($user)
{
if ($user->isset === false)
{
$this->isset = false;
$this->error = EpitechSSH::ERR_USER_NOTSET;
}
if (!$this->connection = ssh2_connect(EpitechSSH::EPITECH_SSH_ADDR, 22))
{
$this->isset = false;
$this->error = EpitechSSH::ERR_SSH_CONNECT_FAILED;
}
if (!ssh2_auth_password($this->connection, $user->get_login(), $user->get_passwd('unix')))
{
$this->isset = false;
$this->error = EpitechSSH::ERR_SSH_AUTH_FAILED;
}
$this->isset = true;
return ($this);
}
public function exec($cmd) {
if (!$this->isset)
return ($this->error = EpitechSSH::ERR_NOT_SET);
if (!($stream = ssh2_exec($this->connection, $cmd)))
return ($this->error = EpitechSSH::ERR_SSH_EXEC_FAILED);
stream_set_blocking($stream, true);
$data = "";
while ($buf = fread($stream, 4096))
$data .= $buf;
fclose($stream);
return ($data);
}
}
$user = new fake_user();
$ssh = new EpitechSSH($user);
echo "<pre>";
rec_tree($ssh, "/", 0);
?>