Link to home
Start Free TrialLog in
Avatar of huji
hujiFlag for United States of America

asked on

Simple Wscript.Shell question

Hi
I want to use WScript.shell Run command, to run a little EXE file on the server, like nslookup.exe , and then get all the results (what the EXE file returns) and show them in my page.
As I am new to Windows Script Host, I don't know how to get the returned texts.
I testes such code:


Set WshShell = WScript.CreateObject("WScript.Shell")
ReturnCode = WshShell.Run("D:\mx.exe",1,True)
WScript.Echo ReturnCode


where mx.exe is a program that performs MX lookups (Mail Server lookups) but What I get is just "1"!

Please help
Huji
Avatar of huji
huji
Flag of United States of America image

ASKER

Let me add that there is a second problem: I have to send some arguments to my program, but this is reporting an error message:

Set Shell = WScript.CreateObject("WScript.Shell")
ReturnCode = Shell.Run("""D:\mx.exe yahoo.com""",1,True)
WScript.Echo ReturnCode

How shall I send the "yahoo.com" argument to my exe file? I do it by seperating them with only one space, when in command line.
Huji
Avatar of huji

ASKER

I overcome the error message this way:
Set Shell = WScript.CreateObject("WScript.Shell")
ReturnCode = Shell.Run("""D:\mx.exe"" yahoo.com",1,True)
WScript.Echo ReturnCode

But I still get nothing! This time, the program is run correctly, but it reports "0" to me! (I see the command prompt window opening and the program running, but then, nothing!
Huji
Avatar of huji

ASKER

For your information this is the output of the program when run from command prompt:

D:\>mx.exe yahoo.com

Asking [66.218.71.63] for [yahoo.com] ... 4 MX records found.

Priority        Server
--------        ------
[000001]        mx3.mail.yahoo.com
[000001]        mx2.mail.yahoo.com
[000001]        mx1.mail.yahoo.com
[000005]        mx4.mail.yahoo.com
Avatar of plambrecht2
plambrecht2

Not immediatly an answer to your problem but....

If the goal is to check if an email adres is correct, try this function:

Function ValidEmail(byval myEmail)
  ' ## Ftn ValidEmail
  ' ## Written by Pieter Lambrecht - Free to use
  dim regEx, DNSMX, Valid
  Valid=True

  ' check address format
  set regEx = New RegExp
  regEx.IgnoreCase = True
  regEx.Pattern = "^([a-z0-9_]|\-|\.)+@(([a-z0-9_]|\-)+\.)+[a-z]{2,4}$"
  Valid=Valid AND (regEx.Test(myEmail))
  'response.write("regEx.Test(myEmail)=" & regEx.Test(myEmail) & "<br>")

  Set DNSMX = Server.CreateObject("ASPMX.Resolver")
  DNSMX.DNSServer = "10.1.1.1"
  DNSMX.Domain = Right(myEmail,len(myEmail)-instr(myEmail,"@"))
  DNSMX.TimeOutValue = 10
  DNSMX.Resolve
  'response.write("myEmail=" & myEmail & "<br>")
  'response.write("Count=" & DNSMX.MXCount & "<br>")
  'response.write("Domain=" & Right(myEmail,len(myEmail)-instr(myEmail,"@")) & "<br>")
  Valid=Valid AND (DNSMX.MXCount>0)
  'response.write("Valid=" & Valid & "<br>")
  Set DNSMX = Nothing
  ValidEmail = Valid
End Function

It checks if the email adres has a valid format (the regex part) and the it checks if the domain exists...

regards

Pieter
Avatar of huji

ASKER

Good idea Pieter, but the little problem is it will only work when ASPMX component is installed on a machine, which is not my case.
I do not want to install anything on the server. I want to use standard features.
The main idea is to check emails validity. I currently do this using XMLHTTP and a web based free MX Lookup system, but I wonder if I can develop my own one, using the little mx.exe file.
Thanks anyways
Huji

PS: TO OTHERS: please help!
ASKER CERTIFIED SOLUTION
Avatar of wesbird
wesbird

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of huji

ASKER

Excellent idea man! But there remains two problems:
1)I can not use the file idea, since the server tries to write this file every time a form with an email address is submitted, and overlapping of these procedures is INEVITABLE by now.
2)Last and not least: I developed everything in VBS files, and used to check them with WScript host of windows, now when I move things to ASP, I get this error:

Error Type:
Microsoft VBScript runtime (0x800A0046)
Permission denied
/Test/Untitled-1.asp, line 14

Line 14 is as follows:
     ReturnCode = WshShell.Run("cmd /c ping.exe 127.0.0.1 > C:\Inetpub\wwwroot\Test\tmp.txt",1,True)


What permissions should be set? My IUSR user has Full Control over the C:\Inetpub\wwwroot folder.
I tried to figure this out by setting the permissions in IIS properies page for this Test site, but no way.

Waiting for your reply
huji
Probably iusr needs execute permission for c:\windows\system32\ping.exe
Avatar of huji

ASKER

No. I made a copy of ping.exe file in the site root, and gave full control to IUSR user but it failed.
Meanwhile the first problem still remains unsolved, I mean not to use a file.
Thanks anyways,
Huji
As long as you generate temporary filenames you can get away with writing files - you may consider an application variable that increments for each request (dont' forget to delete it afer the response.write). - there's no other practical way to capture stdout unless you can figure out a way to use IWshRuntimeLibrary.WshExec from within ASP.  It is definitely some kind of permissions problem since this script does work on ASP for me.  

The file needn't be in a subfolder of your inetpub.  Just a straightforward temp directory with full permissions for all will do since you're not going to expose the file directly to the end user.
Avatar of huji

ASKER

Ok. Just tell me why this is not working:

<%
Set oShell = server.CreateObject("WScript.Shell")
oShell.Run("C:\Inetpub\wwwroot\Test\NOTEPAD.EXE")
Set oShell = nothing
%>

I have a NOTEPAD.EXE with full permissions, I try to run it, but it's window is not opened!
What should I do now?
Huji
Avatar of huji

ASKER

All right this is working for me:


Dim wshell, intReturn, host, str, str2, i
host = "yahoo.com"
set wshell = server.createobject("wscript.shell")
intReturn = wshell.run("%comspec% /c c:\Inetpub\wwwroot\Test\mx.exe "&host&" > c:\Inetpub\wwwroot\Test\test.txt", 0, True)
Response.Write( intReturn )
set wshell = nothing
Const adTypeBinary = 1
On Error Resume Next
Set objStream = Server.CreateObject("ADODB.Stream")
'Check the object has been successfully created
If Err.Number = 0 Then
      objStream.Open
      objStream.Type = adTypeBinary
      objStream.LoadFromFile "c:\Inetpub\wwwroot\Test\test.txt"
      'Check object could actually load the file
      If Err.Number = 0 Then
            str = objStream.Read
            for i = 1 to lenb(str)
                  str2 = str2 & chr(ascb(midb(str,i,1)))
            next      
            Response.Write Replace(str2,chr(13),"<BR>")
      else
            Response.Write "Err: " & Err.Description
      end if
End If


Thanks
Huji
PS: For later references: The code which called notepad.exe was working indeed! I understood it later when I looked at my tasks list in task manager, and so a list of 20 NOTEPAD.EXE files opened by IWAM user on my computer!


Now I face a bigger problem:
Incidentally - this works too - but I don't like it because of the 100% CPU utilisation in the loop.  Now if we could find a way to sleep using WSH it would be a viable alternative.

set s = Server.CreateObject("Wscript.Shell")
Dim e
dim str
   
Set e = s.Exec("cmd /c ping 127.0.0.1")
While e.Status <> WshFinished
Wend
   
str = e.StdOut.ReadAll
Response.Write Replace(str,chr(13),"<BR>")
Avatar of huji

ASKER

Excuse me but I ran this:

set s = Server.CreateObject("Wscript.Shell")
Dim e
dim str
   
Set e = s.Exec("cmd /c c:\Inetpub\wwwroot\Test\mx.exe www.yahoo.com")
While e.Status <> WshFinished
Wend
   
str = e.StdOut.ReadAll
Response.Write Replace(str,chr(13),"<BR>")

And no CPU utilization at all!
As this solution is excellent, I am going to open a points for question for you, please post a reply there, and I'll give you another 100 points. I'll send a link to it right here.
Avatar of huji

ASKER

Here you go: http:Q_21101268.html
Thanks
Avatar of huji

ASKER

:o)