Author: Eugenio Talarico
mailto: eugeniot @ l a ri c o. it
Description:
This script use NETSH to add an IP to interface, using a batch and a txt file
Script:
This Script is composed by 2 files, the batch, to run the script,
the input.txt, that contains all the parameters.
See the BATCH file (add_ip.bat) :
for /f "tokens=1-4 delims=;" %%a in (input.txt) do (netsh interface ip add %%a %%b %%c %%d)
See the Input.txt (this is an example):
"address";"Local Area Cconnection";"10.1.1.1";255.255.255.0"
"address";"Local Area Cconnection";"gateway=10.1.1.3";"gwmetric=0"
"dns";"Local Area Cconnection";"10.1.1.4";"index=1"
"dns";"Local Area Cconnection";"10.1.1.5";"index=2"
"address";"Local Area Cconnection";"10.1.1.2";255.255.255.0"
"address";"Local Area Cconnection";"gateway=10.1.1.6";"gwmetric=0"
Here a description of the use and the script:
USE NETSH to add an IP to interface.
Use the file INPUT.TXT to add address to a network with subnet like the example below.
The first row of the input.txt rappresent the IP address to add and the interface where you want to add the IP,
in case of Windows in english the connection name is "XXXXXXXXXXXXXXXXXXXXXXX" only in case it's not renamed,
otherwise you have to use the name of the connection.(Find it with Control Panel and Network)
"address";"Local Area Connection";"10.1.1.1";255.255.255.0"
ADDRESS tells to Windows that you what to add an IP address
Local Area Connection is the connection name where the IP will be inserted
10.1.1.1 is the IP that will be added
255.255.255.0 is the subnet of the IP
The second row of the INPUT.TXT define a default gateway to add with metric.
"address";"Local Area Connection";"gateway=10.1.1.3";"gwmetric=0"
ADDRESS tells to Windows that you what to add an IP address
Local Area Connection is the connection name where the gateway IP will be inserted
GATEWAY=x.X.X.X is the Ip of the gateway you want to add
gwmetric=0 is the metric 0 is automatic.
It is possible to repeat this two rows and add other IP to the same network connection using the sintax described before.
"address";"Local Area Connection";"10.1.1.1";255.255.255.0"
"address";"Local Area Connection";"gateway=10.1.1.3";"gwmetric=0"
"address";"Local Area Connection";"10.1.1.2";255.255.255.0"
"address";"Local Area Connection";"gateway=10.1.1.6";"gwmetric=0"
Using this 4 row inside the INPUT.TXT the result will be that will added 2 IP on the network connection "Local Area Connection"
the IP will be 10.1.1.1 e 10.1.1.2 with 2 gateway 10.1.1.3 e 10.1.1.6
It's also possible insert DNS automatically using the same bat file but with a different sintax inside the INPUT.TXT as described below
If you insert this string inside the INPUT.TXT
"dns";"Local Area Connection";"10.1.1.4";"index=1"
The result will be that the address 10.1.1.4 will be added as Primary DNS to the "Local Area Connection" Network interface.
DNS tells to Windows that a DNS will be added
Local Area Connection is the name of the connection where the IP will be added
X.X.X.X is the IP that will be added
index=1 is the position of the DNS 1 as Primary
Using two strings like:
"dns";"Local Area Connection";"10.1.1.4";"index=1"
"dns";"Local Area Connection";"10.1.1.5";"index=2"
two addresses, 10.1.1.4 and 10.1.1.5 will be added as primary and secondary dns.
A sequence like this inside the INPUT.TXT
"address";"Local Area Connection";"10.1.1.1";255.255.255.0"
"address";"Local Area Connection";"gateway=10.1.1.3";"gwmetric=0"
"dns";"Local Area Connection";"10.1.1.4";"index=1"
"dns";"Local Area Connection";"10.1.1.5";"index=2"
"address";"Local Area Connection";"10.1.1.2";255.255.255.0"
"address";"Local Area Connection";"gateway=10.1.1.6";"gwmetric=0"
Will produce a result that 2 IP will be added on the interface called "Local Area Connection", 10.1.1.1 and 10.1.1.2 with 255.255.255.0 as a subnet mask
and 2 gateway 10.1.1.3 and 10.1.1.6 and 2 DNS primary and secondary 10.1.1.4. and 10.1.1.5
IF you want to add more IP address you only have to add inside the INPUT.TXT after other address rows like
"address";"Local Area Connection";"XXX.XXX.XXX.XXX";XXX.XXX.XXX.XXX"
"address";"Local Area Connection";"gateway=XXX.XXX.XXX.XXX";"gwmetric=0"
Two rows for each IP address you want to add also if you need to add a gateway, otherwise you can add only address.
>> Stay informed about: Use Netsh To Add An Ip To Interface..