Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Running a .bat file in ASP

Posted on 2002-07-25
20
1,463 Views
Last Modified: 2012-05-04
Having problems running a .bat file from an ASP page - Does anybody have some working code I could see

I'm trying to use the WScript.Shell object at the moment - but without any success

I can't even get the script at the end of this link to run

http://www.4guysfromrolla.com/webtech/102998-1.shtml 


Al
0
Comment
Question by:macdonaldalex
  • 9
  • 8
  • 2
  • +1
20 Comments
 
LVL 12

Expert Comment

by:Wouter Boevink
ID: 7176961
Any error codes?
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 7177009
What is in the .bat file? if you are trying to run a program on the server you have succeeded, but it just does not show the program visible. Correct, since it is ran under the IUSR_ account in the IIS memory.

CJ
0
 
LVL 1

Author Comment

by:macdonaldalex
ID: 7177047
Error Type:
(0x80070483)
/desktop/rudi.asp, line 13

rudi.asp is my asp file...

line 13 is RetCode = WShShell.Run("C:\Documents and Settings\smansoori\Desktop\DoPing.bat",  1, True)

DoPing.bat file contents is
 
ping -a 199.173.152.24 > "C:\Documents and Settings\smansoori\Desktop\ping.txt"

The Ping.txt file is never created.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:taff22
ID: 7177053
I have just tried this myself and got it to work.

Is your batch file stored on the server in a folder with the correct permissions set?

Make sure that your folder on the server has the correct permissions in order to execute the batch file and create/delete temporary files (for this example).

FYI, I run exactly what I found on you link, though I changed the file path and IP addresses to make it work?
0
 
LVL 1

Author Comment

by:macdonaldalex
ID: 7177295
Looks like the inital problem was not permissions, but rather the directory - C:\Documents and Settings\smansoori\Desktop\ping.txt
which has spaces in !! Once we changed  directory it worked


- If we can go back to the original problem not -

ASP page reads as follows -
"
<%

Set FileSys = Server.CreateObject("Scripting.FileSystemObject")
FileName = FileSys.GetTempName

Set WShShell = Server.CreateObject("WScript.Shell")

IP = " 199.173.152.24" ' or whatever you want to ping

RetCode = WShShell.Run("c:\test\DoPing.bat", 1, True)

if RetCode = 0 Then

'There were no errors

else

Response.Redirect "PingErrors.htm"

end if


'Set TextFile = FileSys.OpenTextFile("c:\test\ping.txt", 1)
'TextBuffer = TextFile.ReadAll

'For i = 1 to Len(TextBuffer)

'If Mid(TextBuffer,i,1) = chr(13) Then

'Response.Write("<BR>")

'else

'Response.Write(Mid(TextBuffer,i,1))

'end if

'Next

'TextFile.Close

'FileSys.DeleteFile "c:\test\ping.txt"

%>

"

and by .bat file -
"
kill notepad.exe
"

The .bat file works when run directly, but via the asp page it doesn't - no asp errors - just nothing

Hope you guys can help

Al
0
 
LVL 1

Expert Comment

by:taff22
ID: 7177361
I have taken your code and modidied it slightly, ensuring Option Explicit and objects are set to nothing when finished.  I have also stripped out the stuff I didn't use.

The "ping.bat" file exists in a dir with read & exec permissions on my web server.

The listing that is created lists the c: of my web server into a file on its own c drive.

I have used a simple example of a bat file to ensure that there were no problems with it.



ASP:
<%
Option Explicit
Dim FileSys, FileName, WShShell, RetCode

Set FileSys = Server.CreateObject("Scripting.FileSystemObject")
     FileName = FileSys.GetTempName
     Set WShShell = Server.CreateObject("WScript.Shell")
          RetCode = WShShell.Run("c:\websites\taff\ping.bat", 1, True)
          'retcode key: 0 = no error, 1 = error encountered
          Response.Write RetCode
     Set WShShell = Nothing
Set FileSys = Nothing
%>


BAT:

dir c:\ > "c:\test.txt"
0
 
LVL 1

Author Comment

by:macdonaldalex
ID: 7177386
Thanks Taff22,

I have just changed my .bat file to match the your dir command and that works fine -


Unfortunatly the thing is I need to get the .bat program to kill a process on the server (notepad was just an example)
0
 
LVL 1

Expert Comment

by:taff22
ID: 7177413
I must confess that I know very little about the "kill" command, my W2k box doesn't recognise it.

If it is used to kill processes, I assume that you have to have admin permissions, which your internet guest account is unlikely to have...
0
 
LVL 1

Author Comment

by:macdonaldalex
ID: 7177429
Good point - but at the moment I'm logged in as the administrator of the NT server
0
 
LVL 1

Expert Comment

by:taff22
ID: 7177437
I *think* that if you are you using anonymous access to the site, the process will run as IUSR_MachineName, regardless of who you are logged on as...
0
 
LVL 12

Expert Comment

by:Wouter Boevink
ID: 7177568
taff22

You're right. But if someone is anonymous he shouldn't get the option the run such scripts.

Als, I guess, the wscript object will not run under the anonymous user context. (I hope)
0
 
LVL 1

Expert Comment

by:taff22
ID: 7177577
I have run it as a guest user, but did have to explicitly give him permission to exec scripts in that folder...
0
 
LVL 1

Author Comment

by:macdonaldalex
ID: 7177595
Did you run it with a kill command ?? - As dir commands run fine with the anonymous user

Maybe we should put a terminate process command in a vbs script ? What do u think ?
0
 
LVL 1

Expert Comment

by:taff22
ID: 7177601
No, sorry, was a lil' confused, I thought you meant run *any* scripts... I missed where you put "such" ;-)
0
 
LVL 1

Author Comment

by:macdonaldalex
ID: 7177613
Have piped the result of running

kill notepad.exe

into a txt file and get back

"adjusttokenprivileges failed with 1300"

Any Ideas ???

Cheers

Al
0
 
LVL 1

Accepted Solution

by:
taff22 earned 100 total points
ID: 7177625
0
 
LVL 1

Author Comment

by:macdonaldalex
ID: 7177656
Have found that - Thanks

Also have found local security policy program on Win 2000 server - try to alter diff privileges now
0
 
LVL 1

Author Comment

by:macdonaldalex
ID: 7179582
No luck - but I will award the points to Taff any away

Al
0
 
LVL 1

Author Comment

by:macdonaldalex
ID: 7179583
No Luck I'm afraid

Al
0
 
LVL 1

Expert Comment

by:taff22
ID: 7179585
Thanks Al,

Good luck with that, now I've had a look at that I might be using it myself at some point, so keep your eyes peeled for any problems/solutions I come across!!!

Taff
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question