Solved

Running a .bat file in ASP

Posted on 2002-07-25
20
1,453 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
Comment Utility
Any error codes?
0
 
LVL 22

Expert Comment

by:CJ_S
Comment Utility
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
Comment Utility
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
 
LVL 1

Expert Comment

by:taff22
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Good point - but at the moment I'm logged in as the administrator of the NT server
0
 
LVL 1

Expert Comment

by:taff22
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 12

Expert Comment

by:Wouter Boevink
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 1

Author Comment

by:macdonaldalex
Comment Utility
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
Comment Utility
No luck - but I will award the points to Taff any away

Al
0
 
LVL 1

Author Comment

by:macdonaldalex
Comment Utility
No Luck I'm afraid

Al
0
 
LVL 1

Expert Comment

by:taff22
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now