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!
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;
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!
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.
Hey. Quentin Thanks for the help, but I am 99% sure it's not quote marks. I actually already tried both of your solutions (plus like 8 others that might make sense) and none of them worked =(.
(Fyi your first one fails because you are not wrapping the password into the x call so it fails.
The second one doesn't fail, but acts the same way that the single quote one is.. It's briefly opening the dos window, and then shutting it (open for <1/10 a second).
Also: even with the options xwait and xsync the program is STILL closing the command line pop up window.... Which makes zero sense but that is what is happening. it's extremely frustrating...
Hey tom and Reeza I will look into the calling the variable. Testing it now (i've never done this before actually).
When I try to call system it, it acts the same way as running an X command on it. It opens the dos command window for 1/10th of a second (and it looks like some putty psftp system options are shown) and then immediately closes the dos window... No output file (aka C:\SFTP\OutputofResults.txt) is returned.....
SO again, if I call a batfile with that exact variable command it works, if I call system the variable itself it fails.... I'm quite confused
Call it in a pipe so that you can see the error message.
data _null_;
infile '.....' pipe ;
input;
put _infile_;
run;
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;
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.
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
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;
Just a hint: Are the system options XSYNC and XWAIT turned on?
options xwait xsync;
proc options option=(xwait xsync); run;
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
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.
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!
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.
Ready to level-up your skills? Choose your own adventure.