Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

executing "netsh exec" command from a script

Status
Not open for further replies.

kpierick

ISP
Jul 31, 2002
22
US
Hello All,

I'm trying to execute the following:

strShell.Run ("netsh exec range.netsh")

where range.netsh is the file I have that has all my commands to add exclusion ranges, add IP, etc...to my DHCP server. I can run this file from a command prompt just fine c:\>netsh exec range.netsh
All the commands in the file execute correctly. The problem is when I try to put this command in VBScript it does NOTHING...it runs thru the program...no errors, but does not make any changes to my DHCP server...is there some special syntax I need to be using in VBScript to get this to run????? Does range.netsh need to be a .txt file instead??? I'm all out of ideas to try...any help would be GREATLY APPRECIATED...

Thanks,
Kathi
 
try this:


set oShell = WScript.CreateObject("WScript.shell")
oShell.run "command.com /c netsh exec range.netsh"

cornboy88
[noevil]
 
kpierick,

Is the file range.netsh in the c: root directory. You may have to specify its path.

If you have Windows 2000, you need to use the CMD command.
"/C" allows you to view the execution.

ret = wshShell.exec("cmd /C " & "Dir c:\mydir")

fengshui_1998
 
Ok - thanks for the replies...but it is still not working.

Here is what I have coded:

<%set strSH = createobject(&quot;wscript.shell&quot;)%>
<%strSH.run &quot;cmd /c netsh exec range.netsh&quot;%>

I'm running Windows 2000...the file &quot;range.netsh&quot; is saved on my c: drive.

As I stated in my original post: running netsh exec range.netsh in a command prompt works perferct, but for some reason the above doesn't work in VBScript? Is there something wrong with my syntax???

Thanks,
kpierick
 
Here is a little more background on what I'm trying to do.

This script is contained in a web page (that sits on a machine that is running Win2000, IIS5.0 and DHCP)

We will be having our customers go to this webpage and enter in name, city, phone...once they submit this form, I have some VBScript that grabs their MAC Address, IP Address from a &quot;Private&quot; scope on our DHCP server. Once I have this info, I write a series of commands to a file (range.netsh) - these commands, add IP Address, delete IP Address, add exclusion range, etc...this file is saved on the c: drive of the above mentioned machine.

I can run: netsh exec range.netsh in a command prompt on the above mentioned machine and all works great...but for some reason it won't work on the web page???

Hope this extra info can shed some light on what I'm needing to accomplish...

Thanks,
Kathi
 
kpierick,

When the command executes, what kind of error are you getting or what does it do? Is netsh an EXE file or CMD or what?

Try using the following calling out the location of netsh.

<%

strSH.run &quot;cmd /c &quot;&quot;C:\mydir\netsh.exe&quot;&quot; exec range.netsh&quot;

%>

fengshui_1998
 
In my code, it acts as though it really maybe never even runs the line of code. It never displays an error or anything. This is so &quot;totally&quot; weird...I'm at a loss of what to do...

Netsh is a command-line and scripting utility for Windows 2000 networking components for local or remote computers. The Netsh utility can also save a configuration script in a text file for archival purposes or for configuring other servers.


I found netsh.exe in the following dir: C:\WINNT\system32

so I put the following code in:

<%set strSH = createobject(&quot;wscript.shell&quot;)%>
<%strSH.run &quot;cmd /c &quot;&quot;C:\WINNT\system32\netsh.exe&quot;&quot; exec range.txt&quot;%>

This doesn't generate an error and it never makes any changes to my DHCP server...

Thanks,
kpierick
 
kpierick,

Try this.

strSH.run &quot;cmd /K &quot;&quot;C:\Winnt\System32\netsh.exe&quot;&quot; show helper&quot;

or

strSH.run &quot;cmd /K &quot;&quot;C:\Winnt\System32\netsh.exe&quot;&quot; show mode&quot;


The /K will keep the DOS window and you should be able to see the command execute.


fengshui_1998
 
Thanks for all your replies, but still not working.

Ok here is my code:
I have a function before this that obtains my values for 2 variables below: strIP and strMAC

I then call the following function:

<%Function CreateDHCPEntry()%>

<%set strFSO = createobject(&quot;scripting.filesystemobject&quot;)%>
<!-- will open the following text file -->
<%set strTS = strFSo_Opentextfile(&quot;c:\IPAddress-Start.txt&quot;)%>

<!-- will read the one line that is in the above file - IP Address -->
<%strIP = trim(strTS.readline)%>
<%strTS.close%>
<%set strTS = nothing%>

<%strDescription = Request(&quot;LastName&quot;) & &quot;, &quot; & Request(&quot;FirstName&quot;)%>

<%set strTS = strFSO.createtextfile(&quot;c:\range.txt&quot;,True)%>
<%strTS.WriteLine (&quot;dhcp server&quot;)%>


<!-- need to access the &quot;Public Scope&quot; -->
<%strTS.WriteLine (&quot;#&quot;)%>
<%strTS.WriteLine (&quot;### Public Scope ###&quot;)%>
<%strTS.WriteLine (&quot;scope 10.10.1.0&quot;)%>

<!-- will delete the following exclusion range -->
<%strTS.WriteLine (&quot;delete excluderange &quot; & strIP & &quot; 10.10.1.10&quot;)%>

<!-- will add new reservation -->
<%strTS.WriteLine (&quot;add reservedip &quot; & strIP & &quot; &quot; & strMAC & &quot; &quot; & strDescription & &quot; &quot; & &quot;&quot; & &quot;dhcp&quot;)%>

<!-- need to figure out the last section of the IP address so that 1 can be added to it and written the the text file below. -->
<%intDecimalPos = InStrRev(strIP, &quot;.&quot;)%>
<%intLEN = Len(strIP)%>
<%strIPLeft = Left(strIP, intDecimalPos)%>
<%intCNT = Right(strIP, intLEN - intDecimalPos)%>
<%intCNT = intCNT + 1%>
<%strIP = strIPLeft & intCNT%>

<!-- add a new exclusion range -->
<%strTS.WriteLine (&quot;add excluderange &quot; & strIP & &quot; 10.10.1.10&quot;)%>


<!-- need to delete reservationn out of the &quot;private scope&quot; that is automatically given -->
<%strTS.WriteLine (&quot;### Private Scope ###&quot;)%>
<%strTS.WriteLine (&quot;scope 200.10.60.0&quot;)%>

<!-- will delete &quot;Private Scope&quot; reservation -->
<%strTS.WriteLine (&quot;delete reservedip &quot; & strIPAddress & &quot; &quot; & strMAC)%>


<%strTS.close%>
<%set strTS = nothing%>

<%strFile = &quot;C:\range.txt&quot;%>
<%set strSH = createobject(&quot;wscript.shell&quot;)%>
<!--will run netsh to execute the following file-->
<%strSH.run &quot;cmd /c netsh -f&quot; & strFile,1,True%>
<%If Err.Number <> 0 Then HandleErr%>
<%set strSH = nothing%>

<%End Function%>

It does everything up until: the following line
<%strSH.run &quot;cmd /c netsh -f&quot; & strFile,1,True%>

no matter what I put in this line, nothing runs...I've changed this line 100's of times and nothing ever seems to happen???

I'm completely LOST!!!! HELP PLEASE!!!
 
kpierick,

You need to do two things:

First, try to make CMD run any command to see if you have the syntax correctly.

strSH.run &quot;cmd /c netsh show helper&quot;,1,True

Does thus work? Also, you need a space after &quot;-f&quot;. Try these and hope it works.

strSH.run &quot;cmd /c netsh -f &quot; & strFile,1,True


fengshui_1998
 
I put the following in my code:
strSH.run &quot;cmd /c netsh show helper&quot;,1,True

and NOTHING happened...it didn't start/run my dos prompt.

Do I maybe need to alter some settings for IIS5.0 or something? I haven't changed any settings for IIS5.0 - I just installed it and started coding.

I have downloaded & installed &quot;Windows Scripting Host 5.6&quot; from the microsoft site. Other than installing this, I haven't done anything else with it.

Thanks again...
 
kpierick,

Sorry, try using this one. The &quot;/K&quot; opens a DOS Window, executes the command and stays open for you to view it. Maybe it is executing but you don't see it.

strSH.run &quot;cmd /K netsh show helper&quot;,1,True



fengshui_1998
 
Ok - I did that...

when I put the &quot;cmd /k&quot; in the strSH.run line...it show in the status bar of my I.E6.0 that it is going, but it gets to about 1/2 way and never goes any further...I have an &quot;hour glass&quot; for my cursor and the DOS prompt never &quot;opens up&quot;...I finally just stop my browser window...

????

Thanks
 
kpierick,

Ah, I now see the problem. You are trying to execute a DOS application and display it in a WEB browser which you can't do directly.

Try this to see if the command runs, if so then you can redirect the output

wsh.run &quot;cmd /K netsh show helper > c:\output.txt&quot;,1, True


fengshui_1998
 
Ok - tried this, but it still &quot;hung up&quot; my I.E.
So I used instead the following:
wsh.run &quot;cmd /c netsh show helper > c:\output.txt&quot;,1, True

&quot;cmd /c&quot; instead of &quot;cmd /k&quot;

This created a file called output.txt on my harddrive just like it should...

so I know that I should be able to use &quot;netsh&quot; in my vbscript, but rather than &quot;writing&quot; a file, how would I go about &quot;running&quot; a file with this command?????

It feels like we are &quot;close&quot; to the answer...

Thanks -- I really do appreciate it...



 
kpierick,

Substitute the command with this.


strSH.run &quot;cmd /c netsh -f &quot; & strFile & &quot; > c:\output.txt&quot;, 1, TRUE


fengshui_1998
 
kpierick,

If you just want to run the file and not get any output back from it, use this.

strSH.run &quot;cmd /c netsh -f &quot; & strFile, 1, TRUE

How can you very that NETSH ran with your strFile?

fengshui_1998
 
THANK YOU; THANK YOU; THANK YOU;

I now see the error of my ways...this was executing all along, just I was too dumb to output the results of the execution of a text file....

Now there is one thing that is strange about when this executes. My code I listed previously writes all my commands I want to run to a text file.
If I run: netsh exec range.txt
from a command line prompt it runs successfully doing all the commands.
If I run if from my program: strSH.run &quot;cmd /c netsh -f &quot; & strFile, 1, TRUE
It gets an errors: The command needs a valid Scope IP Address.

If I run each individual command in my program using the netsh command rather than writing all the commands to a text file and then executing that text file I get the same error(The command needs a valid Scope IP Address.
). Here is what I'm running now:
strSH.run &quot;cmd /c netsh dhcp server scope 216.51.183.0 add excluderange &quot; & strIPNew & &quot; &quot; & strEndPublicIP & &quot; > C:\addrange.txt&quot;,1,true

but if I run the above in a command prompt: netsh dhcp server scope 216.51.183.0 add excluderange 10.0.0.1 10.0.0.15 it is successful and puts an exclusion range in my DHCP server.

My IP Address of the machine I'm running this on is 10.0.0.55 I then have a SuperScope called Testing on my DHCP server under this I have 2 scopes: Scope [10.0.0.0] Test and Scope [200.0.0.0] Real

I can access each scope via a command prompt:
netsh dhcp server scope 10.0.0.0 ....
but if I put it in my program I get an error: The command needs a valid Scope IP Address.


Any Ideas on this? Sorry to keep bugging you, but I just can't find much information on Netsh DHCP server commands...so trying to troubleshoot has not been the easiest...

Thanks Again,
kpierick


 
kpierick,

The syntax for running a script file is:

netsh exec filename

I see no syntax using &quot;-f filename&quot;


fengshui_1998
 
kpierick,

Sorry, I take that back, there is a &quot;-f&quot; option. Have you tried running it without the &quot;-f&quot;?

strSH.run &quot;cmd /c netsh exec &quot; & strFile & &quot; > c:\output.txt&quot;, 1, TRUE


fengshui_1998
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top