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.