0

I am working on a web application where one function is to connect to a remote Linux server using SFTP, download some log files, parse and display the results. I am using WinSCP to achieve this.

I got this working and tested it running from VS in my local dev box (Windows 10). I published it to a test server running Windows 2012 R2 and it fails to connect. It throws:

Timeout waiting for WinSCP to respond - WinSCP has not responded in time. There was no output. Response log file C:\Windows\TEMP\wscp3BFC.0292A297.tmp was not created.

I searched and it seems in IIS, impersonation had to be off and I had it on; that change did not fix the issue either although WinSCP support page lists it as a fix. Another WinSCP post mentioned that the executable had to be in same location as DLL or ExecutablePath should be used to specify where it is; I used that latter method.

When I remote to test server, open IIS, select the application and run it from there it works fine; so I know there are no issues with user ID, password, port or host key fingerprint.

This is the code I am using, in a class called SFTP:

private Session SFTP_Session;
private string SFTP_Host;
private int SFTP_Port;
private string SFTP_UserID;
private string SFTP_Password;
private string SFTP_SshHostKeyFingerprint;
private string ExePath;

public SFTP(string sHost, int iPort, string sUserID, string sPassword, string SshHostKeyFingerprint, string sExePath)
{
    SFTP_Host = sHost;
    SFTP_Port = iPort;
    SFTP_UserID = sUserID;
    SFTP_Password = sPassword;
    SFTP_SshHostKeyFingerprint = SshHostKeyFingerprint;
    ExePath = sExePath;
}

public void ConnectSFTP()
{
    // Set up session options
    SessionOptions sessionOptions = new SessionOptions
    {
        Protocol = Protocol.Sftp,
        HostName = SFTP_Host,
        PortNumber = SFTP_Port, 
        UserName = SFTP_UserID,
        Password = SFTP_Password,
        SshHostKeyFingerprint = SFTP_SshHostKeyFingerprint
    };
    sessionOptions.AddRawSettings("Cipher", "aes,chacha20,3des,WARN,des,blowfish,arcfour");
    SFTP_Session = new Session();
    SFTP_Session.ExecutablePath = ExePath;
    //SFTP_Session.XmlLogPath = "Z:\\Data\\WinSCP.xml";
    //SFTP_Session.SessionLogPath = @"Z:\Data\WinSCP.log";

    // Connect

    try
    {
        SFTP_Session.Open(sessionOptions);
        TraceLog.appendLog("Connected to " + SFTP_Host + ":" + SFTP_Port);
    }
    catch (Exception ex)
    {
        TraceLog.appendLog("Failed to connect to " + SFTP_Host + ":" + SFTP_Port + Environment.NewLine + "Error: " + ex.Message);
    }
}

It is called like this:

SFTP client = new SFTP(sftpHost, iSftpPort, sftpUserID, sftpPassword, sshHostKeyFingerprint, sExePath);
client.DownloadFiles();

public string DownloadFiles()
{
    ConnectSFTP();    // FAILS HERE ...
    try
    {
        using (SFTP_Session)
        {
            SFTP_Session.QueryReceived += (sender, e) =>
            {
            e.Continue();
            }
        };
        ....
    }
    catch (Exception e)
    {
        ...
    }
    finally
    {
        if (SFTP_Session != null)
        {
            SFTP_Session.Dispose();
        }
    }
}

I see in my log file (not WinSCP since it can connect):

2021-07-02 08:24:56:169|Failed to connect to 1.2.3.4:22 (ssh-ed25519-hK8U-......)
Error: Timeout waiting for WinSCP to respond - WinSCP has not responded in time. There was no output. Response log file C:\Windows\TEMP\wscp3BFC.0292A297.tmp was not created. This could indicate lack of write permissions to the log folder or problems starting WinSCP itself. 

WinSCP version is 5.17.10.0.

NoBullMan
  • 2,032
  • 5
  • 40
  • 93
  • Set `Session.DebugLogPath` and post the log. – Martin Prikryl Jul 02 '21 at 14:47
  • I did add it, recompiled, re-published and ran again. Now the darnest thing: either it works (specifically when run using IE or Edge, never works in Chrome) connecting and downloading the files; or it doesn't run in which case it does not create any log file (my log files or debug/WinSCP log files) and does not download any files; as if I did not click the menu item to initiate it. Right now, it is a crapshoot. I cannot reproduce the timeout error to see what is in debug log file. – NoBullMan Jul 02 '21 at 16:11
  • Odd times when it runs successfully, it won't run again unless I restart IIS! In Chrome it continuously refuses to run. It runs once in IE 11, Edge and FF and won't again until IIS is restarted. This is odd. – NoBullMan Jul 02 '21 at 16:20
  • Make sure you dispose the `Session` object, as all WinSCP .NET examples do. https://winscp.net/eng/docs/library#csharp – Martin Prikryl Jul 02 '21 at 17:12
  • I do that; I had added minimal code but edited and updated the post. See updated DownloadFiles() function. – NoBullMan Jul 02 '21 at 20:46
  • Well, you still do not dispose the `SFTP_Session`, when the `SFTP_Session.Open` fails. – Martin Prikryl Jul 03 '21 at 05:17
  • Anyway, when *"it won't run again"* – What does not run exactly? I understand that you do not even get the *"Timeout waiting for WinSCP to respond"*. So how far does it get? Is any of your `TraceLog` triggered at all? – Martin Prikryl Jul 03 '21 at 05:19
  • That;s what I meant by it doesn't even run. For testing, as soon as function is called, even before SFTP session is created, I log something like "Entered function". I don't even see that after first run. – NoBullMan Jul 03 '21 at 19:58
  • If you remove the `Session.DebugLogPath`, do you get your original problem back? – Martin Prikryl Jul 04 '21 at 05:30
  • I think it is probably related to how it interacts with IIS or something I am missing in setting up the connection in the application because when I remote desktop to application server (test server) where this web application is deployed and run it, it works. When I run it again, it doesn't until I stop/start or recycle application pool. Then it works again. However, when I access the site from any other machine, say from my dev box, it never works even if I recycle app pool and/or restart IIS. – NoBullMan Jul 06 '21 at 13:00
  • What is `QueryReceived` ? – Kiquenet Jul 19 '22 at 08:25

0 Answers0