Applescript - 从终端命令获取信息


0

我正在创建一个小脚本,以便快速更改我的Mac的网络设置。我已经完成了所有工作,但我希望能够在弹出的第一个对话框中更改它们之前查看设置。

获取此信息的命令是“networksetup -getinfo device

结果是这样的:

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)

如何在我的脚本中将IP地址,子网掩码和路由器作为3个变量,以便通过以下提示将其输出给用户?

Answers:


0

我已经掀起了这个漂亮的代码片段,尝试一下,它应该适合您的特定应用程序。

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)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.