Xenalin
Xenalin

Reputation: 223

JSch - Server "home" path is always "/" with FreeSSHd and can't change directory

I set-up a server with FreeSSHd and am able to Putty it, including changing directories and listing files. I have some example .txt files and a folder in the home directory. I set the home directory on my server to "C:\SFTP" using FreeSSHd (as opposed to defining a HOME variable with the directory being "$HOME\").

Apparently, when using JSch,

        JSch jsch = new JSch();
        session = jsch.getSession(username,host,port);

        jsch.addIdentity(key.getAbsolutePath());

        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");

        session.setConfig(config);
        session.setUserInfo(new MyUserInfo());
        session.connect();

        channel = session.openChannel("sftp");
        channel.connect();
        channelSftp = (ChannelSftp)channel;

        System.out.println("Home: "+channelSftp.getHome());

the last line prints just "Home: /". Any attempts (made immediately after above code) to use

channelSftp.cd(WORKINGDIR);

results in

2: No such file
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2833)
at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:2185)
at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:1295)
at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:1267)
at test.SFTPTest.main(SFTPTest.java:71)

I think if I got to the root of why JSch does not have the correct home path (or any?) this would work. Also, strangely enough I upload and download files no problem using put() and get().

I've heard all kinds of things where people look into the source and find it does strange things in resolving paths and something with a method called "_realPath()" and superfluous leading/trailing "/" but I don't even have it telling me the home directory is correct after connecting.

Any ideas?

Upvotes: 1

Views: 7159

Answers (2)

Thank´s everybody for your comments.

OS windows XP,I installed FreeSSHd and setting the default directory, then, when I tryng connect by console ssh, the directory was "/", I writing chdir but directory was: C:\Windows\system32\ this is very confused...

My Java code:

public void recursiveFolderUpload(String sourcePath, String destinationPath) throws FileNotFoundException {



        if (c == null || session == null || !session.isConnected() || !c.isConnected()) {
            log.debug("Connection to server is closed. Open it first.");
        }

        try {

            // c.put(sourceFile, destinationFile);
            // log.info("Upload successfull.");
            File sourceFile = new File(sourcePath);
            if (sourceFile.isFile()) {
                // copy if it is a file
                c.cd(destinationPath);
                if (!sourceFile.getName().endsWith("."))
                    c.put(new FileInputStream(sourceFile), sourceFile.getName(), c.OVERWRITE);
            } else {
                log.info("Inside else " + sourceFile.getName());
                File[] files = sourceFile.listFiles();

                if (files != null && !sourceFile.getName().startsWith(".")) {
                    log.info("Directory remote server: " + c.pwd());
                    c.cd(destinationPath);
                    SftpATTRS attrs = null;

                    // check if the directory is already existing
                    try {
                        attrs = c.stat(destinationPath + sourceFile.getName());
                    } catch (Exception e) {
                        log.warn(destinationPath + sourceFile.getName() + " not found");
                        //e.printStackTrace();
                    }

                    // else create a directory
                    if (attrs != null) {
                        log.info("Directory exists IsDir : " + attrs.isDir());
                    } else {
                        log.info("Creating dir /" + sourceFile.getName());
                        c.mkdir(sourceFile.getName());
                    }

                    for (File f : files) {

                        if(!f.getName().contains(".dtd")){
                            log.info("Uploading file: " + f.getAbsoluteFile());
                            recursiveFolderUpload(f.getAbsolutePath(), destinationPath + sourceFile.getName() + "/");
                        }           
                    }
                }

            }
        } catch (SftpException e) {
            e.printStackTrace();
        }

    }

My solution was only put the "/" en the input parameter destinationPath of method called recursiveFolderUpload

In other words, my properties file than so:

properties.host             = IP
properties.user             = user
properties.pass             = pass
properties.port             = port
properties.dir              = /    ---> This points to the directory configured by default in opensshd within windows 

Thank so much everthing again.

Upvotes: 1

mendely
mendely

Reputation: 1

SFTP directory should be the directory of the current user is not necessarily SFTP services with the directory, I also encountered the same problem, because I am with the directory and the user's default directory settings.

Upvotes: 0

Related Questions