Applescript – Get information from terminal command

applescriptNetworkterminal

I'm creating a little script to allow quick changes to the network settings of my Mac. I have got it all working, but I wanted to be able to see what the settings are before I change them in the first dialog pop up.

The command that gets this information is "networksetup -getinfo device"

The result is like this:

Manual Configuration --This may also say DHCP Configuration--
IP address: 192.168.0.100
Subnet mask: 255.255.255.0
Router: 192.168.0.1
IPv6: Automatic
IPv6 IP address: none
IPv6 Router: none
Ethernet Address: (null)

How can I get the IP address, Subnet mask and Router as 3 variables in my script so I can output them to the user over the following prompts?

Best Answer

I've whipped up this nice code snippet, try it out, it should work well for your specific application.

set device to "Ethernet"

set getInfo to paragraphs of (do shell script "networksetup -getinfo " & device)
set ipAddress to ((item 2 of getInfo) as string)
set subnetMask to ((item 3 of getInfo) as string)
set router to ((item 4 of getInfo) as string)
-- You could use the output here, and remove the rest of the script, or continue to remove the excess wordage.

set ipAddress to ((characters 13 through -1 of ipAddress) as string)
set subnetMask to ((characters 14 through -1 of subnetMask) as string)
set router to ((characters 9 through -1 of router) as string)