簡體   English   中英

Mac OS X中的網絡狀態更改通知

[英]Network status change notification in mac os x

在Mac Os X中,當網絡連接狀態更改時,如何通知我的應用程序? 我嘗試使用《 SCNetworkConnection參考》中的SCNetworkConnectionGetStatus。 但是必須不斷調用它。 我需要一個API,它將在網絡狀態發生變化時立即通知我。

這就是我最終使用的。 一旦有了這個腳本,我就將其放入基本的while循環和voila中-網絡連接更改監視。

#!/bin/bash
set -o pipefail

configured_ip_addresses="$((ifconfig | \
  grep -iEo '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | \
  grep -vi '127.0.0.1' | tr '\n' ' ') || echo NONE_CONFIGURED)"
externally_visible_ip_address="$(curl -m 1 ipinfo.io/ip 2>/dev/null || echo NO_CONNECTIVITY)"
computed_state="Actual:  $externally_visible_ip_address, Configured: $configured_ip_addresses"

statefile="/tmp/net-watcher.state"
if [ -f $statefile ]; then
  echo "$computed_state" > "${statefile}-new"
  new_chksum="$(md5 "${statefile}-new" | awk '{print $NF}')"
  existing_chksum="$(md5 "${statefile}" | awk '{print $NF}')"
  if [[ "${new_chksum}" != "${existing_chksum}" ]]; then
    mv "${statefile}-new" "${statefile}"
    osascript -e "display notification \"$(cat $statefile)\" with title \"ALERT: Network Changed\""
  else
    rm "${statefile}-new"
  fi
else
  echo "$computed_state" > $statefile
fi

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM