SFTP for NAV 2009

Hi,

I was using FTP on classic client which was working fine
Now it has been changed to SFTP
can you please tell me how can I use SFTP
WinSCP does not work on classic client 2009
Any other standard/free api/automation ?

Thanks
Ajay Jain
UK

Answers

  • txerifftxeriff Posts: 228Member
    edited 2018-11-29
    Well, I´ve done sftp implementation for some customer in the past I recall I used winscp dotnet variable and adapted a piece of code, I hope this helps. Sftp key can be found when logging from windows client, you can store it in customer card or like this. Dotnet variables can be found in winscp website, they need to go to service folder so they distribute automatically

    About classic client, the only way I see is to call a exposed function from NAV WS so this way it will run RTC.
    :


    PROCEDURE [email protected]([email protected] : 'Upload,List,Delete';[email protected] : Text[50];[email protected] : Text[50];[email protected] : Text[250];[email protected] : Text[250];[email protected] : Text[250];[email protected] : Text[250]) vSuccess : Boolean;
    VAR
    [email protected] : DotNet "'WinSCPnet, Version=1.3.7.7601, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.SessionOptions";
    [email protected] : DotNet "'WinSCPnet, Version=1.3.7.7601, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.Session";
    [email protected] : DotNet "'WinSCPnet, Version=1.3.7.7601, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.TransferOptions";
    [email protected] : DotNet "'WinSCPnet, Version=1.3.7.7601, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.TransferOperationResult";
    [email protected] : Text;
    [email protected] : Integer;
    [email protected] : Text;
    BEGIN

    vSuccess:=FALSE;
    gsFTPError:=FALSE;
    WinSCPSessionOptions := WinSCPSessionOptions.SessionOptions;
    vHost:=COPYSTR(gHost,7,STRLEN(gHost)); //remove port. we start after ftp://
    EVALUATE(vPort,COPYSTR(vHost,STRPOS(vHost,':')+1,STRLEN(vHost))); //copy port
    vHost:=COPYSTR(vHost,1,STRPOS(vHost,':')-1); //copy only until port
    WinSCPSessionOptions.HostName(vHost);
    WinSCPSessionOptions.PortNumber(vPort);
    WinSCPSessionOptions.UserName(gLogin);
    WinSCPSessionOptions.Password(gPassword);

    //amend remote file
    //full string to amend: 'ftp://XXX.XXX.XX.XX:22/uploads/Inventory.csv'
    vRemoteFile:=COPYSTR(pRemoteFile,STRPOS(pRemoteFile,':')+1,STRLEN(pRemoteFile)); //copy till first :
    vRemoteFile:=COPYSTR(vRemoteFile,STRPOS(vRemoteFile,':')+3,STRLEN(vRemoteFile));

    //run winscp windows client and you will find the following key there
    WinSCPSessionOptions.SshHostKeyFingerprint(sFTPKey);

    WinSCPSesh:=WinSCPSesh.Session;
    WinSCPSesh.Open(WinSCPSessionOptions);


    WinSCPTransferOptions := WinSCPTransferOptions.TransferOptions();
    WinSCPTransferOptions.TransferMode:=0;
    CASE Operation OF
    Operation::Upload: BEGIN
    //premotefile needs to be cut the string
    WinSCPTransferResults:=WinSCPSesh.PutFiles(pLocalFile,vRemoteFile,FALSE,WinSCPTransferOptions);
    // WinSCPTransferResults:=WinSCPSesh.GetFiles(pRemoteFile,pLocalFile,FALSE,WinSCPTransferOptions);
    // WinSCPTransferResults.Check();
    vSuccess:=WinSCPTransferResults.IsSuccess();
    IF vSuccess=FALSE THEN BEGIN
    WriteToErrorLog(pLogFile,FORMAT(WinSCPTransferResults.ToString(),1024));
    gsFTPError:=TRUE;
    END;
    END;
    Operation::List:
    BEGIN
    vSuccess:=WinSCPSesh.FileExists(vRemoteFile);
    // IF vSuccess THEN
    // WinSCPSesh.RemoveFiles(pRemoteFile);
    END;
    Operation::Delete: BEGIN
    IF WinSCPSesh.FileExists(vRemoteFile) THEN BEGIN
    WinSCPSesh.RemoveFiles(vRemoteFile);
    WinSCPTransferResults:=WinSCPSesh.GetFiles(vRemoteFile,TEMPORARYPATH+'tmpfile.tmp',FALSE,WinSCPTransferOptions);
    // WinSCPTransferResults.Check();
    vSuccess:=WinSCPTransferResults.IsSuccess();
    IF vSuccess=FALSE THEN
    vSuccess:=TRUE; //if it cant find means the file is deleted properly
    END
    ELSE
    vSuccess:=TRUE;
    END;
    END;

    END;

    PROCEDURE [email protected]([email protected] : Boolean;[email protected] : Text);
    BEGIN
    gsFTP:=psFTP;
    sFTPKey:=pSftpKey;

    END;



    More details:

    https://community.dynamics.com/nav/b/researchdesigndevelop/archive/2017/10/31/file-upload-sftp

    https://winscp.net/eng/docs/library#vbnet
  • ajayjainajayjain Posts: 101Member
    Thanks very much treff
    I have done in classic client, it works in the same way as used to be
    you can write the dos comments in batch/txt file and execute

    Thanks
    Ajay
    Ajay Jain
    UK
Sign In or Register to comment.