加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

AddinganIPAddressfromtheCommandLine

(2018-09-09 22:42:57)
标签:

it

教育

分类: 工作

Multiple IPs assign to one NIC is common web server configuration. however, if it is NAT through firewall when you use GUI to change the setting (e.g. add a new IP to it), it will change the DNS registration order, that would cause an issue for the web site. to avoid it happen, you can use Netsh command to add the new IP to the NIC. 

Windows includes the “netsh” command which allows you to configure just about any aspect of your network connections. If you view the accepted parameters using “netsh /?” you will be presented with a list of commands each which have their own list of commands (and so on). For the purpose of adding IP addresses, we are interested in this string of parameters:

netsh interface ipv4 add address

Note: For Windows Server 2003/XP and earlier, “ipv4” should be replaced with just “ip” in the netsh command.

If you view the help information, you can see the full list of accepted parameters but for the most part what you will be interested in is something like this:

netsh interface ipv4 add address “Local Area Connection” 192.168.1.2 255.255.255.0

The above command adds the IP Address 192.168.1.2 (with Subnet Mask 255.255.255.0) to the connection titled “Local Area Network”.

Adding Multiple IP Addresses at Once

When we accompany a netsh command with the FOR /L loop, we can quickly add multiple IP addresses. The syntax for the FOR /L loop looks like this:

FOR /L %variable IN (start,step,end) DO command

So we could easily add every IP address from an entire subnet using this command:

FOR /L %A IN (0,1,255) DO netsh interface ipv4 add address “Local Area Connection” 192.168.1.%A 255.255.255.0

This command takes about 20 seconds to run, where adding the same number of IP addresses manually would take significantly longer.

A Quick Demonstration

Here is the initial configuration on our network adapter:

ipconfig /all

https://www.howtogeek.com/wp-content/uploads/sg/2010/11/ximage_thumb.png.pagespeed.gp+jp+jw+pj+ws+js+rj+rp+rw+ri+cp+md.ic.xPVMqNOD60.png

Now run netsh from within a FOR /L loop to add IP’s 192.168.1.10-20 to this adapter:

FOR /L %A IN (10,1,20) DO netsh interface ipv4 add address “Local Area Connection” 192.168.1.%A 255.255.255.0

After the above command is run, viewing the IP Configuration of the adapter now shows:

https://www.howtogeek.com/wp-content/uploads/sg/2010/11/ximage_thumb1.png.pagespeed.gp+jp+jw+pj+ws+js+rj+rp+rw+ri+cp+md.ic.5MIEoeJnNB.png

After all, if the issue is still remaining, you can have skipassource switch to skip it register itself to DNS:

Netsh Int IPv4 Add Address SkipAsSource=True

And use the following command to verify it. the issue should be fixed.

netsh int ipv4 show ipaddresses level=verbose

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有