Solved

Simple Wscript.Shell question

Posted on 2004-08-19
18
2,998 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:huji
18 Comments
 
LVL 14

Author Comment

by:huji
ID: 11839772
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
0
 
LVL 14

Author Comment

by:huji
ID: 11839798
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
0
 
LVL 14

Author Comment

by:huji
ID: 11839814
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
0
 
LVL 1

Expert Comment

by:plambrecht2
ID: 11840602
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
0
 
LVL 14

Author Comment

by:huji
ID: 11841386
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!
0
 
LVL 7

Accepted Solution

by:
wesbird earned 250 total points
ID: 11841524
Here's an example that does ping.  You'll want to change the fixed filename to a temporary one so that multiple users dont conflict - but this should be enough to get you going.

Function StreamPing

      Set WshShell = Server.CreateObject("Wscript.Shell")
    ReturnCode = WshShell.Run("cmd /c ping.exe 127.0.0.1 > c:\tmp.txt",1,True)
      dim str
      dim str2
      dim i
    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:\tmp.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
End Function
0
 
LVL 14

Author Comment

by:huji
ID: 11844732
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
0
 
LVL 15

Expert Comment

by:joeposter649
ID: 11845194
Probably iusr needs execute permission for c:\windows\system32\ping.exe
0
 
LVL 14

Author Comment

by:huji
ID: 11845994
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
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Expert Comment

by:wesbird
ID: 11850484
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.
0
 
LVL 14

Author Comment

by:huji
ID: 11850495
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
0
 
LVL 14

Author Comment

by:huji
ID: 11850593
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:
0
 
LVL 14

Author Comment

by:huji
ID: 11850675
0
 
LVL 7

Expert Comment

by:wesbird
ID: 11850877
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>")
0
 
LVL 14

Author Comment

by:huji
ID: 11852397
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.
0
 
LVL 14

Author Comment

by:huji
ID: 11852430
Here you go: http:Q_21101268.html
0
 
LVL 7

Expert Comment

by:wesbird
ID: 11873810
Thanks
0
 
LVL 14

Author Comment

by:huji
ID: 11874541
:o)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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