How do I take out the last digit of an IP address in PowerShell?

Wu Zhengqiang 0 Reputation points
2023-02-04T16:24:47.1433333+00:00

We've been doing IPv4 and IPv6 dual-stack recently.

That is, add an IPv6 address to the original IPv4 address.

Looking at Figure 1, the last bit (network bit) of the IPv4 address and the IPv6 address are the same. Since there are many hosts, we plan to write a batch to achieve this.

What is the way to get the last bit of the IPv4 address?

图1

For example, as shown in Figure 2, this is my network card. How do you take this 130 out?

图2

Our approach is this:

The first three digits of an IPv4 address are fixed, only the last bit is uncertain.

192.168.1.%% Make a loop, start with 129, add 1 each time, all the way to 254, see if that can all match my address.

Is there any better way? Please help me, thank you very much.

——————————————————————————

我们最近在做IPv4、v6双栈。

即在原有的IPv4地址上再添加一个IPv6地址。

看看图1,IPv4和IPv6地址的最后一位(网络位)都是一样的.由于主机较多,我们打算写个批处理来实现这个事情。

请问有什么方法可以取出IPv4地址的最后一位?

举个例子,如图2,这是我的网卡。怎么把这个130取出来呢?

我们的方法是这样:

IPv4地址的前三位是固定的,只有最后一位不确定。

192.168.1.%%做个循环,129开始,每次加1,一直到254,看看那个可以全部匹配到我的地址。

请问还有什么更好的方法吗?

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,577 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Andreas Baumgarten 110.9K Reputation points MVP
    2023-02-04T18:22:12.19+00:00

    Hi @Wu Zhengqiang ,

    to get the last octet value of an IP address you can try this to get started. Hope this helps.

    $ip = "192.168.123.170"
    $ip.Split('.')[-1]
    
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards Andreas Baumgarten


  2. Wu Zhengqiang 0 Reputation points
    2023-02-04T19:25:38.83+00:00

    Wow, that's awesome, you're such an expert!!!

    Thank you very much for your help, I would like to ask you for advice: Is there any way to take out the default gateway for IPv4 addresses?

    We use the default gateway to determine whether this address is a dedicated office address and whether it needs to be modified

    ————————————————

    哇,太棒了,你真是个专家

    非常感谢您的帮助,我想向你请教:是否有任何方法可以将IPv4地址的默认网关取出。

    我们使用默认网关来确定此地址是否为专用办公室地址以及是否需要改造。

    0 comments No comments

  3. Andreas Baumgarten 110.9K Reputation points MVP
    2023-02-04T21:11:48.08+00:00

    Hi @Wu Zhengqiang ,

    with Remove-NetRoute it should be possible to remove the default gateway.

    https://learn.microsoft.com/en-us/powershell/module/nettcpip/remove-netroute?view=windowsserver2022-ps

    Remove-NetRoute -NextHop "<Default Gateway IP address>"
    
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards

    Andreas Baumgarten


  4. MotoX80 34,346 Reputation points
    2023-02-05T13:34:31.8266667+00:00

    The inaccuracy of Google translation makes you misunderstand my meaning.

    Unfortunately, it is not clear what you are trying to do.

    On a running system, you can query the IP configuration like this.

    cls
    Get-NetIPConfiguration | foreach {
        "NIC: {0}" -f $_.InterfaceAlias
        "Default gateway is: {0}" -f $_.IPv4DefaultGateway.NextHop
        foreach ($ip in $_.IPv4Address) {
            "IP address: {0}" -f $ip.ipAddress
            "4th octet : {0}" -f $ip.ipAddress.split('.')[3]
        }
    }
    

    User's image

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.