2015-05-18 81 views
0

我想使用ssh2庫nodejs從遠程服務器獲取文件。我的代碼似乎對用戶「xyz」正常工作。但是,實際用戶的格式爲「abc \ xyz」,其中abc是域,xyz是實際用戶。當我運行與用戶相同的代碼爲「abc \ xyz」時,我不斷收到錯誤: 「sftp error:{[ERROR All configured authentication methods failed] level:'client-authentication'}」nodejs ssh2身份驗證失敗

我是能夠手動使用這個用戶作爲「xyz \ abc」@hostname的ssh,但由於某種原因,這在代碼中失敗。我錯過了什麼嗎?

由於某些限制不能在這裏提供確切的代碼,但下面是做一些必要的修改後的相關代碼,以掩蓋變量:

else { 
    // Provide an SFTP interface remote files 
var conn = new ssh2(); 
    conn.on('ready', function() { 
    logger.info('sftp connection ready'); 
    conn.sftp(function(err, sftp) { 
     if(err) throw err; 
     refreshRemoteFiles(sftp); 
    }); 
    }).on('error', function(err) { 
    logger.info('some text +variable: '+variable value); 
    logger.info('some text +variable'+variable value); 
    logger.info('some text +variable'+varibale value); 
    logger.info('some text: sftp error:', err); 

    }).connect({ 
    host: hostname, 
    port: 22, 
    username: username, 
    password: password 
    // debug: function(str) { logger.debug('sftp debug: %j', str); } 
    }); 
} 
+0

您可以顯示一些相關的代碼? – Trott

+0

這很難遵循。代碼很難在註釋中進行格式化,而註釋並非針對冗長的內容而設計的。只需編輯您的問題並將代碼放在那裏。謝謝! – Trott

+0

只是爲了澄清它確實拿起用戶名的價值(記錄器部分): 用戶名被選爲abc \ xyz。另外,我嘗試了不同的組合,例如將用戶名設爲「'abc \ xyz'」,以便傳遞給sftp接口的實際用戶名是'abc \ xyz' –

回答

0

可能是你忘記用戶名使用雙反斜線字符串,以防止使用反斜線作爲轉義特殊字符標記

0

希望能在下面可以給你一些想法... 我讀了SSH2和ssh2streams API參考,並制定出的東西用以下解決方案...

注意如果您有任何錯誤,如:句柄不是緩衝區。這可能意味着文件不存在或者您沒有權限訪問它。

讓我知道如果您有任何問題:)

checkJobStatus(startPos: number, readBytes: number, callback: (error: any, result: any) => void) { 
    let conn = new Client(); 
    conn.on('ready', function() { 
     console.log('Client :: ready'); 
     conn.sftp(function(err, sftp) { 
      if (err) throw err; 
      else {       
       sftp.open("/absolute/path/to/file", "r", function(err, fd) { 
        //Define a buffer you want to send back to your parent method 
        let m_fileBuffer: Buffer = new Buffer(readBytes); 
        sftp.read(fd, m_fileBuffer, 0, readBytes, startPos, function(err, bytesRead, buffer: Buffer, position) { 
         if(err) sftp.close(fd, callback(err, undefined)); 
         else sftp.close(fd, callback(undefined, m_fileBuffer)); 
        }); 
       }); 
      } 
     }); 
    }).connect ({ 
     host: 'server ip', 
     port: 22, 
     username: 'username', 
     password: 'password' 
    }); 
}