Getting the SFTP Host Key Programmatically
I'm not finding a way in the API to silently discover the Host Key at runtime. This doesn't mean that there's not a way to do it--just that if it's there I'm not finding it.
So I've come up with an alternate method, using WinSCP.com. I'm including my VB.NET code below for review and suggestion.
As far as I can tell, WinSCP.com doesn't use STDIN and STDOUT for its interactive Host Key prompts. The process emits several STDOUT calls and immediately exits, without waiting for STDIN to simulate user interaction. Because of this I've handled the output asynchronously, using the Process_OutputDataReceived event. The Process.WaitForExit() call is essential for the operation.
This is clunky to be sure, but I can see no other way presently. My plan is to present a form for entry of the key that the user has already obtained from the server administrator, so that I can compare that manually entered key against this one that I'm getting programmatically. Upon a successful match my app will persist the key and proceed.
If the ability to discover the Host Key doesn't already exist in the API, I'd like to enter it as a feature request. Thank you Martin, for all the hard work you do.
In the meantime, I'd be interested to know of any downsides to this approach--memory leaks, etc.
Thanks,
Jeff Bowman
Fairbanks, Alaska
So I've come up with an alternate method, using WinSCP.com. I'm including my VB.NET code below for review and suggestion.
As far as I can tell, WinSCP.com doesn't use STDIN and STDOUT for its interactive Host Key prompts. The process emits several STDOUT calls and immediately exits, without waiting for STDIN to simulate user interaction. Because of this I've handled the output asynchronously, using the Process_OutputDataReceived event. The Process.WaitForExit() call is essential for the operation.
This is clunky to be sure, but I can see no other way presently. My plan is to present a form for entry of the key that the user has already obtained from the server administrator, so that I can compare that manually entered key against this one that I'm getting programmatically. Upon a successful match my app will persist the key and proceed.
If the ability to discover the Host Key doesn't already exist in the API, I'd like to enter it as a feature request. Thank you Martin, for all the hard work you do.
In the meantime, I'd be interested to know of any downsides to this approach--memory leaks, etc.
Thanks,
Jeff Bowman
Fairbanks, Alaska
Public Class Main Private HostKey As String Private Sub cmdTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTest.Click File.Delete("WinScp.ini") Using oProcess As New Process AddHandler oProcess.OutputDataReceived, AddressOf Process_OutputDataReceived oProcess.StartInfo.UseShellExecute = False oProcess.StartInfo.FileName = "WinScp.com" oProcess.StartInfo.Arguments = "/command ""open server.com""" oProcess.StartInfo.RedirectStandardInput = True oProcess.StartInfo.RedirectStandardOutput = True oProcess.StartInfo.CreateNoWindow = True oProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden oProcess.Start() oProcess.BeginOutputReadLine() oProcess.WaitForExit() RemoveHandler oProcess.OutputDataReceived, AddressOf oProcess_OutputDataReceived End Using File.Delete("WinScp.ini") End Sub Private Sub Process_OutputDataReceived(sender As Object, e As DataReceivedEventArgs) If Trim(e.Data).ToLower.StartsWith("ssh-rsa") Then Me.HostKey = e.Data End If End Sub End Class