Share via


trying to get the IP address out of a PING command using regex

Question

Thursday, January 19, 2012 2:42 PM

The goal is to ping a computer name, and extract the IP address from the result of the PING command. The problem is, I am returning an entire string instead of just the IP address, and I am not sure why. I know I could use substring to get the value, and maybe thats the best way to go, but for now I am curious about using regex.

 

cls
$comp_name = Read-Host "Enter computer name to ping"
$rtn = ping $comp_name

foreach ($line in $rtn)
{
    if ($line.StartsWith("Pinging") -eq $true)
    {
        #$IP = $rtn | where {$_ -match "\[(.*?)\]"}
        $IP = $rtn -match "\[(.*?)\]"
        $IP
    }
}

Thanks

Rob

All replies (6)

Thursday, January 19, 2012 2:58 PM ✅Answered | 2 votes

Hi Rob,

Instead of all that, why not use Test-Connection.  This makes it really easy to extract the IP address:

 

(Test-Connection -comp $computername -Count 1).ipv4address.ipaddressToString

Result:

 

PS C:\scripts> (Test-Connection . -Count 1).ipv4address.ipaddressToString
127.0.0.1

Grant Ward, a.k.a. Bigteddy

What's new in Powershell 3.0 (Technet Wiki)

Network Live Audit - Powershell script


Thursday, January 19, 2012 3:09 PM ✅Answered | 1 vote

The Regex version would be something like this:

 

$comp_name = '127.0.0.1'
$rtn = ping $comp_name

foreach ($line in $rtn)
{
    if ($line.StartsWith("Pinging") -eq $true)
    {
    $line -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' | Out-Null
        $matches.Values
    }
}
 

 

Grant Ward, a.k.a. Bigteddy

What's new in Powershell 3.0 (Technet Wiki)

Network Live Audit - Powershell script


Thursday, January 19, 2012 3:36 PM ✅Answered | 1 vote

alternate solution:

$rtn -match '^pinging.+'|%{$_ -replace '.+\(.+)\.+','$1'}

 or:

($rtn -match '^pinging.+')[0] -replace '.+\(.+)\.+',

'$1'

 

 

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

 

 

 

 

 


Thursday, January 19, 2012 5:20 PM ✅Answered | 1 vote

This function will return the IP address of a computername by querying DNS

function Get-Ip ([string]$name)
{

    trap [System.Management.Automation.MethodInvocationException]{ 
    #write-host ("ERROR: " + $_) -Foregroundcolor Red; 
    Continue}

    $r=[System.Net.Dns]::GetHostAddresses($name) | ? {$_.AddressFamily -eq "InterNetwork" } | select IPAddressToString  #return only IPv4

    if ($r) { return $r.IPAddressToString} else { return "Not Responding" }
}

You code will then be: 

cls
$comp_name = Read-Host "Enter computer name to ping"
Get-Ip $comp_name

Thursday, January 19, 2012 3:14 PM | 1 vote

Hi Robert,

For an IPv4 match, I switched two of your lines around a bit and used this:

$IP = $line -match "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
$matches[0]

The key is the $matches[0], but I went for a slightly stricter IP match at the same time.

Cheers,
Lain


Friday, January 20, 2012 3:32 PM

I marked all as helpful and as answers, although I am going with Bigteddy's solution. I was aware of the test-connection applet, but I didn't know it had the IP as a property. This seems to be the simplest and most straightforward method for this project.

Thanks everyone, good answers and good thread!