69604

How to retrieve Ethernet adapter name in Windows 8.1 batch script

Question:

I'm looking to pull the Ethernet adapter name out of ipconfig to use in a batch script which will create a static ip to that adapter name using netsh.

Ethernet adapter Ethernet0: Connection-specific DNS Suffix . : foo.bar.com IPv4 Address. . . . . . . . . . . : 10.0.0.123 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 10.0.0.456

What I am trying to do is pull out Ethernet0 and use that in the following netsh command (net_city and net_lab are inputted by the user).

netsh interface ip set address "<adapter name>" static 10.%net_city%.15%net_lab%.235 255.255.255.0 10.%net_city%.15%net_lab%.1 1

What would be the best way to retrieve the name? I have begun looking into regex to try and filter out the name.

Thank you!

Answer1:

As already suggested, you can use netsh to gather a list of interfaces, and then get the user to select one using choice. Here is my implementation of that:

@echo off setLocal enableDelayedExpansion set c=0 set "choices=" echo Interfaces - for /f "skip=2 tokens=3*" %%A in ('netsh interface show interface') do ( set /a c+=1 set int!c!=%%B set choices=!choices!!c! echo [!c!] %%B ) choice /c !choices! /m "Select Interface: " /n set interface=!int%errorlevel%! echo %interface%

The choice command changes the value of errorlevel, and by making the name of each variable comprising your list of interfaces int1, int2, etc. you can simply call them with !int%errorlevel%! after the choice command.

If you can assume there will only ever be one interface, then you can simply do the following.

for /f "skip=2 tokens=3*" %%A in ('netsh interface show interface') do set interface=%%B echo %interface%

Answer2:

for /f "skip=2 tokens=3*" %%A in ('netsh interface show interface') do set interface=%%B netsh interface ip set address name=%interface% static 192.168.1.10 255.255.255.0 192.168.1.1

Answer3:

Why not use the following, then use <a href="https://stackoverflow.com/questions/5435884/creating-a-batch-file-to-identify-the-active-internet-connection" rel="nofollow">this post</a> to select the correct interface.

C:\Scripts>netsh interface show interface Admin State State Type Interface Name ------------------------------------------------------------------------- Enabled Connected Dedicated Local Area Connection

Recommend

  • Programmatically setting up a vpn on Android
  • Why do I get the wrong color of a pixel with following code?
  • Checking for double email addresses which are encrypted in the database
  • How can I process images with OpenCV in parallel using multiprocessing?
  • Textarea in IE8 Newline issue
  • How do I get the RGB Value of a pixel using CGContext?
  • Remove IPEndpoint Bind
  • How to change Docker IP address on Centos 7?
  • MAMP all virtual hosts point to htdocs folder
  • Azure Webjob triggered twice
  • Inputting a character value into an int array? [duplicate]
  • How do I force jquery terminal to remain the same size?
  • How do I set the IP Address of a device using Windows Universal App?
  • Connecting Couchbase sync adapter to couchdb server
  • How to integrate CCAvenue to Ionic app?
  • Replace any string in columns with 1
  • Difficulties implementing the Hysteresis step of Canny Algorithm in Halide without define_extern fun
  • Why do GeoJSON features appear like a negative photo of the features themselves?
  • Unique SMS sender id?
  • Thread synchronization with syncwarp
  • Can I have a variable number of URI parameters or key-value pairs in Laravel 4?
  • Prevent Tomcat from caching request during starup
  • UIAlertController button function not working
  • Filter strings with regex before casting to numeric
  • Center align outputs in ipython notebook
  • Diff between two dataframes in pandas
  • Parse a date string in a specific locale (not timezone!)
  • How to get Eclipse Oxygen to run on Java 9
  • Chrome doesn't support silverlight anymore? How to solve this?
  • Syntax for setting draggablecursor property in google maps api
  • Paperclip, set path outside of rails root folder
  • Jenkins: How To Build multiple projects from a TFS repository?
  • How do I fake an specific browser client when using Java's Net library?
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Spray.io: When (not) to use non-blocking route handling?
  • Modifying destination and filename of gulp-svg-sprite
  • Apache 2.4 - remove | delete | uninstall
  • GridView Sorting works once only
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • How to set the response of a form post action to a iframe source?