Question
Token Ibragimov · Jun 11, 2020

SFTP connect

Hello,

I'm trying to upload file to SFTP server.  

I have access to SFTP server . 
My code is bellow 

Try {
Set sftp = ##class(%Net.SSH.Session).%New()

Set sc = sftp.AuthenticateWithUsername(username,password)
if 'sftp.Connect(host){
            Set status = $$$ERROR($$$GeneralError,"Cannot connect to sftp  ")
            Quit       
}

Set sc = sftp.OpenSFTP(.t)

If 'sftp.Disconnect() Write "Failed to logout",!

}Catch ex {
Set status=$$$ERROR($$$GeneralError,$ZE)
Quit
}

The output : $lb(5001,"Cannot connect to sftp "

What i'm doing wrong ?

00
1 1 10 470
Log in or sign up to continue

Replies

Docs specify that Connect should come before Authentication.

Try this code:

Try {
    Set ssh = ##class(%Net.SSH.Session).%New()

    Set sc = ssh.Connect(host)
    Write:$$$ISERR(sc) "Connect:", $System.Status.GetErrorText(sc),!
    
    Set sc = ssh.AuthenticateWithUsername(username, password)
    Write:$$$ISERR(sc) "Auth: ", $System.Status.GetErrorText(sc), !
    
    #dim sftp As %Net.SSH.SFTP
    Set sc = ssh.OpenSFTP(.sftp)
    Write:$$$ISERR(sc) "SFTP: ", $System.Status.GetErrorText(sc), !
    
    Set sc = sftp.Dir("/", .contents, , $$$YES)
    Write:$$$ISERR(sc) "Dir: ", $System.Status.GetErrorText(sc), !
    
    Zwrite contents
    
    //If 'sftp.Disconnect() Write "Failed to logout",!

} Catch ex {
    Set sc = ex.AsStatus()
    Write "Exception: ", ex.DisplayString(), !
}

Eduard thanks for the answer

Connect: ОШИБКА #7500: SSH Connect Ошибка '-2147014836': A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.  [8007274C]: Unknown error [8007274c] at ..\..\shared\Utility\utlNetSocket.cpp:1105,0                                                                          

Auth: ОШИБКА #7500: SSH AuthenticateWithUsername Ошибка '-2146435071': An internal consistency check failed

[80100001]
SFTP: ОШИБКА #7500: SSH OpenSFTP Ошибка '-2146435071': An internal consistency check failed.                                                                  [80100001]
Exception: <UNDEFINED> 9 zuploadTest+15^pgw.task.UploadToFTP.1 sftp

The code bellow works, but still I can't put file to remote server.

#dim sftp As %Net.SSH.SFTP 
    set ssh = ##class(%Net.SSH.Session).%New()
    do ssh.Connect(host)
    do ssh.AuthenticateWithUsername(username,password)
    //open an SFTP session and get that returned by reference
    do ssh.OpenSFTP(.sftp)

    set dir="C:\Registries\MonthlyReports\KARTEL\202004\30\test1.txt"
    set remotePath="sftp://TEST@77.77.33.11/NEW.txt"

   Set sc=sftp.Put(dir,remotePath)

Exception: <UNDEFINED> 9 zuploadTest+34^pgw.task.UploadToFTP.1 sftp

zw sftp

sftp=<OBJECT REFERENCE>[14@%Net.SSH.Session]
+----------------- general information ---------------
|      oref value: 14
|      class name: %Net.SSH.Session
| reference count: 2
+----------------- attribute values ------------------
|            HostKey = ""
|          (Session) = "Session@91273700"

Remote path should be just:

set remotePath="/NEW.txt"

That said, this error

Ошибка '-2147014836': A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.   

looks like you don't have the network connectivity. Have you tried to connect with Putty from the same server as Ensemble?

Yes, i have check connect using FileZillaConnect and connection is successful.

Status:    Connected to XXXXXXXX
Status:    Starting upload of C:\Registries\MonthlyReports\KARTEL\202006\08\50_8.06_8.06.xls
Status:    File transfer successful, transferred 6,065 bytes in 1 second
Status:    Retrieving directory listing of "/"...
Status:    Listing directory /
Status:    Directory listing of "/" successful

 

I think contacting the WRC would be a better option at this stage.

Hi,

We are using %Net.FtpSession to upload the file to SFTP, you can try with this.

 method OpenSFTP(ByRef sftp As %Net.SSH.SFTP) as %Status

Open up an SFTP session for SFTP activity.

 ByRef this means that the variable you pass has to be initialized first.

#dim sftp As %Net.SSH.SFTP 

 just binds the object type to variable  sftp but doesn't initialize it.
So it is <UNDEFINED>

Use your  original line instead or in addition to initalize it

Set sftp ##class(%Net.SSH.Session).%New()

Not really.

Object ByRef means pointer itself may be changed (so we need to pass a pointer to a pointer), which is exactly what happens in this method.

A more correct qualifier would be Output as old object is always discarded.

But initializing stfp before the call would serve no purpose in this case.

Fully agrees with Output.
though, is it worth a prodglog ?