Monday, November 10, 2014

IP address from command line

The 'netsh' command is a very useful command.

Create a batch file with different ip settings. And we can change ip addresses, gateway and DNS with a single click.

Syntax:
Netsh [-a AliasFile] [-c context] [-r RemoteComputer] [{NetshCommand¦-f ScriptFile}]

-a : Returns you to the netsh prompt after running AliasFile.
AliasFile : specifies the name of the text file that contains one or  more netsh commands.
-c : specifies the netsh context.

Following are the available netsh contexts.

AAAA :

DHCP : administers DHCP servers and provides an equivalent alternative to console based management.

Diag :

Interface : configures the TCP/IP protocol

RAS :

Routing :

WINS :

-r : configures  a remote computer
-f : exits netsh after running the script.
ScriptFile : specifies the script that you want to run.

Eg:
netsh interface "local area network" set address static 192.168.0.100 255.255.255.0 192.168.0.1 1

We can use 'add' other than 'set'

You can use like a tool
Run command prompt

NETSH -> will go to netsh prompt
NETSH DUMP : dump all the network information as a script to the screen. You can sent to file using
NETSH DUMP> netshDump.txt
SET ADDRESS NAME="Local Area Connection" source=dhcp
Set the interface "local area connection " to DHCP
SET ADDRESS LOCAL STATIC 10.0.0.8 255.0.0.0 10.0.0.1 1
NETSH INTERFACE IP SHOW CONFIG
Set the interface to static

Eg:

1. Static ip
netsh interface ip set address name="Local Area Connection" static 192.168.1.10 255.255.255.0 192.168.1.1 1

2. Obtain ip automatic
netsh interface ip set address name="Local Area Connection" dhcp

3. Primary DNS
netsh interface ip set dns name="Local Area Connection" static 8.8.8.8

4. Alternative DNS
netsh interface ip add dns name="Local Area Connection" static 4.4.8.8 index=2

5. DNS from dhcp server
netsh interface ip set dnsservers name="Local Area Connection" source=dhcp