Using NMSSH and NMSFTP, I am able to connect to our company's remote SFTS server. I am also able to upload files to an "Incoming" folder in SFTS site. The issue is, when I try read a JSON file in the "Outgoing" folder using NMSFTP Contents(ofFile:), I keep getting NIL value. I am able to get the file name using contentsOfDirectory(atPath:). Any help is appreciated. The remote Windows 2019 server had read/write permissions for all folders I am dealing with
XCODE 12.0
iOS 14.0
Here is my code:
DispatchQueue.global(qos: .userInitiated).async { () in
let empDownloadSession = NMSSHSession(host: "host_site:22", andUsername: "user name")
empDownloadSession.connect()
if empDownloadSession.isConnected{
print("Employee download session connected")
empDownloadSession.authenticateBy(inMemoryPublicKey: nil, privateKey: self.privKey, andPassword: "password entered here")
if empDownloadSession.isAuthorized == true {
print("SFTP employee download session is authorized")
let downloadSession = NMSFTP(session: empDownloadSession)
downloadSession.connect()
if downloadSession.isConnected {
print("Employee download session is connected")
} else {
print("Error: employee download session not connected")
return
}
guard let employeeListContents = downloadSession.contentsOfDirectory(atPath: "/MailTracking/Test/Outgoing")
else {
print("Unable to read contents of file in SSH site")
return
}
print("Printing Contents of file at path")
print(employeeListContents)
print("Employee data captured")
guard let employeeList = downloadSession.contents(atPath: "/MailTracking/Test/Outgoing/")
// this is where the issue is. I get nil value
else {
print("Unable to read contents of file in SSH site")
return
}
print("Printing Contents of file at path")
print(employeeList)
do {
if let json = try JSONSerialization.jsonObject(with: self.employeeList!, options: []) as? [String : Any] {
if let empList1 = json["employees"] as? [String] {
print("Employee Details ")
print(empList1)
}
}
} catch let error as NSError {
print("Unable to create json file from employeeList data")
print("Failed to load \(error.localizedDescription)")
}
} else {
print("empDownloadSession not authorized")
}
} else {
print ("empDownloadSession is NOT connected")
}
empDownloadSession.disconnect()
}
Log:
2020-11-09 10:00:21.150336-0500 My Application [23155:9533137] NMSSH: libssh2 (v1.8.0) initialized 2020-11-09 10:00:21.150739-0500 My Application[23155:9533137] NMSSH: Start our_company_sfts_site.com resolution 2020-11-09 10:00:31.164833-0500 My Application[23155:9533140] NMSSH: Socket connection to 149.32.227.28 on port 22 failed with reason -2, trying next address... 2020-11-09 10:00:31.397388-0500 My Application[23155:9533140] NMSSH: Socket connection to 149.32.195.28 on port 22 succesful 2020-11-09 10:00:32.226092-0500 My Application[23155:9533140] NMSSH: Remote host banner is SSH-2.0-9.99 sshlib Employee download session connected 2020-11-09 10:00:32.226616-0500 My Application[23155:9533140] NMSSH: The host's fingerprint is XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:68:95:A4 2020-11-09 10:00:32.226952-0500 My Application[23155:9533140] NMSSH: SSH session started 2020-11-09 10:00:32.313983-0500 My Application[23155:9533140] NMSSH: User auth list: publickey,password SFTP employee download session is authorized 2020-11-09 10:00:37.456136-0500 My Application[23155:9533140] NMSSH: Public key authentication succeeded. Employee download session is connected Printing Contents of file at path [<NMSFTPFile: 0x28272c280> Filename: test.txt] // Above file test.txt is last of several files I tried. All of them are.json files Employee data captured No Data file creation 2020-11-09 10:00:40.921732-0500 My Application [23155:9533140] NMSSH: Disconnected Fatal error: Unexpectedly found nil while unwrapping an Optional value: file My Application/SSHEmplyeeDownload.swift, line 97 2020-11-09 10:00:40.923135-0500 My Application[23155:9533139] Fatal error: Unexpectedly found nil while unwrapping an Optional value: file My Application/SSHEmplyeeDownload.swift, line 97 (lldb)