Hướng dẫn php sftp connect with private key - php sftp kết nối với khóa cá nhân

Bạn có thể sử dụng bộ điều hợp SFTP của FlySystem cho PHP. Bạn có thể sử dụng điều này trong bất kỳ khung nào bằng cách sử dụng cài đặt nhà soạn nhạc. FlySystem FileSystem trừu tượng cho PHP (Thư viện PHP).

https://flysystem.thephpleague.com/v2/docs/

Cài đặt thư viện bằng lệnh.

composer require league/flysystem-sftp:^2.0

Sử dụng mã dưới đây để kết nối với SFTP bằng hệ thống tập tin.

 [
            'public' => 0640,
            'private' => 0604,
        ],
        'dir' => [
            'public' => 0740,
            'private' => 7604,
        ],
    ])
));
$allFiles = $filesystem->listContents('Outbound')->toArray();
$response = $filesystem->write('Outbound/info.txt', 'Hello How are you',array());
if($response){
    echo "Success";
}else echo "Error";
print_r($allFiles );
?>

Composer.json trông giống như

{
    "name": "league/flysystem-sftp",
    "description": "Flysystem adapter for SFTP",
    "license": "MIT",
    "authors": [
        {
            "name": "Frank de Jonge",
            "email": ""
        }
    ],
    "require": {
        "php": ">=5.6.0",
        "league/flysystem-sftp": "^2.1",
        "phpseclib/phpseclib": "~2.0"
    },
    "require-dev": {
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "^5.7.25"
    },
    "autoload": {
        "psr-4": {
            "League\\Flysystem\\Sftp\\": "src/"
        }
    }
}

Trình bày cách xác thực với máy chủ SSH/SFTP bằng cách sử dụng xác thực của PublicKey.



// The version number (9_5_0) should match version of the Chilkat extension used, omitting the micro-version number.
// For example, if using Chilkat v9.5.0.48, then include as shown here:
include("chilkat_9_5_0.php");

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

$sftp = new CkSFtp();

// Set some timeouts, in milliseconds:
$sftp->put_ConnectTimeoutMs(5000);
$sftp->put_IdleTimeoutMs(15000);

// Connect to the SSH server.  
// The standard SSH port = 22
// The hostname may be a hostname or IP address.
$hostname = 'sftp.example.com';
$port = 22;
$success = $sftp->Connect($hostname,$port);
if ($success != true) {
    print $sftp->lastErrorText() . "\n";
    exit;
}

$key = new CkSshKey();

// Load a PEM file into a string variable:
// (This does not load the PEM file into the key.  The LoadText
// method is a convenience method for loading the full contents of ANY text
// file into a string variable.)
$privKey = $key->loadText('myPrivateKey.pem');
if ($key->get_LastMethodSuccess() != true) {
    print $key->lastErrorText() . "\n";
    exit;
}

// Load a private key from a PEM string:
// (Private keys may be loaded from OpenSSH and Putty formats.
// Both encrypted and unencrypted private key file formats
// are supported.  This example loads an unencrypted private
// key in OpenSSH format.
$success = $key->FromOpenSshPrivateKey($privKey);
if ($success != true) {
    print $key->lastErrorText() . "\n";
    exit;
}

// Authenticate with the SSH server.  Chilkat SFTP supports
// both password-based authenication as well as public-key
// authentication.  
$success = $sftp->AuthenticatePk('myLogin',$key);
if ($success != true) {
    print $sftp->lastErrorText() . "\n";
    exit;
}

print $sftp->lastErrorText() . "\n";
print 'Public-Key Authentication Successful!' . "\n";

?>

© 2000-2022 Phần mềm Chilkat, Inc. Tất cả quyền được bảo lưu.

Làm cách nào để vượt qua khóa riêng bằng SFTP?

2 câu trả lời..
Mở Notepad ..
Dán văn bản được cung cấp cho khóa SSH của bạn ..
Lưu tệp (Tôi đặt tên cho khóa của tôi. TXT).
Mở Cyberduck ..
Chọn Mở Kết nối ..
Chọn SFTP (Giao thức truyền tệp SSH) cho loại kết nối ..
Nhập máy chủ, cổng (22), tên người dùng ..
Bạn có thể bỏ qua mật khẩu - nó sẽ sử dụng phím SSH ..

SFTP có yêu cầu khóa riêng không?

Các khóa công khai và riêng tư của người dùng là một cặp khóa được sử dụng để xác thực máy khách khi kết nối với máy chủ SFTP. Khóa riêng của người dùng được giữ bí mật và được lưu trữ cục bộ trên PC của người dùng trong khi khóa công khai của người dùng được tải lên và đăng ký trên máy chủ SFTP mà người dùng kết nối.The user's private key is kept secret and stored locally on the user's PC while the user's public key is uploaded and registered on the SFTP server the user connects to.

Làm cách nào để tạo ra một cặp khóa SFTP?

Để tạo một cặp khóa SSH trên máy Macintosh hoặc Linux:..
Mở cửa sổ thiết bị đầu cuối ..
Nhập dòng lệnh này: ssh -keygen -t rsa ..
Chọn các giá trị mặc định cho tất cả các tùy chọn.Lệnh này tạo ra hai tệp khóa SSH, ID_RSA và ID_RSA.....
Gửi tệp khóa công khai ID_RSA.Pub cho đại diện đối tác của bạn ..

Bạn có thể kết nối với SFTP với khóa công khai không?

SFTP cho phép bạn xác thực khách hàng bằng các khóa công khai, điều đó có nghĩa là họ sẽ không cần mật khẩu., which means they won't need a password.