Having trouble using SFTP from VB.NET (2010) with WinSCP
Problems are:
1. GetAndStoreFingerprint fails. (I can get to the FTP site via FileZilla so I know that the url, user, password and port is correct).
hostkey="*" used since the routine
2.
"winscp> open sftp://user@test.com:@ftp.text.com:22999 -hostkey-"*" Too many parameters for command 'open'. winscp> ls No session. winscp> put c:\lxcg.log No session. winscp>
Here is the code
Dim hostKey As String = Nil
'hostKey = GetAndStoreFingerprint(pHost:=pURL, pOutReason:=pOutReason) ' This always fails
If hostKey = Nil Then
hostKey = "*"
'GoTo exitfunction
End If
Dim OpenConnectStr As String = "open sftp://" & pUser & ":" & pPswd & "@" & pURL & ":22999" '& " -hostkey-" & DQ & hostKey & DQ
If hostKey <> Nil Then
OpenConnectStr &= " -hostkey-" & DQ & hostKey & DQ
End If
' My.Computer.FileSystem.WriteAllText(logname, "", True)
' Run hidden WinSCP process
Dim winscp As Process = New Process()
winscp.StartInfo.FileName = ProgramFiles & "\WinSCP\winscp.com"
winscp.StartInfo.Arguments = "/xmllog=" + logname
winscp.StartInfo.UseShellExecute = False
winscp.StartInfo.RedirectStandardInput = True
winscp.StartInfo.RedirectStandardOutput = True
winscp.StartInfo.CreateNoWindow = True
' winscp.StartInfo.host = pURL
Dim process As New Process
If Not winscp.Start() Then
i = i
End If
' Feed in the scripting commands
winscp.StandardInput.WriteLine(OpenConnectStr)
'winscp.StandardInput.WriteLine("option batch abort")
'winscp.StandardInput.WriteLine("option confirm off")
' winscp.StandardInput.WriteLine("open mysession")
' winscp.StandardInput.WriteLine("port 22999")
Private Function GetAndStoreFingerprint(ByRef pHost As String, _
Optional ByRef pOutReason As String = Nil) As String
Dim i As Integer = 0
Dim s As String = Nil, Ss As String = Nil
Dim FunRC As String = Nil
On Error GoTo ErrX
retB = 0 : pOutReason = Nil
'attempt a connect and load the fingerprint from initial connect.
Dim startInfo As New ProcessStartInfo
startInfo.FileName = ProgramFiles & "WinSCP\WinSCP.com"
startInfo.RedirectStandardInput = True
startInfo.RedirectStandardOutput = True
startInfo.UseShellExecute = False
startInfo.CreateNoWindow = True
startInfo.WindowStyle = ProcessWindowStyle.Hidden
Dim process As New Process
process.StartInfo = startInfo
process.Start()
process.StandardInput.WriteLine("open " & pHost)
process.StandardInput.Close()
process.WaitForExit()
'read the first line from the output of the command.
Dim response As String = process.StandardOutput.ReadLine
Dim rsa As String = ""
'loop through all lines of the output for the line containing
'the servers fingerprint (hostkey)
s = Nil
While response <> Nothing
If response.Contains("ssh-rsa") Then
rsa = response
Exit While
End If
s &= response & SP
response = process.StandardOutput.ReadLine
End While
winscp.StandardInput.WriteLine("ls")
winscp.StandardInput.WriteLine("put " & pFFn)
winscp.StandardInput.Close()
' Collect all output (not used in this example)
Dim output As String = winscp.StandardOutput.ReadToEnd()
' Wait until WinSCP finishes
winscp.WaitForExit()
I have been trying various combinations for hours.
Please help.