ssh 或 cURL 将数据发送到远程服务器


ssh or cURL to send data to remote server

我正在创建一个应用程序来将我的本地mysql数据库同步到远程mysql数据库,所以我正在生成一个转储文件,通过sFTP发送它,然后在服务器上执行它。

但是,我知道还有其他可用的方法,例如 cURL。我喜欢将数据发送到远程服务器并在接受时在服务器上执行它(TRUE),但我对在这方面使用 cURL 相关的安全问题知之甚少。任何人都可以就 cURL 解决方案提供建议,否则建议任何替代方法?

首先,您应该使用 file_get_contents()fread() 等从文件中读取数据,而不是生成转储文件。

将此结果存储在变量中,然后通过管道发送原始数据(如果需要,可以通过 cURL),并让服务器端的代码生成转储文件。

使用 cURL,您可以指定用于身份验证的私有证书文件 - 因此其安全性与通过 ssh 使用证书相同 - 这不是您需要担心的事情。

您可以使用以下 cURL 选项示例设置 pem 文件:

curl_setopt($ch, CURLOPT_SSLCERT, $pemfile);
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM'); 
curl_setopt($ch, CURLOPT_SSLKEY, $keyfile); 

互联网上有上述教程。使用私钥身份验证,您的安全问题已解决。只要确保你没有CURLOPT_SSL_VERIFYPEER设置为false(你现在不想有任何MiTM(中间人)攻击,是吗)。

我使用Curl来做到这一点。

我有一个生成 json 或 xml 的导出脚本(取决于我的心情比其他任何事情都重要)然后将此文件发布到远程服务器,远程服务器使用ignore_user_abort以便即使父内部系统脚本超时/完成任何内容,处理也可以继续。

工作方式类似于在本地 Web 服务器和远程 Web 服务器之间将更改同步到 2.6gb 表的超级按钮。

我使用phpseclib,一个纯粹的PHP SFTP实现,来做这样的事情。

<?php
include('Net/SFTP.php');
$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
    exit('Login Failed');
}
$sftp->put('filename.remote', 'filename.local', NET_SFTP_LOCAL_FILE);
?>

与libssh2相比,它具有许多优势,包括速度和可移植性:

http://phpseclib.sourceforge.net/ssh/compare.html

这是使用 ssh2/libssh 的基本解决方案。

此代码假定您已经有创建数据库转储的方法,并且只是在当前服务器上读取它,目的是将其加载到远程服务器上。

它通过 SSH 连接到远程主机,将sql_dump写入远程服务器上的文件,然后执行命令将其加载到数据库中。

我不建议存储用于连接的用户名/密码,这只是测试代码的快速方法。最好使用 ssh2_auth_pubkey_file 进行身份验证:http://php.net/manual/en/function.ssh2-auth-pubkey-file.php

// remote host authentication details. hostname/ip, user, pass
$host = 'REMOTE_HOST';
$user = 'REMOTE_USER';
$pass = 'REMOTE_PASS';
// check if we have ssh2 installed first
if (function_exists("ssh2_connect")) {
    //connect to remote host
    $connection = ssh2_connect($host, 22);
    // if connection successful, proceed
    if ($connection) {
        // authenticate on remote connection
        $auth = ssh2_auth_password($connection, $user, $pass);
        // if we have authenticated, proceed with remote commands
        if ($auth) {
            // load our dump file to a string
            $sql_str = file_get_contents('dump_file.sql');
            // bash command to cat our dump string to a file
            $write_remote_file_command = "cat <<'EOF' > /home/tmp_file.sql 'n$sql_str 'nEOF";
            // call our execute ssh function to execute above command
            executeSSHCommand($connection, $write_remote_file_command);
            // command to load our temp dump file into the database
            // - you may need to add additional commands to drop the existing db, etc
            $remote_load_command = "mysql -Uroot -p -h localhost database_name < /home/tmp_file.sql";
            // remotely execute load commands
            executeSSHCommand($connection, $remote_load_command);
        }
    }
}
// basic function to execute remote shell commands via our authenticated $connection
function executeSSHCommand($connection, $command) {
    $output = array();
    $ssh_data = "";
    $stream = ssh2_exec($connection, $command);
    if ($stream) {
        stream_set_blocking($stream, true);
        while ($buffer = fread($stream, 65536)) {
            $ssh_data .= $buffer;
        }
        fclose($stream);
        $output = explode(PHP_EOL, $ssh_data);
    } 
    return $output;
}