Topic "Having trouble using SFTP from VB.NET (2010) with WinSCP"

Author Message
QuickBooksDev
[View user's profile]

Joined: 2013-05-17
Posts: 11
Location: Daytona Beach, FL USA
Not sure exactly how things should be setup. I copied the example but not having any success.

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.
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
1) I recommend you to use WinSCP .NET assembly, instead of scripting:
http://winscp.net/eng/docs/library

2) Make sure you understand security consequences of using * for hostkey.

3) There should be equal sign after hostkey, not dash:
open sftp://user@test.com:@ftp.text.com:22999 -hostkey="*"
Guest




Changed hostkey- to hostkey= and now I get a different error
hostkey="*" -hostkey="*" Unknown switch 'hostkey'. winscp> ls No session. winscp>

Can you tell me why the GetAndStoreFingerprint fails. That should fix the hostkey issue.

It is my understanding that hostkey="*" will will blinding connect to the url that was provided. Is there more?

Why would the assembly be better?
QuickBooksDev
[View user's profile]

Joined: 2013-05-17
Posts: 11
Location: Daytona Beach, FL USA
Can anyone tell me what is wrong with the hostkey paramenter?
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
Anonymous wrote:
hostkey="*" -hostkey="*" Unknown switch 'hostkey'. winscp> ls No session. winscp>

You are possibly using a very old version of WinSCP. Make sure you have the latest. Otherwise please post a complete session log file.

Quote:
Can you tell me why the GetAndStoreFingerprint fails. That should fix the hostkey issue.

Once you make the -hostkey=* working, there's no point for the overkill with GetAndStoreFingerprint. It's no safer than -hostkey=*
_________________
Martin Prikryl
QuickBooksDev
[View user's profile]

Joined: 2013-05-17
Posts: 11
Location: Daytona Beach, FL USA
It was just downloaded. Reads File version 4.4.0.1904. Date modified 1/27/2013 10:54 PM

I think that I got past the invalid hostkey but getting
winscp> open sftp://demo-quickbooks@xxx.com:@ftp.xxx.com:22999 -hostkey="*" Searching for host... Connecting to host... Authenticating... The server's host key was not found in the cache

If I remove the ftp. from the url I get a time out.

I am uncertain about what the various parameters are i.e. from winscp.StandardInput.WriteLine =

Dim hostKey As String = Nil
If hostKey = Nil Then
hostKey = "*"
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
Dim winscp As Process = New Process()
winscp.StartInfo.FileName = AaaTeXProdExePath & "\winscp.com"
winscp.StartInfo.Arguments = "/xmllog=" + logname
winscp.StartInfo.UseShellExecute = False
winscp.StartInfo.RedirectStandardInput = True
winscp.StartInfo.RedirectStandardOutput = True
winscp.StartInfo.CreateNoWindow = True

Dim process As New Process

If Not winscp.Start() Then
i = i
End If

' Feed in the scripting commands
winscp.StandardInput.WriteLine(OpenConnectStr)
wnscp.StandardInput.WriteLine("option batch abort")
wwinscp.StandardInput.WriteLine("option confirm off")
winscp.StandardInput.WriteLine("open mysession")

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()
QuickBooksDev
[View user's profile]

Joined: 2013-05-17
Posts: 11
Location: Daytona Beach, FL USA
The rest of the SFTP response is
If you trust this host, press Yes. To connect without adding host key to the cache, press No.

How to you reply Yes????
QuickBooksDev
[View user's profile]

Joined: 2013-05-17
Posts: 11
Location: Daytona Beach, FL USA
We have gotten past the previous issue by parsing StandardOutput results to get the hostkey then retrying.

Then the file uploaded.

What is really needed is all the parameters for SFTP and how to specify them and what response to get.

We did upload a file but
1. winscp.ExitCode is 1 not 0
2. It does seem to replace the file that is there. Is there a way to check or not overwrite it.
3. The StanardOutput results did not state it. It was:
cp> open sftp://test@Test.com:test.com:22999 -hostkey="ssh-dss 1024 xxxxx" Searching for host... Connecting to host... Authenticating... Using username "test@test.com". Authenticating with pre-entered password. Authenticated. Starting the session... Reading remote directory... Session started. Active session: [1] test@Test.com.com@ftp.test.com winscp> option batch continue batch continue winscp> option confirm off confirm off winscp> open mysession Searching for host... Host does not exist winscp> ls D--------- 0 0 .. winscp> put C:\IpNotFound.txt C:\IpNotFound.txt
QuickBooksDev
[View user's profile]

Joined: 2013-05-17
Posts: 11
Location: Daytona Beach, FL USA
Bump

Am I missing something here. How can I be assumed that the transfer actual worked? What are the other parameters that might be used and how codes should be checked for good or bad results.
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
QuickBooksDev wrote:
It was just downloaded. Reads File version 4.4.0.1904. Date modified 1/27/2013 10:54 PM

The -hostkey=* is supported only since 5.2 beta. 4.4 is not even the latest stable version.
_________________
Martin Prikryl
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
QuickBooksDev wrote:
How can I be assumed that the transfer actual worked?

Check the exit code. I know you are doing this already. But when it's 1, you have some error there.
The "Host does not exist" in your output looks like a key.
_________________
Martin Prikryl
QuickBooksDev
[View user's profile]

Joined: 2013-05-17
Posts: 11
Location: Daytona Beach, FL USA
Sorry but I just do not know what to look for. That is why I posted the question since the exit code as a 1.

I do not understand your other comment
Quote:
The -hostkey=* is supported only since 5.2 beta. 4.4 is not even the latest stable version.

When I downloaded it a could of weeks ago it was the latest. According to the web page now (http://winscp.net/eng/download.php) it still is and 5.2.1 is beta?????????

If hostkey is not supported then how can it be accepted and the file is being uploaded.
The release notes have
Quote:
Added switch /hostkey to command-line and open script command to automatically accept host keys with given fingerprint
. under 4.1 beta.

Doesn't make much sense to me.

What are the input parameters and what can I expect as output and where?

How can I get the ExitCode to be 0 and how can I tell what the 'error' is?
QuickBooksDev
[View user's profile]

Joined: 2013-05-17
Posts: 11
Location: Daytona Beach, FL USA
Hi

I see that the WinSCP was just upgraded to 5.1.5.

I put the WinSCP.exe and WinSCP.com into my test machine but I am having the exact same issues as before.

Can I please get some concrete assistance?

Why doesn't GetAndStoreFingerprint work? Get
winscp> open ftp.nnnnnnn.com Searching for host... Network error: Connection timed out. winscp>

So I used hostkey=* which gives me an error
The server's host key was not found in the cache. ...
but does give me the fingerprint which I then use in the hostkey and that does work (The file does get uploaded) but gives me an exitcode=1.


What are the commands to send via StandardInput and what are the expected responses?

How can I get an ExitCode=0 or know that the file has been uploaded successfully?

Last edited by QuickBooksDev on 2013-05-31 14:15; edited 1 time in total
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
Let's start from a scratch. Please attach your current complete code, and complete session log file showing the problem, for the -hostkey=* scenario.
_________________
Martin Prikryl
QuickBooksDev
[View user's profile]

Joined: 2013-05-17
Posts: 11
Location: Daytona Beach, FL USA
I had to replace the actual url and hostkey

GetAndStoreFingerprint is called with a URL as ftp.nnnnnn.com which does work with the upload the 2nd time.
The GetAndStoreFingerprint response contains
"winscp> open ftp.nnnnnnncom Searching for host... Network error: Connection timed out. winscp> "
The log has
<?xml version="1.0" encoding="UTF-8"?>
<session xmlns="http://winscp.net/schema/session/1.0" name="ftp.nnnnnnn.com" start="2013-05-31T12:55:22.969Z">
<failure>
<message>Network error: Connection timed out.</message>
</failure>
</session>

We continue and try the upload in Function SFTP with hostkey=*
Output is
? output
"winscp> open sftp://nnnnnnnn@nnnnnnnn.com:nnnnn@ftp.nnnnnnnnn.com:22999 -hostkey="*" Searching for host... Connecting to host... Authenticating... The server's host key was not found in the cache. You have no guarantee that the server is the computer you think it is. The server's dss key fingerprint is: ssh-dss 1024 nnnnhostkeynnnn If you trust this host, press Yes. To connect without adding host key to the cache, press No. To abandon the connection press Cancel. Continue connecting and add host key to the cache? (Y)es, (N)o, C(a)ncel, (C)opy Key: Cancel Host key wasn't verified! Host key fingerprint is ssh-dss 1024 nnnnhostkeynnnn. Authentication failed. winscp> option batch continue batch continue winscp> option confirm off confirm off winscp> open mysession Searching for host... Host does not exist winscp> ls No session. winscp> put C:\IpNotFound.txt No session. winscp>
"
Log has
<session name="mysession" start="2013-05-31T12:58:21.390Z"><failure><message>Host does not exist</message></failure></session>

Then retry after getting the hostkey from the above response

The ExitCode is 1 on the 2nd upload even though the upload did work.

Code attached.
WinSCP.txt (11.46 KB) [Download]

Description: (none)

QuickBooksDev
[View user's profile]

Joined: 2013-05-17
Posts: 11
Location: Daytona Beach, FL USA
The last log where the upload did work has invalid XML. It will not open with Firefox, etc. Has
<failure>
<message>Host does not exist</ <ls>
<destination value="/" />
<files>
File is attached. File extension changed from xml to txt.

But it does have that the result success="true" for everything. It uploads the file and does 'touch' it. Just it returns ExitCode=1 and Host does not exist
???????????????
CheckExportFTPlog4.txt (799 Bytes) [Download]

Description: (none)

martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
Ok, I see. You are opening two sessions in your script (OpenConnectStr and open mysession, both writing to the same XML log file, overwriting one another. I suppose that the open mysession should not be there at all.
_________________
Martin Prikryl
QuickBooksDev
[View user's profile]

Joined: 2013-05-17
Posts: 11
Location: Daytona Beach, FL USA
Ok that solved the ExitCode problem and the invalid XML. Thanks

There is only 1 in GetAndStoreFingerprint
process.StandardInput.WriteLine("open " & pHost)
and that fails.

Any ideas on why?
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
Again, you do not need the GetAndStoreFingerprint. Any problem, now that you have removed the second open, with -hostkey=*? If you have, please attach a complete session log file (log=path_to_log.log).
_________________
Martin Prikryl
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License