How to match IP address by using 'findstr'? Or any other method of batch in windows

前端 未结 5 1411
广开言路
广开言路 2021-01-21 07:30

    As the title said, I want to match ip address with batch in windows, please tell me how I can do it?
    I see that \"finds

5条回答
  •  天涯浪人
    2021-01-21 08:11

    tldr;

    This would fall under the "any other method" part of the title but I used a for loop in a batch command together with findstr to get my desired IP. In my case I set the IP to an environment variable called IP.

    Here is the command:

    for /f "tokens=3 delims=: " %i  in ('netsh interface ip show config name^="Ethernet" ^| findstr "IP Address"') do set IP=%i
    

    For those who are curious to know what all that means, read on

    Most commands using ipconfig for example just print out all your IP addresses and I needed a specific one which in my case was for my Ethernet network adapter.

    You can see your list of network adapters by using the netsh interface ipv4 show interfaces command. Most people need Wi-Fi or Ethernet.

    You'll see a table like so in the output to the command prompt:

    Idx     Met         MTU          State                Name
    ---  ----------  ----------  ------------  ---------------------------
      1          75  4294967295  connected     Loopback Pseudo-Interface 1
     15          25        1500  connected     Ethernet
     17        5000        1500  connected     vEthernet (Default Switch)
     32          15        1500  connected     vEthernet (DockerNAT)
    

    In the name column you should find the network adapter you want (i.e. Ethernet, Wi-Fi etc.).

    As mentioned, I was interested in Ethernet in my case.

    To get the IP for that adapter we can use the netsh command:

    netsh interface ip show config name="Ethernet"

    This gives us this output:

    Configuration for interface "Ethernet"
        DHCP enabled:                         Yes
        IP Address:                           169.252.27.59
        Subnet Prefix:                        169.252.0.0/16 (mask 255.255.0.0)
        InterfaceMetric:                      25
        DNS servers configured through DHCP:  None
        Register with which suffix:           Primary only
        WINS servers configured through DHCP: None
    

    (I faked the actual IP number above for security reasons

提交回复
热议问题