3
3
namespace DivineOmega \SSHConnection ;
4
4
5
5
use InvalidArgumentException ;
6
+ use phpseclib \Crypt \RSA ;
7
+ use phpseclib \Net \SCP ;
8
+ use phpseclib \Net \SSH2 ;
6
9
use RuntimeException ;
7
10
8
11
class SSHConnection
@@ -11,10 +14,9 @@ class SSHConnection
11
14
private $ port = 22 ;
12
15
private $ username ;
13
16
private $ password ;
14
- private $ publicKeyPath ;
15
17
private $ privateKeyPath ;
16
18
private $ connected = false ;
17
- private $ resource ;
19
+ private $ ssh ;
18
20
19
21
public function to (string $ hostname ): self
20
22
{
@@ -40,9 +42,8 @@ public function withPassword(string $password): self
40
42
return $ this ;
41
43
}
42
44
43
- public function withKeyPair ( string $ publicKeyPath , string $ privateKeyPath ): self
45
+ public function withPrivateKey ( string $ privateKeyPath ): self
44
46
{
45
- $ this ->publicKeyPath = $ publicKeyPath ;
46
47
$ this ->privateKeyPath = $ privateKeyPath ;
47
48
return $ this ;
48
49
}
@@ -57,30 +58,32 @@ private function sanityCheck()
57
58
throw new InvalidArgumentException ('Username not specified. ' );
58
59
}
59
60
60
- if (!$ this ->password && (!$ this ->publicKeyPath || ! $ this -> privateKeyPath )) {
61
- throw new InvalidArgumentException ('No password or public- private key pair specified. ' );
61
+ if (!$ this ->password && (!$ this ->privateKeyPath )) {
62
+ throw new InvalidArgumentException ('No password or private key path specified. ' );
62
63
}
63
64
}
64
65
65
66
public function connect (): self
66
67
{
67
68
$ this ->sanityCheck ();
68
69
69
- $ this ->resource = ssh2_connect ($ this ->hostname , $ this -> port );
70
+ $ this ->ssh = new SSH2 ($ this ->hostname );
70
71
71
- if (!$ this ->resource ) {
72
+ if (!$ this ->ssh ) {
72
73
throw new RuntimeException ('Error connecting to server. ' );
73
74
}
74
75
75
- if ($ this ->publicKeyPath || $ this ->privateKeyPath ) {
76
- $ authenticated = ssh2_auth_pubkey_file ($ this ->resource , $ this ->username , $ this ->publicKeyPath , $ this ->privateKeyPath );
76
+ if ($ this ->privateKeyPath ) {
77
+ $ key = new RSA ();
78
+ $ key ->loadKey (file_get_contents ($ this ->privateKeyPath ));
79
+ $ authenticated = $ this ->ssh ->login ($ this ->username , $ key );
77
80
if (!$ authenticated ) {
78
81
throw new RuntimeException ('Error authenticating with public-private key pair. ' );
79
82
}
80
83
}
81
84
82
85
if ($ this ->password ) {
83
- $ authenticated = ssh2_auth_password ( $ this ->resource , $ this ->username , $ this ->password );
86
+ $ authenticated = $ this ->ssh -> login ( $ this ->username , $ this ->password );
84
87
if (!$ authenticated ) {
85
88
throw new RuntimeException ('Error authenticating with password. ' );
86
89
}
@@ -97,7 +100,7 @@ public function disconnect(): void
97
100
throw new RuntimeException ('Unable to disconnect. Not yet connected. ' );
98
101
}
99
102
100
- ssh2_disconnect ( $ this ->resource );
103
+ $ this ->ssh -> disconnect ( );
101
104
}
102
105
103
106
public function run (string $ command ): SSHCommand
@@ -106,7 +109,7 @@ public function run(string $command): SSHCommand
106
109
throw new RuntimeException ('Unable to run commands when not connected. ' );
107
110
}
108
111
109
- return new SSHCommand ($ this ->resource , $ command );
112
+ return new SSHCommand ($ this ->ssh , $ command );
110
113
}
111
114
112
115
public function upload (string $ localPath , string $ remotePath ): bool
@@ -119,7 +122,7 @@ public function upload(string $localPath, string $remotePath): bool
119
122
throw new InvalidArgumentException ('The local file does not exist. ' );
120
123
}
121
124
122
- return ssh2_scp_send ( $ this ->resource , $ localPath , $ remotePath );
125
+ return ( new SCP ( $ this ->ssh ))-> put ( $ remotePath , $ localPath , SCP :: SOURCE_LOCAL_FILE );
123
126
}
124
127
125
128
public function download (string $ remotePath , string $ localPath ): bool
@@ -128,7 +131,7 @@ public function download(string $remotePath, string $localPath): bool
128
131
throw new RuntimeException ('Unable to download file when not connected. ' );
129
132
}
130
133
131
- return ssh2_scp_recv ( $ this ->resource , $ remotePath , $ localPath );
134
+ return ( new SCP ( $ this ->ssh ))-> get ( $ remotePath , $ localPath );
132
135
}
133
136
134
137
public function isConnected (): bool
0 commit comments