Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to figure out how to write a script that will let me arbitrarily assign and remove a secondary IP address to a system. I'm most comfortable with batch scripting, so I'd rather focus on solutions for that, if it's possible.

I'm pretty sure I've figured out how to take user input and use it to configure a new IP address and gateway.

The batch script below is written for an interface named "LAN".

SET /P NEW-IPADDR="Enter IP:"
SET /P NEW-MASK="Enter Subnet Mask:"
SET /P NEW-GW="Enter Gateway:"
netsh interface ip add address name=LAN addr=%NEW-IPADDR% mask=%NEW-MASK% gateway=%NEW-GW% gwmetric=0
PAUSE

The PAUSE is inserted to allow an opportunity to check for errors in netsh before the console window automatically exits.

I'm sure the script could use some extra statements for error checking (to work around empty user inputs, or validate that inputs match the pattern of an IP address). But what I'm worried about figuring out right now, is how to clear previously-set secondary IP addresses.

One easy option would be to just set the script to clear all IPs every time, and have a line pre-written that automatically sets up the primary IP before prompting for the secondary. Ideally though, I'd like the script to only be working on secondary IPs.

Option two is to change the last line of the above script to the following:

ECHO "Secondary IP address configured.  Press any key to remove secondary IP."
PAUSE
netsh interface ip delete address name=LAN addr=%NEW-IPADDR% gateway=%NEW-GW%

However, that requires trusting that the script will not be abnormally terminated during the PAUSE. Not exactly something you can really rely upon.

Another option would be to have the script store the secondary IPs that it configures somewhere permanent, so that it can pull the values later and only work with those. It's easy enough to get the values into permanent storage (ECHO "%NEW-IPADDR%" > SecondaryIP.txt && ECHO "%NEW-MASK%" >> SecondaryIP.txt && ECHO "%NEW-GW%" >> SecondaryIP.txt) but now I have two problems:

  1. I don't know how to get the data out of that text file, in a way that's usable by the batch script. (I'm sure this can easily be fixed.)
  2. This relies upon the script being the only thing that touches the secondary IP. A bit more trustworthy than the script exiting normally, but still not quite 100%.

The ideal option would be a script that can recognize a "Primary IP" as opposed to a "Secondary IP" and only work on the latter. Is there a way to do this?

I need a method that's compatible with a standard Windows XP build or later, with little to no usage of third-party tools. Although I'd rather do this in batch, I'm also interested in learning more about PowerShell and it is installed on the systems where I'll be using this. So, PowerShell-based solutions are welcome also.

share|improve this question

1 Answer 1

I can't come up with a complete answer, but this may help put you in the right direction.

You could use netsh from PowerShell to dump the current configuration..

netsh interface ipv4 dump >C:\netsh.txt

This should give you something like this:

# ----------------------------------
# IPv4 Configuration
# ----------------------------------
pushd interface ipv4

reset
set global icmpredirects=enabled
add route prefix=0.0.0.0/0 interface="WAN" nexthop=192.168.1.1 publish=Yes
set subinterface interface= subinterface=ethernet_6 mtu=1492
add address name="WAN" address=192.168.1.200 mask=255.255.255.0
add address name="WAN" address=192.168.1.210 mask=255.255.255.0


popd
# End of IPv4 configuration

You can do 'grep' and 'sed'-like operations on the resulting text file, using the information here. This is the bit that I cannot detail without spending more time on it, because as you can see above, the lines for the addresses have an identical format. Your script will have to grep both the address lines to variables, and you will only sed the second variable.

You can then run the edited configuration script with..

netsh exec C:\netsh.txt

Note that ipconfig will show both new and old configuration data until you reboot, but it should appear correctly in the GUI, I think.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.