Sunday, 21 October 2007

using Netsh with DHCP

You can use Netsh commands for Dynamic Host Configuration Protocol (DHCP) in batch files and other scripts to automate tasks. The following example batch file demonstrates how to use Netsh commands for DHCP to perform a variety of related tasks.

In the circumstance of this example procedure, DHCP-01 is a DHCP server with the IP address 192.168.0.2. The procedure adds a new scope to DHCP-01 with the name MyScope, IP address 192.168.10.0, subnet mask 255.255.255.0, and comment NewScope. It then configures the scope with an address range (192.168.10.1 through 192.168.10.254), an exclusion range (192.168.10.1 through 192.168.10.25), and router IP addresses (DHCP option 003). The scope is then set to an active state.

For more information, see Setting up scopes, Setting up options, and DHCP options

For more information and a complete list of Netsh commands for DHCP, see Netsh commands for DHCP

In the following example procedure, lines that contain comments are preceded by "rem," for remark. Netsh ignores comments, i've also highlighted the commands for you.

===========================================================

rem one DHCP server:
rem (DHCP-01) 192.168.0.2

rem 1. Connect to (DHCP-01), and add the scope MyScope with IP address 192.168.10.0,
rem 1.1 subnet mask 255.255.255.0, and the comment NewScope.
netsh dhcp server 192.168.0.2 add scope 192.168.10.0 255.255.255.0 MyScope NewScope

rem 2. Connect to (DHCP-01 MyScope), and add IP address range 192.168.10.1 to 192.168.10.254 for distribution
rem 2.1 and the default ClientType of DHCP.
netsh dhcp server 192.168.0.2 scope 192.168.10.0 add iprange 192.168.10.1 192.168.10.254

rem 3. Connect to (DHCP-01 MyScope), and add IP exclusion range 192.168.10.1 to 192.168.10.25
rem 3.1 and the default ClientType of DHCP.
netsh dhcp server 192.168.0.2 scope 192.168.10.0 add excluderange 192.168.10.1 192.168.10.25

rem 4. Connect to (DHCP-01 MyScope), and set the value of option code 003
rem 4.1 to list two router IP addresses (10.1.1.1, 10.1.1.2).
netsh dhcp server 192.168.0.2 scope 192.168.10.0 set optionvalue 003 IPADDRESS 10.1.1.1 10.1.1.2

rem 5. Connect to (DHCP-01 MyScope), and set the scope state to active.
netsh dhcp server 192.168.0.2 scope 192.168.10.0 set state 1

rem 6. End example batch file.
===========================================================


The following table lists the netsh dhcp commands that are used in this example procedure.

Command Description
server
Shifts the current DHCP command-line context to the server that is specified by either its name or IP address.

add scope
Adds a new scope to the specified DHCP server.

scope
Switches the command context to the DHCP scope that is specified by its IP address.

add iprange
Adds a range of IP addresses to the current scope.

add excluderange
Adds a range of excluded addresses to the current scope.

set optionvalue
Sets an option value for the current scope.

set state
Sets or resets the state of the current scope to either an active or inactive state.

Here is a list of DHCP options i've not had chance to try them all but i'm sure you will fine them useful anyway

1 Subnet Mask.
2 Time Offset (deprecated).
3 Router.
4 Time Server.
5 Name Server.
6 Domain Name Server.
7 Log Server.
8 Quote Server.
9 LPR Server.
10 Impress Server.
11 Resource Location Server.
12 Host Name.
13 Boot File Size.
14 Merit Dump File.
15 Domain Name.
16 Swap Server.
17 Root Path.
18 Extensions Path.
19 IP Forwarding enable/disable.
20 Non-local Source Routing enable/disable.
21 Policy Filter.
22 Maximum Datagram Reassembly Size.
23 Default IP Time-to-live.
24 Path MTU Aging Timeout.
25 Path MTU Plateau Table.
26 Interface MTU.
27 All Subnets are Local.
28 Broadcast Address.
29 Perform Mask Discovery.
30 Mask supplier.
31 Perform router discovery.
32 Router solicitation address.
33 Static routing table.
34 Trailer encapsulation.
35 ARP cache timeout.
36 Ethernet encapsulation.
37 Default TCP TTL
38 TCP keepalive interval.
39 TCP keepalive garbage.
40 Network Information Service domain.
41 Network Information Servers.
42 NTP servers.
43 Vendor specific information.
44 NetBIOS over TCP/IP name server.
45 NetBIOS over TCP/IP Datagram Distribution Server.
46 NetBIOS over TCP/IP Node Type.
47 NetBIOS over TCP/IP Scope.
48 X Window System Font Server.
49 X Window System Display Manager.
50 Requested IP Address.
51 IP address lease time.
52 Option overload.
53 DHCP message type.
54 Server identifier.
55 Parameter request list.
56 Message.
57 Maximum DHCP message size.
58 Renew time value.
59 Rebinding time value.
60 Class-identifier.
61 Client-identifier.
62 NetWare/IP Domain Name.
63 NetWare/IP information.
64 Network Information Service+ Domain.
65 Network Information Service+ Servers.
66 TFTP server name.
67 Bootfile name.
68 Mobile IP Home Agent.
69 Simple Mail Transport Protocol Server.
70 Post Office Protocol Server.
71 Network News Transport Protocol Server.
72 Default World Wide Web Server.
73 Default Finger Server.
74 Default Internet Relay Chat Server.
75 StreetTalk Server.
76 StreetTalk Directory Assistance Server.
77 User Class Information.
78 SLP Directory Agent.
79 SLP Service Scope.
80 Rapid Commit.
81 FQDN, Fully Qualified Domain Name.
82 Relay Agent Information.
83 Internet Storage Name Service.

85 NDS servers.
86 NDS tree name.
87 NDS context.
88 BCMCS Controller Domain Name list.
89 BCMCS Controller IPv4 address list.
90 Authentication.
91 client-last-transaction-time.
92 associated-ip.
93 Client System Architecture Type.
94 Client Network Interface Identifier.
95 LDAP, Lightweight Directory Access Protocol.

97 Client Machine Identifier.
98 Open Group's User Authentication.
99 GEOCONF_CIVIC.
100 IEEE 1003.1 TZ String.
101 Reference to the TZ Database.
102
111
112 NetInfo Parent Server Address.
113 NetInfo Parent Server Tag.
114 URL.

116 Auto-Configure
117 Name Service Search.
118 Subnet Selection.
119 DNS domain search list.
120 SIP Servers DHCP Option.
121 Classless Static Route Option.
122 CCC, CableLabs Client Configuration.
123 GeoConf.
124 Vendor-Identifying Vendor Class.
125 Vendor-Identifying Vendor-Specific.

128 TFPT Server IP address.
129 Call Server IP address.
130 Discrimination string.
131 Remote statistics server IP address.
132 802.1P VLAN ID.
133 802.1Q L2 Priority.
134 Diffserv Code Point.
135 HTTP Proxy for phone-specific applications.
136 OPTION_PANA_AGENT.

No comments: