FOR command assistance: Setting variable from command output
Posted: 17 May 2010 15:33
I've got a tricky one (for me). I'm trying to create a batch file which will reset all the network interfaces on the computer to DHCP. Setting a single interface to DHCP isn't an issue (see http://samanathon.com/reset-your-ip-add ... atch-file/ for exact commands), but I want to set all the interfaces to DHCP (some viruses set bad IP addresses or DNS servers).
Here are the commands to run to change the interface to DHCP:
netsh int ip set address name = "Local Area Connection" source = dhcp
netsh int ip set dns name = "Local Area Connection" source = dhcp
netsh int ip set wins name = "Local Area Connection" source = dhcp
Here is the command used to list the interfaces on the machine:
netsh int ip show interface
And here is the output of that command:
What I need to do is loop through the output of that command, gather every "User-friendly Name" (excluding loopback, setting that to DHCP may cause some problems), then run the first commands to reset that interface to DHCP.
I've created a mockup of what it may look like, but I know it's not going to work. Perhaps someone knows how to do this, r has some code lying around?
(note to self: also need to export the registry settings for the routing and remote access service [remoteaccess], enable and start the service, wait 15 seconds, run above commands, then restore settings for that service and stop it)
Here are the commands to run to change the interface to DHCP:
netsh int ip set address name = "Local Area Connection" source = dhcp
netsh int ip set dns name = "Local Area Connection" source = dhcp
netsh int ip set wins name = "Local Area Connection" source = dhcp
Here is the command used to list the interfaces on the machine:
netsh int ip show interface
And here is the output of that command:
Code: Select all
MIB-II Interface Information
------------------------------------------------------
Index: 1
User-friendly Name: Loopback
GUID Name: Loopback
Type: Loopback
MTU: 32768
Speed: 10000000
Physical Address:
Admin Status: Up
Operational Status: Operational
Last Change: 0
In Octets: 0
In Unicast Packets: 0
In Non-unicast Packets: 0
In Packets Discarded: 0
In Erroneous Packets: 0
In Unknown Protocol Packets: 0
Out Octets: 0
Out Unicast Packets: 0
Out Non-unicast Packets: 0
Out Packets Discarded: 0
Out Erroneous Packets: 0
Output Queue Length: 0
Description: Internal loopback interface for 127.0.0 netw
ork
Index: 2
User-friendly Name: Local Area Connection 3
GUID Name: {CB5FD14D-B673-488E-87D2-B9DEB098582B}
Type: Ethernet
MTU: 1500
Speed: 100000000
Physical Address: 00-22-68-72-2E-14
Admin Status: Up
Operational Status: Operational
Last Change: 3307940815
In Octets: 2307725244
In Unicast Packets: 27991495
In Non-unicast Packets: 229894
In Packets Discarded: 0
In Erroneous Packets: 0
In Unknown Protocol Packets: 1483213
Out Octets: 2112210243
Out Unicast Packets: 27929817
Out Non-unicast Packets: 15069
Out Packets Discarded: 0
Out Erroneous Packets: 0
Output Queue Length: 0
Description: NVIDIA nForce Networking Controller - Packet
Scheduler Miniport
What I need to do is loop through the output of that command, gather every "User-friendly Name" (excluding loopback, setting that to DHCP may cause some problems), then run the first commands to reset that interface to DHCP.
I've created a mockup of what it may look like, but I know it's not going to work. Perhaps someone knows how to do this, r has some code lying around?
Code: Select all
FOR /F "tokens=2* delims= " %%A IN (netsh int ip show interface) DO (
netsh int ip set address name = "%%B" source = dhcp
netsh int ip set dns name = "%%B" source = dhcp
netsh int ip set wins name = "%%B" source = dhcp
)
(note to self: also need to export the registry settings for the routing and remote access service [remoteaccess], enable and start the service, wait 15 seconds, run above commands, then restore settings for that service and stop it)