X command and call system not working correctly

Accepted Solution Solved
Reply
Super Contributor
Posts: 418
Accepted Solution

X command and call system not working correctly

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!


Accepted Solutions
Solution
‎05-27-2015 03:38 PM
Super User
Super User
Posts: 6,844

Re: X command and call system not working correctly

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


All Replies
Super Contributor
Posts: 418

Re: X command and call system not working correctly

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!

PROC Star
Posts: 1,291

Re: X command and call system not working correctly

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.

Super Contributor
Posts: 418

Re: X command and call system not working correctly

Hey. Expert

Super Contributor
Posts: 418

Re: X command and call system not working correctly

Hey Master Master

Super User
Super User
Posts: 6,844

Re: X command and call system not working correctly

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

data _null_;

  infile '.....' pipe ;

  input;

put _infile_;

run;

Super User
Posts: 19,105

Re: X command and call system not working correctly

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;

Super User
Super User
Posts: 6,844

Re: X command and call system not working correctly

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.

Super Contributor
Posts: 418

Re: X command and call system not working correctly

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

Solution
‎05-27-2015 03:38 PM
Super User
Super User
Posts: 6,844

Re: X command and call system not working correctly

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;

Occasional Contributor
Posts: 7

Re: X command and call system not working correctly

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

 

options xwait xsync;

proc options option=(xwait xsync); run;

Super Contributor
Posts: 418

Re: X command and call system not working correctly

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

Super User
Posts: 7,422

Re: X command and call system not working correctly

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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