3

I am trying to get a JSON file from my server to my iPhone. I am using NMSSH for my FTP connection and the connection works.

But I can't retrieve any data from my server. It doesn't matter what path I am using ~/ or / or /someFolder/ or someFolder. Every time, I am getting the error:

NMSSH_Test[] NMSSH: Could not open file at path /testDirectory (Error 0: )

Here my code:

class Data {
    init() {
        let session = NMSSHSession.init(host: "someIp", andUsername: "user")
        session.connect()
        if session.isConnected{
            session.authenticate(byPassword: "password")
            if session.isAuthorized {
                let sftpsession = NMSFTP(session : session)
                sftpsession.contents(atPath: "/testDirectory")
//                if (sftpsession.contentsOfDirectory(atPath: "~/") != nil) {
//                    print("Found Data!")
//                }
            }
        }
    }
}

sandpat
  • 1,478
  • 12
  • 30
Max
  • 683
  • 2
  • 8
  • 12
  • connection works without problems. I can also send data to my server. – Max Jan 21 '20 at 18:50
  • What about checking the error conditions on an `else` branch of each if ? – Christophe Jan 21 '20 at 19:41
  • Found the solution. I also have to use .connect() for the sftpsession variable that I created. So I have to use it twice if im correct. It works! – Max Jan 22 '20 at 17:36
  • If you made it work, would you mind posting an answer with a couple of explanations, so that people who have the same problem can solve it as well? – Christophe Jan 22 '20 at 17:39

1 Answers1

0

Try with below code:

let session = NMSSHSession(host: serverHost, port: portNo, andUsername: serverUsername)
session.connect()
if session.isConnected{
         let privateKeypath:String = Bundle.main.path(forResource: "mykey", ofType: "")!
         let privateKey: String = try! String(contentsOfFile: privateKeypath, encoding: String.Encoding.utf8)
                
         session.authenticateBy(inMemoryPublicKey: "", privateKey: privateKey, andPassword: passwordStr)
                
         if session.isAuthorized == true {
                let sftpsession = NMSFTP(session: session)
                sftpsession.connect()
                if sftpsession.isConnected {
                     print("download session is connected")
                }
                else {
                     print("Error: download session not connected")
                return
                }
                guard let directoryContents = downloadSession.contentsOfDirectory(atPath: "Your Directory Path goes here!")
                else {
                     print("Unable to read contents of file in SSH site")
                return
                }
                print("Data at specified path captured: " + directoryContents)
                 
                guard let contentList = downloadSession.contents(atPath:  "Enter your path here!")
                else {
                     print("Unable to read contents of file in SSH site")
                return
                }
                print("Printing Contents of file at path")
                print(contentList)             
                }
            }
       }
Dharman
  • 30,962
  • 25
  • 85
  • 135
Avneet Singh
  • 55
  • 1
  • 11
  • I have been trying to get a file from remote SFTS server to I-phone for two years now. I read several articles in this site as well as in GITHUB and other areas. I keep getting the error unable to open file at remote location. I have checked read/write permissions in the location and there are no issues. I can get a list of files in the directory, but not get a file from SFTS using contents(atPAth: ). Please see my code below. Any help is apprecated – Dharma Apr 11 '22 at 14:56
  • Can you share your code? – Avneet Singh Apr 25 '22 at 22:03