我正在使用sftp和密鑰登錄到服務器的腳本。我在使用腳本訪問服務器時遇到以下錯誤。錯誤是如下:sftp腳本登錄到服務器
: No such file or directory
./new.php: line 2: syntax error near unexpected token \`'SSH_HOST','
'/new.php: line 2: `define('SSH_HOST', 'IP_Address');
請參見下面的腳本我使用:
<?php
define('SSH_HOST', 'IP_Address');
define('SSH_PORT', 7475);
define('SSH_PUBLIC_KEY', '/home/test/test_sftp.pub');
define('SSH_PRIVATE_KEY', '/home/test_sftp.ppk');
define('SSH_USERNAME', 'test55');
define('SSH_PASSWORD', 'lkdlkdsf');
//define('SSH_USERNAME', 'test5654');
//define('SSH_PASSWORD', 'dfsfsd45');
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include_once('Net/SSH2.php');
include_once('Net/SFTP.php');
include_once('Crypt/RSA.php');
include_once('Crypt/Hash.php');
include_once('Math/BigInteger.php');
$rsa = new Crypt_RSA();
//$pubKey = file_get_contents('id_rsa.pub');
//$rsa->setPublicKey($pubKey);
$priKey = file_get_contents(SSH_PRIVATE_KEY);
$rsa->loadKey($priKey);
$sftp = new Net_SFTP(SSH_HOST, SSH_PORT);
if (!$sftp->login(SSH_USERNAME, $rsa)) {
echo 'error:';
print_r($sftp->getLog());
exit("Login Failed\n");
}
echo $sftp->pwd() . "\r\n";
//$sftp->put('filename.ext', 'Hiiii!');
這是我的錯誤。我使用'./ script.php'而不是'php script.php'。 但登錄仍然有一些問題。現在變得 '錯誤:登錄失敗' – Gopu
您正在調用$ sftp-> getLog(),但尚未啓用日誌記錄。你需要在開始時執行define('NET_SSH2_LOGGING',2)'。 – neubert