-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh_connect.php
More file actions
27 lines (23 loc) · 789 Bytes
/
Copy pathssh_connect.php
File metadata and controls
27 lines (23 loc) · 789 Bytes
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
<?php
function sshConnect() {
$remote_server = $_ENV['FILE_HOSTING_URL'];
$remote_user = $_ENV['FILE_HOSTING_USER'];
$remote_password = $_ENV['FILE_HOSTING_PASS'];
// Establish SSH connection
$connection = ssh2_connect($remote_server, 22);
if (!$connection) {
throw new Exception("Failed to establish SSH connection.");
}
// Authenticate
if (!ssh2_auth_password($connection, $remote_user, $remote_password)) {
throw new Exception("SSH authentication failed.");
}
// Initialize SFTP
$sftp = ssh2_sftp($connection);
if (!$sftp) {
throw new Exception("Failed to initialize SFTP subsystem.");
}
// Return both connection and sftp as an array
return ['connection' => $connection, 'sftp' => $sftp];
}
?>