Click to See Complete Forum and Search --> : Executing shell from server ASP.Net


dia
04-17-2007, 07:33 AM
I am trying to access SAP R/3 for the company I am co-oping for. They won't give me direct access so I have to use a executable called startrfc.exe to run queries( remote functions ) against SAP.

Problem is, I think I need to use cmd.exe to be able to execute within the same folder that startrfc is located. I am unable to specify a working directory on cmd because it does not allow for virtual paths which is all I have to work with right now. When you run startrfc by itself, all it does is flick on the screen showing parameters of the call and then closes.

I've tried just executing a simple .bat from the server's end to echo my name and then capture the output and I still get nothing.


ProcessStartInfo proc = new ProcessStartInfo("cmd.exe"); // tried startrfc, rootPath + "startrfc.exe"... anything I can think of.

proc.UseShellExecute = false;
proc.RedirectStandardError = true;
proc.RedirectStandardInput = true;
proc.RedirectStandardOutput = true;

//proc.WorkingDirectory = rootPath + "\\";
string msg = *the arguments to pass to sap*
Process rfc = Process.Start(proc);
StreamReader stuff = rfc.StandardOutput;
StreamWriter sw = rfc.StandardInput;

sw.WriteLine(msg);

rfc.Close();
sw.Close();


I'm at a loss. Does anyone have any knowledge of SAP or running a shell from the server's end?

TIA!

PeOfEo
04-17-2007, 10:25 AM
Yeah, you can execute a shell, but I was just going to say it would be just as easy and a little safer to use a batch, but I see you've tried that. The trouble with using cmd is then you have to make sure that the ASPNET system account has access to cmd.exe and that is a dangerous proposition. I did a little googling, and this should be helpful:
http://www.willasrari.com/blog/run-a-command-shell-executable-from-aspnet/000155.aspx
But, Instead of executing a shell to execute this console application, why not just execute this application directly (just pass in the appropriate parameters when you call it)?

I must say, it is actually more straight forward to do this kind of thing in ASP classic, which is kind of surprising.

I have never worked with SAP R/3 myself, but I believe some of the other members on this forum have experience with that.

dia
04-17-2007, 02:25 PM
I've tried running startrfc.exe with arguments after it. It then says it can't find the file. I've tried running startrfc.exe and then settings arguments.

But since I have been writing and rewriting this reply for the past hour, I have figured out something. I can execute the startrfc from this PC and get output like I need. It seems to probably be more of a permissions thing than anything.

Anyone else has input please let me know! :)

PeOfEo
04-17-2007, 05:14 PM
Well no matter what, the ASPNET machine account will need permissions to execute anything it is executing. By default it isn't going to have those permissions. However, I would imagine that it should throw an error saying it doesn't have permissions to access that resource rather than throw an error because it can't find the file if that is the case.

dia
04-18-2007, 07:50 AM
I was able to execute cmd.exe, specify the working directory and do "dir" to get the directory listing. It showed me the files that I have stored on the server. I also found that it doesn't matter if the user has access to the drive that SAP will write to ( duh ) since I tried it on my sup's PC who doesn't have access to the specific drive and he was at least able to execute startrfc.exe. Just got wait now for IT to check on it.

@ PeOfEo:
If the permissions are there by default but yet I can run a cmd.exe, which I would have thought would be restricted too, would permissions have to be set for any executable in IIS ( startrfc.exe )? I'm not familiar with IIS at all. I'm more used to apache/mysql type things.

PeOfEo
04-18-2007, 10:08 AM
I posted this link in another thread
http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/3648346f-e4f5-474b-86c7-5a86e85fa1ff.mspx?mfr=true
The only worker account that should get execute permission to the executables is the ASPNET account. By default none of these worker accounts should have permission to get at cmd.exe, let alone any other system files, and it certainly wouldn't have permission to get at something you put there. So if you were able to make a call to cmd.exe without modifying the permissions I am a bit shocked and surprised.

The user on the website uses an IUSR account, but the user isn't doing the executing, it is in fact the ASPNET worker account. You can however, if you wanted to, play with all of this and use windows auth and have ASPNET use another account through identity impersonation, we do that a ton here, but because we have scripts that need to be able to get at active directory, or communicate with remote sql servers or our mainframes, so on and so fourth.

dia
04-18-2007, 10:23 AM
Thanks a lot for the replies and information.

One more question, hopefully simple. When the ASP page performs Response.Write(whatever), does the execution on the server side continue? I'm just wanting to pass back a JSON file but still do a small bit of processing on the server.

PeOfEo
04-18-2007, 10:28 AM
Yeah, it's just going to print your variable in the client side output wherever that response.write is. It isn't going to stop the execution of the rest of your code if that's what you mean.