Parse IP address from here string

This topic contains 6 replies, has 2 voices, and was last updated by Profile photo of ofergenat ofergenat 1 year, 4 months ago.

  • Author
    Posts
  • #29151
    Profile photo of ofergenat
    ofergenat
    Participant

    Hi all,
    I have a script that has a part that needs to retrieve a list of IP addresses from a linux server which runs Netapp DFM.
    when I execute the command and put is in a variable I get a each character in a new line, like a here-string.
    Now, I need to only take the IP addresses out and put them all in an array.
    Any idea how I can do that?
    The variable output looks something like that:

    PS C:\Users\XXX> $x
    A
    B
    C
    D
    E
    1
    9
    2
    .
    1
    6
    8
    .
    1
    6
    .
    1
    0
    A
    B
    C
    D
    E
    1
    9
    2
    .
    1
    6
    8
    .
    1
    2
    .
    1
    2
    A
    S
    D
    F

  • #29152
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Something like this should work:

    $ips = ($x -join '' -replace '[\r\n]') |
           Select-String -Pattern '[\d\.]+' -AllMatches |
           Select-Object -ExpandProperty Matches |
           Select-Object -ExpandProperty Value
    
    
  • #29153
    Profile photo of ofergenat
    ofergenat
    Participant

    Thanks for the very prompt reply, it works great on the test $x example, I will try that also on my script.
    I do have a FOR loop that does the work but I figured there must be a better and simpler way.
    If anyone find that helpful here is my loop (created with a help of a friend)

    $ntp=Invoke-NaSsh -Name 192.168.111.11 -Credential $Cred -Command "dfm host list"
    #$ntp = "dfm host list"
    $final=" "
    $test = $ntp.ToCharArray()
    $x = $ntp.Length
    for ($i = 0; $i -lt $x; $i++)
    {
    if ($test[$i] -eq "1" -and $test[$i+1] -eq "9" -and $test[$i+2] -eq "2" -and $test[$i+3] -eq ".")
    {
    $final += "`n"
    for ($ii = $i; $ii -lt $i+16; $ii++)
    {
    $final += $test[$ii]
    }
    }
    }

    $NetappList = $final.split(“`n”).trim()
    $NetappList=$NetappList[1..($NetappList.Length-1)]

  • #29154
    Profile photo of ofergenat
    ofergenat
    Participant

    Hi Dave,
    I tried that within my script and unfortunately the regex has caught other numbers besides the IPs.
    *one correction is that I get the output of the command like that:

    ID Type Host Name Host Address ProductId Deleted
    —- ———————— ———————————– —————————————– ———— ——–
    6721 Active/Active Controller xxx-xxx-xxx.com 192.168.111.12 2545002333 No
    6738 Active/Active Controller yy-y-yyyy.com 192.168.111.13 24723423444 No
    718 Active/Active Controller zzz-zzzz-zzzzz.com 192.168.111.14 1523223549 No

    And the output from the regex is: (just and example I changed the real numbers)

    192.168.111.12
    2545002333
    9675
    1
    2
    192.168.111.13
    24723423444
    3365
    1
    1.
    .
    192.168.111.14
    1523223549
    3366
    1
    2.

    Any idea how to adjust it now?

  • #29155
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    That's a mess. I would be trying to fix the upstream bit that is giving you each character on a single line, rather than trying to put it back together when you have multiple consecutive numeric fields. Fore example, is that 192.168.111.122545002333 meant to be an IP address that ends in .1, .12, or .122? There's no way to know.

  • #29157
    Profile photo of ofergenat
    ofergenat
    Participant

    I understand the mess.
    But the output of your parsing gives us something that is not that bad.
    I mean the output pattern is always the same, I get the IP I want and some other numeric lines afterwards.
    How can I select only the lines the start with '192.' and drop all the rest?

  • #29158
    Profile photo of ofergenat
    ofergenat
    Participant

    OK I played with it a bit and this has gave me the output I needed.

    $ips = (-split $x) |
    Select-String -Pattern '[\d\.]+' -AllMatches |
    Select-String -Pattern "192."

You must be logged in to reply to this topic.