You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS应用用NMSSH连接自建SFTP服务器失败,测试服务器正常

iOS使用NMSSH连接自建SFTP服务器失败问题

我在iOS应用中使用NMSSH库连接SFTP服务器,用于下载JSON文件并保存到本地目录。目前能成功连接一款公开测试服务器并完成文件下载,但连接自建服务器时,在SSH会话建立阶段失败。


测试服务器凭证与成功日志

连接凭证代码

let host = "test.rebex.net" 
let port = 22  
let user = "demo"
let password = "password"
let fileToDownload = "readme.txt"

成功连接日志

2023-07-26 16:33:35.514732+0500 FTP[42664:5996676] NMSSH: libssh2 (v1.8.0) initialized
2023-07-26 16:33:35.514788+0500 FTP[42664:5996676] NMSSH: Start test.rebex.net resolution
2023-07-26 16:33:35.937435+0500 FTP[42664:5996672] NMSSH: Socket connection to 194.108.117.16 on port 22 succesful
2023-07-26 16:33:36.984023+0500 FTP[42664:5996672] NMSSH: Remote host banner is SSH-2.0-RebexSSH_5.0.8466.0
2023-07-26 16:33:36.984372+0500 FTP[42664:5996672] NMSSH: The host's fingerprint is 03:61:C4:98:F1:FF:7D:23:97:51:07:13:88:B8:C5:55
2023-07-26 16:33:36.984533+0500 FTP[42664:5996672] NMSSH: SSH session started
2023-07-26 16:33:37.148409+0500 FTP[42664:5996672] NMSSH: User auth list: password,keyboard-interactive,publickey
2023-07-26 16:33:37.298370+0500 FTP[42664:5996672] NMSSH: Password authentication succeeded.

Optional("Welcome,\r\n\r\nYou are connected to an FTP or SFTP server used for testing purposes by Rebex FTP/SSL or Rebex SFTP sample code.\r\nOnly read access is allowed and the FTP download speed is limited to 16KBps.\r\n\r\nFor information about Rebex FTP/SSL, Rebex SFTP and other Rebex .NET components, please visit our website at https://www.rebex.net/\r\n\r\nFor feedback and support, contact support@rebex.net\r\n\r\nThanks!\r\n")

自建服务器错误日志

2023-07-26 16:36:47.112351+0500 FTP[42687:5998750] NMSSH: libssh2 (v1.8.0) initialized
2023-07-26 16:36:47.112440+0500 FTP[42687:5998750] NMSSH: Start 52.6.162.79 resolution
2023-07-26 16:36:47.372795+0500 FTP[42687:5998755] NMSSH: Socket connection to 52.6.162.79 on port 22 succesful
2023-07-26 16:36:48.017514+0500 FTP[42687:5998755] NMSSH: Failure establishing SSH session
2023-07-26 16:36:48.017645+0500 FTP[42687:5998755] NMSSH: Disconnected
Connection failed.

连接服务器代码片段

func readSFTPFile() {
    let session = NMSSHSession(host: host, port: port, andUsername: user)
    session.connect()

    if session.isConnected {
        session.authenticate(byPassword: password)

        if session.isAuthorized {
            let sftp = NMSFTP.connect(with: session)

            if sftp != nil {
                let remotePath = fileToDownload
                let localPath = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Downloads/\(fileToDownload)")

                let fileData = sftp.contents(atPath: fileToDownload)
                let StrValue = String(data: fileData!, encoding: String.Encoding.utf8)
                print(StrValue)
                
                sftp.disconnect()
            } else {
                print("Error connecting to SFTP server.")
            }
        } else {
            print("Authentication failed.")
        }
        session.disconnect()
    } else {
        print("Connection failed.")
    }
}

疑问

为何测试服务器连接正常,自建服务器却在SSH会话建立阶段失败?是否需要在服务器端进行某些配置变更,以允许iPhone连接?


内容的提问来源于stack exchange,提问作者Ammar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 11:07:18