【问题标题】:AppleScript - determine the network service to which the current IP address is boundAppleScript - 确定当前 IP 地址绑定的网络服务
【发布时间】:2014-06-29 23:47:14
【问题描述】:

以下 AppleScript 代码将返回网络设置中的所有网络服务。

tell application "System Events"
        tell current location of network preferences
            set names to get name of every service
        end tell
 end tell

如何只获取活动的网络服务名称而不是全部?

澄清更新:在此上下文中的“活动”表示当前[主要] IPv4 地址绑定到其接口的网络服务。

【问题讨论】:

    标签: macos applescript network-service system-preferences systemevent


    【解决方案1】:

    @mcgrailm's answer 向您展示如何获取所有活动的(阅读:未禁用)网络服务,当前是否绑定到地址 .

    相比之下,此答案向您展示了如何确定当前(主)IPv4 地址绑定到其接口的网络服务

    不幸的是,这并不重要,因为信息必须来自多个来源:

    • services 对象(来自字典 System Events.sdef)不包含有关当前绑定地址的信息。
    • 但是,service 对象的 interface 属性包含 MAC address 属性。
    • 虽然IPv4 address of (system info) 返回当前(主)IPv4 地址,但遗憾的是,primary Ethernet address of (system info) 总是返回有线以太网接口的 MAC 地址,它可能是也可能不是当前 IPv4 的接口地址已绑定。
    • 因此,为了确定当前 IPv4 地址对应的 MAC 地址,下面的解决方案使用 do shell scriptawk 的帮助下解析 CLI ifconfig 的输出。
    # Obtain the [network] `service` instance underlying the 
    # current IPv4 address...
    set serviceWithPrimaryIpv4Address to my networkServiceByIp4Address("")
    # ... and extract the name.
    set nameOfServiceWithPrimaryIpv4Address to name of serviceWithPrimaryIpv4Address
    
    # ---- Helper handlers.
    
    # SYNOPSIS
    #   networkServiceByIp4Address([addr])
    # DESCRIPTION
    #   Given (one of) the local system's IPv4 address(es), returns the network service object whose interface
    #   the address is bound to (class `network service` is defined in `Sytem Events.sdef`).
    #   If `addr` is an empty string or a missing value, the current primary IPv4 address is used.
    #  An error is thrown if no matching service is found.
    on networkServiceByIp4Address(addr)
      local macAddress
      set macAddress to my ip4ToMacAddress(addr)
      tell application "System Events"
        try
          tell current location of network preferences
            return first service whose (MAC address of interface of it) is macAddress
          end tell
        end try
      end tell
      error "No network service found matching IP address '" & addr & "'." number 500
    end networkServiceByIp4Address
    
    # SYNOPSIS
    #   ip4ToMacAddress([addr])
    # DESCRIPTION
    #   Given (one of) the local system's IPv4 address(es), returns the corresponding network interface's
    #   MAC address (regardless of interface type).
    #   If `addr` is an empty string or a missing value, the current primary IPv4 address is used.
    #  An error is thrown if no matching MAC address is found.
    on ip4ToMacAddress(addr)
      if addr as string is "" then set addr to IPv4 address of (system info)
      try
        do shell script "ifconfig | awk -v ip4=" & ¬
          quoted form of addr & ¬
          " 'NF==2 && $2 ~ /^[a-f0-9]{2}(:[a-f0-9]{2}){5}$/ {macAddr=$2; next} $1 == \"inet\" && $2 == ip4 {print macAddr; exit}'"
        if result ≠ "" then return result
      end try
      error "No MAC address found matching IP address '" & addr & "'." number 500
    end ip4ToMacAddress
    
    my ip4ToMacAddress("")
    

    【讨论】:

    • good catch @mklement0,我应该知道这就是 OP 的意思。这是一个很好的答案,非常详细,谢谢!
    • @CodenameK:不客气;我很高兴听到它。为了未来读者的利益:您介意更新问题及其标题以阐明“活跃”的含义吗?
    • 您好,您的代码帮助很大。现在它没有检测到来自 4G 棒的连接。你知道需要对代码进行哪些更改才能得到它吗?
    • @CodenameK:我刚刚更新了ip4ToMacAddress() 处理程序以查找任何类型的接口(不仅仅是ether,就像以前一样),希望它也能检测到4G 连接。
    【解决方案2】:

    您只需将其包含在您的查询中:)

    tell application "System Events"
        tell current location of network preferences
            set names to get name of every service whose active is true
        end tell
    end tell
    

    【讨论】:

    • 谢谢。但是,这显示了所有网络服务名称,而不仅仅是活动名称。
    • 它确实给出了“活动”服务的列表,只是没有绑定到 IPV4 大声笑如果你点击一个服务然后点击底部的齿轮你可以将服务设置为非活动,如果它是活动的,反之亦然,我以为你指的是这个
    • 谢谢,这也很有帮助。当我需要将所有网络服务放到一个列表中时,可以使用它。
    猜你喜欢
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    • 2012-01-28
    • 1970-01-01
    • 2021-01-20
    • 2022-11-03
    相关资源
    最近更新 更多