Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Script to switch between Static IP and DHCP

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,2785
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Script to switch between Static IP and DHCP
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Script to switch between Static IP and DHCP - 4/24/2005 7:10:18 AM   
  warhorse

 

Posts: 7
Score: 0
Joined: 4/24/2005
From:
Status: offline
Hey there scripting gurus!

I am working on a script that is an adaptation of something I found in Microsoft's script center. It is really pretty simple. You enter "cscrip", the name/path of the script, and 3 arguments into a command propmt. This will either enable DHCP on all network adapters or set a static IP based on the arguments you send. Example...

cscript setstaticip.vbs true
enables DHCP on all network adapters

cscript setstaticip.vbs false 172.17.0.15 172.17.0.254
sets static ip with 2nd argument, and gateway with 3rd argument
...it also sets subnet mask and dns address, but those are constants in my case (it would be easy to change though)

I have this script called by a VB program to make it easy to use.

The thing works like a charm on active network connections, but I have noticed one thing...

If you do not have the network cable connected properly then it won't make the changes. I, at the very least, need it to be able to enable DHCP on all network adapters, even those with "network cable uplugged."

The errors I get for these adapters are...
Enable DHCP: 94(Path, file, or object not found)
Set Static IP: 81(Unable to configure DHCP service)

Anyway, I could really use some help on this one.

Thanks a ton!

Matt "WarHorse"


Here is the code. Try it for youself. If things start messing up you can fix it by manually putting the settings back.


      
 
 
Post #: 1
 
 Re: Script to switch between Static IP and DHCP - 4/24/2005 7:35:03 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
At what lines do those errors occur (DHCP and static) ?

(in reply to warhorse)
 
 
Post #: 2
 
 Re: Script to switch between Static IP and DHCP - 4/25/2005 2:17:42 AM   
  warhorse

 

Posts: 7
Score: 0
Joined: 4/24/2005
From:
Status: offline
quote:
Originally posted by token

At what lines do those errors occur (DHCP and static) ?





The line that produces error 94 when enabling DHCP is...

      

I changed the line

      
to

      
and now I can set the static ip on the "unplugged" connections.

However, if I decide to use this then I cannot set it back to DHCP with the script. Any ideas?

(in reply to warhorse)
 
 
Post #: 3
 
 Re: Script to switch between Static IP and DHCP - 4/25/2005 12:46:46 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
A couple of things/questions.

1. I don't understand why the following line is necessary. Why can't you just use the existing object from the object collection instead of creating a new one like what you did below ?

Set objNicChanged = objWMIService.Get("Win32_NetworkAdapterConfiguration.Index=" & objNicConfig.Index)

2. You said when you have this "If intStaticReturn = 0 or intStaticReturn = 94 Then", you are able to set teh static ip on the unplugged connection. Are able to verify that the IP address and subnet mask are ACTUALLY configured on the NIC ?

I'm thinking that maybe those 2 settings weren't actually "set". As a result, the NIC is still configured to use DHCP even thought the gateway and DNS are statically configure on the NIC. As a result,

"If Not objNicConfig.DHCPEnabled Then" failed; therefore,

WScript.Echo " DHCP already enabled" & VbCrLf & " DHCP Server: " & objNicConfig.DHCPServer

didn't execute at all.

(in reply to warhorse)
 
 
Post #: 4
 
 Re: Script to switch between Static IP and DHCP - 4/26/2005 2:13:41 AM   
  warhorse

 

Posts: 7
Score: 0
Joined: 4/24/2005
From:
Status: offline
First of all, thank you so much for your help.

Secondly, I mentioned that I got this script from Microsoft and made some changes to get it to work for me.

You are probably right about the line that finds the changed NICs or whatever it is there for.

I didn't make it clear, but I commented out the
       statement. I guess I should post better code in the future.

The SetStaticIP function is messy, but I have a pretty good grasp on how to make it work for me.

I really have to finish this thing and have thought of a way that will allow the script to work without the option of enabling DHCP on the "unplugged" NICs.

However, it would be way better if I could get that to work.

Is it going to be possible to enable DHCP on the "unplugged" NICs?

(in reply to warhorse)
 
 
Post #: 5
 
 Re: Script to switch between Static IP and DHCP - 4/26/2005 3:35:15 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Not from what I've seen; of course, I haven't see much and I could be wrong.

One thing you should be aware of is that as soon as you change the IP from DHCP to static (or vice versa), you will probably lose all network connectivity to the remote host. Keep in mind the idea that you do not want to break a network connection when you remotely administer a machine since that will also "break" your connection to the remote machine. One thing I would suggest is that you can run the script as part of a Startup script so all you have to do is restart the remote machines which can be done using WMI as well.

(in reply to warhorse)
 
 
Post #: 6
 
 Re: Script to switch between Static IP and DHCP - 4/27/2005 2:41:37 AM   
  warhorse

 

Posts: 7
Score: 0
Joined: 4/24/2005
From:
Status: offline
Thanks for you help. I am going to work on a few ideas to work around this problem. I am aware that the scrip will drop the network connection. This script will be on a laptop and the reason they need to run it is to get connected to the network.

I'll let you know what I figure out.

(in reply to warhorse)
 
 
Post #: 7
 
 Re: Script to switch between Static IP and DHCP - 4/27/2005 4:44:19 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Nice, thanks =) Hopefully you can get it working soon ;)

(in reply to warhorse)
 
 
Post #: 8
 
 Re: Script to switch between Static IP and DHCP - 4/27/2005 5:52:10 AM   
  CaffeineAddiction

 

Posts: 144
Score: 0
Joined: 2/9/2005
From:
Status: offline
Hey Warhorse ... There is almost always more than one way to skin a horse

The DHCP and Static IP settings are all just Registry Entries ... in order to find it ... make a static IP other than 127.0.0.1 or 192.168.0.1 ... aka make something unique and then search the registry for it.

I use to use a .reg file back in the day to setup small gaming networks ... this would allow you to setup the Static / Dynamic IP addresses without having to have a network cable plugged in.

While I dont have the scripts that I used back then ... I do have another script that is similar that you may find useful.


      

Note: I changed it a little as to not give away my domain name or IP ... but it should still be functional code

Also note that If you wanted to, you could change the static lines of text to encluded dynamic variables ... to allow for it to work with diffrent IP addresses depending on god only knows what ... username maybe?

But anyway, you can do the same thing for the networksettings of Windows ver 98 through XP ... just got to find the keys

(in reply to warhorse)
 
 
Post #: 9
 
 Re: Script to switch between Static IP and DHCP - 4/27/2005 6:32:12 AM   
  warhorse

 

Posts: 7
Score: 0
Joined: 4/24/2005
From:
Status: offline
Thanks for the suggestion CaffeineAddiction.

Do you have to restart in order to get the settings to take effect?

(in reply to warhorse)
 
 
Post #: 10
 
 Re: Script to switch between Static IP and DHCP - 4/27/2005 7:54:00 AM   
  CaffeineAddiction

 

Posts: 144
Score: 0
Joined: 2/9/2005
From:
Status: offline
I know that you have to restart Internet Explorer in order for that one to take effect ... I am not sure about the Network Settings ... I dont believe you do, but like I said, it has been quite a while since I have used that particular Reg Key

(in reply to warhorse)
 
 
Post #: 11
 
 Re: Script to switch between Static IP and DHCP - 4/27/2005 1:20:20 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Interesting idea, CaffeineAddiction. I guess the good'l registry editing still worth something nowadays ;)

(in reply to warhorse)
 
 
Post #: 12
 
 Re: Script to switch between Static IP and DHCP - 4/29/2005 1:55:44 AM   
  CaffeineAddiction

 

Posts: 144
Score: 0
Joined: 2/9/2005
From:
Status: offline
yah, I find that dynmicly creating a .reg file and then running it from the script that created it is more effective/easier that the built in reg editing stuff ...

(in reply to warhorse)
 
 
Post #: 13
 
 RE: Re: Script to switch between Static IP and DHCP - 5/11/2006 12:29:17 AM   
  rasta

 

Posts: 1
Score: 0
Joined: 5/11/2006
Status: offline
old treat but:

be sure that dhcp-server is connected when switching from static-ip to dhcp

if not connected: 94 !!!

(in reply to CaffeineAddiction)
 
 
Post #: 14
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Script to switch between Static IP and DHCP Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts