Solved

Running a .bat file in ASP

Posted on 2002-07-25
20
1,461 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

803 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