5

This is my code to connect and send a file to a remote SFTP server.

public static void SendDocument(string fileName, string host, string remoteFile, string user, string password)
        {            
            Scp scp = new Scp();

            scp.OnConnecting += new FileTansferEvent(scp_OnConnecting);
            scp.OnStart += new FileTansferEvent(scp_OnProgress);
            scp.OnEnd += new FileTansferEvent(scp_OnEnd);
            scp.OnProgress += new FileTansferEvent(scp_OnProgress);

            try
            {
                scp.To(fileName, host, remoteFile, user, password);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

I can successfully connect, send and receive files using CoreFTP. Thus, the issue is not with the server. When I run the above code, the process seems to stop at the scp.To method. It just hangs indefinitely.

Anyone know what might my problem be? Maybe it has something to do with adding the key to the a SSH Cache? If so, how would I go about this?

EDIT: I inspected the packets using wireshark and discovered that my computer is not executing the Diffie-Hellman Key Exchange Init. This must be the issue.

EDIT: I ended up using the following code. Note, the StrictHostKeyChecking was turned off to make things easier.

            JSch jsch = new JSch();
            jsch.setKnownHosts(host);

            Session session = jsch.getSession(user, host, 22);
            session.setPassword(password);

            System.Collections.Hashtable hashConfig = new System.Collections.Hashtable();
            hashConfig.Add("StrictHostKeyChecking", "no");
            session.setConfig(hashConfig);

            try
            {
                session.connect();

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

                c.put(fileName, remoteFile);

                c.exit();            
            }
            catch (Exception e)
            {
                throw e;
            }

Thanks.

3
  • Have you tried watching the traffic with WireShark? it would be interesting to see if the request ever even made it out. Commented Oct 16, 2009 at 17:33
  • try making the connection manually with something like pscp (from the makers of PuTTY) and see what happens. Commented Oct 16, 2009 at 17:39
  • @ryber, I inspected packets and discovered that it stops right before the Diffie-Hellman Key Exchange Init.. Commented Oct 16, 2009 at 18:03

2 Answers 2

6

I use Tamir.SharpSSH - latest version 1.1.1.13

This has a class SFTP. You can use this class directly to do SFTP instead of using JSch, Session class.

Quick Sample here:

127.0.0.1 - Server IP

/SFTPFolder1/SFTPFolder2 - Server Location Where I want my files to go

        Sftp sftpClient = new Sftp("127.0.0.1", "myuserName", "MyPassword");

        sftpClient.Connect();

        sftpClient.Put(@"C:\Local\LocalFile.txt", "/SFTPFolder1/SFTPFolder2");

Let me know if you have any issues.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, How can I use private & public keys which generated by "PuTTY Key Generator" to connect to openSSH sftp server with Tamir.SharpSHH clinet?. Thanks.
i converted private key file PuttyKey format to OpenSSH format using "Puttykeygen.exe" Conversions->Export OpenSSH Key. Thanks
1

Without looking at your log files it is hard to tell what the issue is.

However keep in mind that SCP is not SFTP - they are completely different protocols that run over SSH. It is possible that your SFTP does not actually support SCP - not all SFTP servers do. CoreFTP may be using SFTP.

Our commercial package, edtFTPnet/PRO, might also be worth trying, if only as an alternative to try to get a different client working against your server.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.