我在我的ios应用程序中使用NMSSH库来打开与服务器的连接并下载json文件并将其保存到本地目录。
我尝试了两个不同的 sFTP 服务器来打开连接。一台是我自己的服务器,另一台是我在互联网上找到的虚拟服务器。现在的问题是我能够成功打开连接并从互联网上找到的虚拟服务器下载文件。(附有屏幕截图。)
日志:
凭证:
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 [email protected]\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.")
}
}
我很困惑,为什么与虚拟服务器的连接工作正常,而不是与我自己的服务器连接。我需要在服务器端进行任何配置更改才能允许 Iphone 与服务器连接吗?
有关虚拟服务器的一些信息: