Search code examples
sasenterprise-guide

Is there a way to SFTP files from Linux directory to Windows within SAS Enterprise Guide?


I am trying to SFTP files from Linux to a Windows directory using SAS, but I keep running into errors.

My code is:

filename attn '/<Linux Directory>/file.doc';

filename outfile sftp '/<Linux Directory>/file.doc'; cd ='C:\Temp\file.doc'
options="oIdentityFile='~/.ssh/authorized_keys' -oPort=<port number>" 
host="<hostname>" user="<username>"  DEBUG  ;


data _null_;
infile attn ;
file outfile  ;
input;
put _infile_;
run;

But I keep facing the below errors:

ERROR: Physical file does not exist, /<Linux directory>/file.doc.

NOTE: usage: sftp [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config]            [-o ssh_option] [-P sftp_server_path] [-R 
      num_requests]            [-S program] [-s subsystem | sftp_server] host       sftp [user@]host[:file ...]       sftp 
      [user@]host[:dir[/]]       sftp -b batchfile [user@]host

NOTE: cd C:\Temp\task1045v2_13yrs_cc.doc

ERROR: Directory or file C:/temp/file.doc/ doesn't exist.

NOTE: The SAS System stopped processing this step because of errors.

Basically I am trying to recreate the below Visual Basic code in SAS:

open sftp://<userid>:<password>@<hostname> -hostkey="ssh-rsa <Port Number> <key>"
option echo on
option batch on
option confirm off
option transfer ascii
lcd "<Windows path/>" 
cd <Linux path>
get <file.doc> /*the file which need to get transferred from linux to windows*/
close
exit

VB Code (actual code to transfer):

    strPath = "C:\Temp\"
    strFileName = "<filename>.txt"
    strFullName = objFSO.BuildPath(strPath, strFileName)
    strLogName = objFSO.BuildPath(strPath, "<logfilename>.log") 

Set objShell = CreateObject("WScript.Shell")
objShell.CurrentDirectory = strPath

Set objExec = objShell.Exec("""C:\Program Files (x86)\WinSCP\WinSCP.exe"" /console /script=""" & strFullName & """ /log=""" & strLogName & """")

    Set objExec = Nothing
    Set objShell = Nothing

Any advice would be appreciated. Thank you!


Solution

  • Looks like you are trying to move a file named file.doc without changing its name. Make sure to tell SFTP the proper locations on the target machine where you want to write the file.

    %let source=/<Linux Directory>;
    %let target=C:\Temp;
    %let fname=file.doc;
    
    filename attn "&source/&fname";
    
    filename outfile sftp "&fname" cd ="&target\"
      options="oIdentityFile='~/.ssh/authorized_keys' -oPort=<port number>" 
      host="<hostname>" user="<username>"  DEBUG  
    ;
    

    Note: When a statement is so long that you want to break it into multiple lines for readability I find it best to put the semi-colon that ends the statement on a new line. Just like you would put the end; statement on a new line for a block of multiple statements. This will make it easier to spot missing (or as in your case, extra) semi-colons in the code.