0
votes

I was wondering how to extract only certain data from the output of a shell command in Applescript. I want to be able to only pass the IP address into the variable from a "ping -o" command like this:

   do shell script "ping -o " & blockedURL

    -- Set the IP to blockedIP -- 

    set blockedIP to ..

but I receive this:

"PING example.com (192.0.43.10): 56 data bytes 64 bytes from 192.0.43.10: icmp_seq=0 ttl=239 time=101.587 ms

--- example.com ping statistics --- 1 packets transmitted, 1 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 101.587/101.587/101.587/0.000 ms"

When I execute the ping command I receive a lot of data I dont need. Is there any way of being able to only recall the (192.0.43.10)?

1

1 Answers

4
votes
set a to "PING example.com (192.0.43.10): 56 data bytes 64 bytes from 192.0.43.10: icmp_seq=0 ttl=239 time=101.587 ms

--- example.com ping statistics --- 1 packets transmitted, 1 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 101.587/101.587/101.587/0.000 ms"

set text item delimiters to "("
set temp to text item 2 of a
set text item delimiters to ")"
set temp to first text item of temp
return temp

The above is a full applescript solution. You can also use the following to get the IP just using the shell ping -o www.google.com | cut -d'(' -f2|cut -d')' -f1 | head -n1 so in applescript it would look like this : do shell script "ping -o " & blockedURL & " | cut -d'(' -f2 | cut -d')' -f1 | head -n1"