1

I'm trying to extract just the IPs from a filtered JSON using the following code:

$curl = Invoke-WebRequest -Method Get -Uri "http://urltojson.com/file.json"
$curl2 = $curl.Content.replace("Script", "Script_").replace("iphone", "iphone_").replace("android", "android_").replace("ipad","ipad_")  | ConvertFrom-Json
$curl3 = $curl2 | Where-Object {$_.Name -eq "/Common/cloudfront_nets"} | Select-Object -ExpandProperty data
$curl3
[System.Collections.ArrayList]$curl3[0] = $curl3
$curl3[1]

Which returns the following

13.59.250.0 255.255.255.192 : region: us-east-2-service: CLOUDFRONT

52.57.254.0 255.255.255.0 : region: eu-central-1-service: CLOUDFRONT

54.182.0.0 255.255.0.0 : region: GLOBAL-service: CLOUDFRONT

52.212.248.0 255.255.255.192 : region: eu-west-1-service: CLOUDFRONT

205.251.250.0 255.255.254.0 : region: GLOBAL-service: CLOUDFRONT

35.162.63.192 255.255.255.192 : region: us-west-2-service: CLOUDFRONT

13.32.0.0 255.254.0.0 : region: GLOBAL-service: CLOUDFRONT

205.251.254.0 255.255.255.0 : region: GLOBAL-service: CLOUDFRONT

I'm struggling to simply output the IPs (without the subnet-mask)

Can anyone help please ?

4

try:

$datas=@"
13.59.250.0 255.255.255.192 : region: us-east-2-service: CLOUDFRONT

52.57.254.0 255.255.255.0 : region: eu-central-1-service: CLOUDFRONT

54.182.0.0 255.255.0.0 : region: GLOBAL-service: CLOUDFRONT

52.212.248.0 255.255.255.192 : region: eu-west-1-service: CLOUDFRONT

205.251.250.0 255.255.254.0 : region: GLOBAL-service: CLOUDFRONT

35.162.63.192 255.255.255.192 : region: us-west-2-service: CLOUDFRONT

13.32.0.0 255.254.0.0 : region: GLOBAL-service: CLOUDFRONT

205.251.254.0 255.255.255.0 : region: GLOBAL-service: CLOUDFRONT
"@


# if you want put the result into an array
$ipv4s = @()


Write-Host ("In loop");
# if file provide from a linux system change `r`n` for `n
([String]$datas).split("`r`n") |% { 

    if($_){

        # adjust index for your choice (here ipv4 addr) at index 0
        [String]$ip     = ([String]$_).split(" ")[0];
        $ipv4s += $ip;

        Write-Host ($ip);
    }

}


Write-Host -ForegroundColor Yellow ("`r`nTotal IPV4s : " + $ipv4s.Count);
Write-Host ("In array");
$ipv4s | % { write-host $_}
# equivalent
#foreach($ipv4 in $ipv4s){
#    write-host $ipv4;
#}
# You can access to object with index (in range of $ipv4s.Count)
# Write-Host ($ipv4s[0]); Write-Host ($ipv4s[1]); etc...

OUT

In loop
13.59.250.0
52.57.254.0
54.182.0.0
52.212.248.0
205.251.250.0
35.162.63.192
13.32.0.0
205.251.254.0

Total IPV4 : 8
In array
13.59.250.0
52.57.254.0
54.182.0.0
52.212.248.0
205.251.250.0
35.162.63.192
13.32.0.0
205.251.254.0
1

Giving @J-barnaby credit for the first and correct answer, the shorter bit (assuming $curl3[1] contains the output data you need formatted) would look like this:

# split the text into array of strings on line breaks and filter empty lines
$output = $curl3[1] -split "`r`n" | ? {$_} 

# process each line
$addresses = foreach ($line in $output)
{
    # further break each line into space delimited words, selecting first word
    $line.Split(' ')[0] 
}

# output the resulting list
$addresses

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.