BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Anotherdream
Quartz | Level 8

Hello everyone. I have a command line function that I would like to run. This function calls the "putty" program, with a given input.  I am using it to connect to a SFTP server...

The command is as follows.

"C:\Program Files\PuTTY\psftp" -b "C:\SFTP\DirectoryCommand.txt" >"C:\SFTP\OutputofResults.txt" -pw myfakepassword brandon@connect.myPRDServer.biz

if i save this command line to a .bat file, and double click the bat file the correct results are returned to the file 'C:\SFTP\OutputofResults.txt'.

Also if I simply open the command line and paste this string into it, the correct results are returned.

However If I run it from SAS through either of the following commands.

x '"C:\Program Files\PuTTY\psftp" -b "C:\SFTP\DirectoryCommand.txt" >"C:\SFTP\OutputofResults.txt" -pw myfakepassword brandon@connect.myPRDServer.biz'  ;

or

data _null_;

call system ('"C:\Program Files\PuTTY\psftp" -b "C:\SFTP\DirectoryCommand.txt" >"C:\SFTP\OutputofResults.txt" -pw myfakepassword brandon@connect.myPRDServer.biz' );

run;

Then the program does not run.  The Command prompt window very quckly opens, and then immediately closes. From the very click glimpse of the command window, it looks like it is calling PSFTP correctly, but then immediately not recognizing the command -b, etc...

I am not sure where to go from here... Does anyone have any great ideas at this point?

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Yes, put the command you want to run.  You can also try copying the command to the paste buffer and pasting it into a command window and see if you can get the error message that way.

Here is a simple example for pipe.

data _null_;

  infile 'dir' pipe ;

  input ;

  put _infile_;

run;

View solution in original post

12 REPLIES 12
Anotherdream
Quartz | Level 8

Some additional information.  It looks like if I actually have SAS CREATE a batfile with teh exact same command, and then simply call that batfile the program works. Aka the code below is working perfectly.

data _null_;
var1='"C:\Program Files\PuTTY\psftp" -b "C:\SFTP\DirectoryCommand.txt"
>"C:\SFTP\OutputofResults.txt" -pw myfakepassword brandon@connect.myPRDServer.biz';
file 'C:\SFTP\Testbatwrite.bat';
put var1;
run;

x '"C:\SFTP\Testbatwrite.bat"';

So writing a bat file and then calling the bat file works, but simply putting the command into an X command (or call system command) doesn't..... That to me makes no sense so if anyone has any insight I would greatly appreciate it!

Quentin
Super User

I think it's probably a problem with quote marks.  What if you try it without adding the single quotes, ie:

x "C:\Program Files\PuTTY\psftp" -b "C:\SFTP\DirectoryCommand.txt" >"C:\SFTP\OutputofResults.txt" -pw myfakepassword brandon@connect.myPRDServer.biz  ;

After that would try adding more quotes, something crazy like:

x """C:\Program Files\PuTTY\psftp"" -b ""C:\SFTP\DirectoryCommand.txt"" >""C:\SFTP\OutputofResults.txt"" -pw myfakepassword brandon@connect.myPRDServer.biz"  ;

After that I would re-read Art Carpenter's papers about quote marks.

I think if you set options xwait xsynch; it should leave the dos window open so you can at least read any messages you get back.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
Anotherdream
Quartz | Level 8

Hey. Expert

Anotherdream
Quartz | Level 8

Hey Master Master

Tom
Super User Tom
Super User

Call it in a pipe so that you can see the error message.

data _null_;

  infile '.....' pipe ;

  input;

put _infile_;

run;

Reeza
Super User

What if you call system that variable?


data _null_;

var1='"C:\Program Files\PuTTY\psftp" -b "C:\SFTP\DirectoryCommand.txt"

>"C:\SFTP\OutputofResults.txt" -pw myfakepassword brandon@connect.myPRDServer.biz';

file 'C:\SFTP\Testbatwrite.bat';

put var1;

call system(var1);

run;

Tom
Super User Tom
Super User

Type using CALL SYSTEM() in a data step so the the strings are similar to what you are doing to create the BAT file.

Is it possible that PUTTY is smart enough to behave differently when called from the command line (essentially a terminal session) versus from a batch file.

Anotherdream
Quartz | Level 8

Hey tom.. What do i put in the '  '.  Do I put the entire command string into that portion of the infile? Sorry I'm not following your solution (I have never used pipe before).

So I was able to grab a screenshot of the windows command that keeps popping up for 1/10 a second (hurray fast typing).    if you cannot see it, it is just some system options for the Putty program PSFTP.exe...

So it looks like when called thorugh the X command, or Call system, etc.. The program is maybe only recognizing  the first part (just the call to psftp itself?)

C:\Program Files\PuTTY\psftp" -b "C:\SFTP\DirectoryCommand.txt"

>"C:\SFTP\OutputofResults.txt" -pw myfakepassword brandon@connect.myPRDServer.biz'

It doesn't look like any of the other options that were specified went through the system

I'm not sure else it would run the program and note the options, without actually running the options.... (i do note that the scroll bar on the program has a lot of room left... so maybe it tried but we just can't see it because it won't hold open....)

Anyways attached is a copy of that screenshot show sas users.png

Tom
Super User Tom
Super User

Yes, put the command you want to run.  You can also try copying the command to the paste buffer and pasting it into a command window and see if you can get the error message that way.

Here is a simple example for pipe.

data _null_;

  infile 'dir' pipe ;

  input ;

  put _infile_;

run;

barchan
Calcite | Level 5

Just a hint: Are the system options XSYNC and XWAIT turned on?

 

options xwait xsync;

proc options option=(xwait xsync); run;

Anotherdream
Quartz | Level 8

Turns out this is a problem with the Putty program. It doesn't allow one to call it from SAS using a command line call.  I assume this has something to do with calling the program from an outside command line call.

As a work around I am simply going to have SAS create the .BAT file, and then call the bat file from sas. This works and does what I need it too, so i'll be a happy camper.

Thanks for the pipe trick Tom, I wasn't aware of that and that is what allowed me to determine it was a Putty program and not sas

Kurt_Bremser
Super User

I'd move the output redirection to the end of the command for a test.

Better yet to omit the redirection at all and use the pipe method suggested by Tom, you'll find the output in the SAS log.

For good measure, add 2>&1 at the very end of the command to make sure that stderr is also caught.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 12 replies
  • 5009 views
  • 1 like
  • 6 in conversation