Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSH / SFTP connection issue using Tamir.SharpSsh

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.

like image 712
jinsungy Avatar asked Oct 16 '09 17:10

jinsungy


2 Answers

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.

like image 105
Jeganinfo Avatar answered Oct 02 '22 15:10

Jeganinfo


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.

like image 33
Bruce Blackshaw Avatar answered Oct 02 '22 14:10

Bruce Blackshaw