KpyM Telnet/SSH Server - Forum
KTS SFTP w/ Ganymed SSH-2: uploads work, but files are empty
Derek Moore KTS SFTP w/ Ganymed SSH-2: uploads work, but files are empty
 
Kroum,

I'm attempting to use KTS' SFTP functionality to transfer files to/from Win2k8 R2 servers.

I'm coding an automated client with Ganymed SSH-2 for Java.

My sftp_get function works fine, I can download remote files from the Win2k8 R2 servers.

My sftp_put function appears to work, it creates a remote file, the upload of data seems to take place (the read/write loop works properly until EOF), but the resultant files are empty.

Any ideas? I've been digging through the KTS code, but I haven't found anything glaring just yet.


Derek Moore
 
The problem was with the old old Ganymed version I was using. I upgraded to the latest jar available at <http://www.cleondris.ch/opensource/ssh2/>, and my upload logic works perfectly now!

Code:

package org.hackunix.helpers;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.SFTPException;
import ch.ethz.ssh2.SFTPv3Client;
import ch.ethz.ssh2.SFTPv3FileAttributes;
import ch.ethz.ssh2.SFTPv3FileHandle;

public class SshHelper
{

    public static void sftp_get(String host, String user, String pass, String rpath, String lpath) throws IOException
    {
        File rfile = new File(rpath);
        File lfile = new File(lpath);
        if (lfile.exists() && lfile.isDirectory())
        {
            lfile = new File(lfile, rfile.getName());
        }

        Connection ssh = new Connection(host);
        ssh.connect();
        ssh.authenticateWithPassword(user, pass);
        SFTPv3Client sftp = new SFTPv3Client(ssh);
        SFTPv3FileHandle file = sftp.openFileRO(rpath);

        long fileOffset = 0;
        byte[] dst = new byte[32768];
        int i = 0;
        FileOutputStream output = new FileOutputStream(lfile);
        while ((i = sftp.read(file, fileOffset, dst, 0, dst.length)) != -1)
        {
            fileOffset += i;
            output.write(dst, 0, i);
        }

        output.close();
        sftp.closeFile(file);
        sftp.close();
    }

    public static void sftp_put(String host, String user, String pass, String lpath, String rpath) throws IOException
    {
        File rfile = new File(rpath);
        File lfile = new File(lpath);
        if (!lfile.exists() || lfile.isDirectory())
        {
            throw new IOException("Local file must be a regular file: " + lpath);
        }

        Connection ssh = new Connection(host);
        ssh.connect();
        ssh.authenticateWithPassword(user, pass);
        SFTPv3Client sftp = new SFTPv3Client(ssh);

        try
        {
            SFTPv3FileAttributes attr = sftp.lstat(rpath);
            if (attr.isDirectory())
            {
                rfile = new File(rpath, lfile.getName());
            }
        }
        catch (SFTPException e)
        {
            try
            {
                SFTPv3FileAttributes attr = sftp.lstat(rfile.getParent());
                if (!attr.isDirectory())
                {
                    throw new IOException("Remote file's parent must be a directory: " + rpath, e);
                }
            }
            catch (SFTPException ex)
            {
                throw new IOException("Remote file's parent director must exist: " + rpath, ex);
            }
        }
        SFTPv3FileHandle file = sftp.createFileTruncate(rfile.getCanonicalPath());

        long fileOffset = 0;
        byte[] src = new byte[32768];
        int i = 0;
        FileInputStream input = new FileInputStream(lfile);
        while ((i = input.read(src)) != -1)
        {
            sftp.write(file, fileOffset, src, 0, i);
            fileOffset += i;
        }

        input.close();
        sftp.closeFile(file);
        sftp.close();
    }

    public static void scp_get(String host, String user, String pass, String rpath, String lpath) throws IOException
    {
        Connection ssh = new Connection(host);
        ssh.connect();
        ssh.authenticateWithPassword(user, pass);
        SCPClient scp = ssh.createSCPClient();
        scp.get(rpath, lpath);
    }

    public static void scp_get(String host, String user, String pass, String[] rpath, String lpath) throws IOException
    {
        Connection ssh = new Connection(host);
        ssh.connect();
        ssh.authenticateWithPassword(user, pass);
        SCPClient scp = ssh.createSCPClient();
        scp.get(rpath, lpath);
    }

    public static void scp_put(String host, String user, String pass, String lpath, String rpath) throws IOException
    {
        Connection ssh = new Connection(host);
        ssh.connect();
        ssh.authenticateWithPassword(user, pass);
        SCPClient scp = ssh.createSCPClient();
        scp.put(lpath, rpath);
    }

    public static void scp_put(String host, String user, String pass, String[] lpath, String rpath) throws IOException
    {
        Connection ssh = new Connection(host);
        ssh.connect();
        ssh.authenticateWithPassword(user, pass);
        SCPClient scp = ssh.createSCPClient();
        scp.put(lpath, rpath);
    }

}



Joseph Lawson
 
Amazing post! this could be more fascinating if you share little more about the character of stories like initial information could be more interesting. Anyhow now you have shared <a href="https://www.bestessayservicereviews.com/">top writing websites</a> next time you can add some spice to your story. So think about it.


uzair9@
 
If more people that write articles really concerned themselves with writing great content like you, more readers would be interested in their writings. Thank you for caring about your content.
Buy Kratom http://www.goodtimebob.com/content-of-kratom-leaves-and-side-effects.html


uzairawan
 
Investment casting is one of the oldest forms of creating precision parts in metal. It is also known as the lost wax method. The process involves creating a wax pattern which is essentially a replica of the desired part in steel.
https://dawangcasting.com/stainless-steel-casting


jonnybaba1
 
I went over this website and I believe you have a lot of wonderful information, saved to my bookmarks
BetSafe casino https://thejerny.com/travel/things-to-do-in-las-vegas-during-quarantine/


uzairawan
 
Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!Thanks
eGOS submission http://www.visionpluspms.co.uk/


Mushtaq <a href="https://www.londonittraining.co.uk/ccna-tra
 
Please let me know if you?re looking for a article writer for your site. You have some really great posts and I feel I would be a good asset. If you ever want to take some of the load off, I?d absolutely love to write some material for your blog in exchange for a link back to mine. Please send me an email if interested. Thank you!
ccna training london https://www.londonittraining.co.uk/ccna-training-courses-london


muhfdagh
 
Today, I was just browsing along and came upon your blog. Just wanted to say good blog and this article helped me a lot, due to which I have found exactly I was looking. indian visa eligibility
https://bit.ly/3i70nW5


 

© 2007 - 2008 Kroum Grigorov
Powered by phpBB © 2001, 2005 phpBB Group