無法在 shell 腳本內正確執行 ssh-ed osascript

無法在 shell 腳本內正確執行 ssh-ed osascript

我在 etc/ppp/ 目錄中建立了一個 ip-down 腳本。本質上,我試圖在 VPN 斷開連接時殺死某些程式/伺服器,然後透過 ssh 在另一台電腦上顯示通知。我已經設定了 ssh 密鑰,並且以下命令在終端中運行良好,但在腳本中運行不正常:

ssh {userName}@{address} 'osascript -e "display notification \"The VPN has disconnected.\" with title \"Server\" sound name \"Pop\""'

腳本中的其他所有內容都有效。我的完整腳本如下:

#!/bin/sh

killall someApp1
killall someApp2
killall someApp3
killall someApp4
ssh {userName}@{address} 'osascript -e "display notification \"The VPN has disconnected.\" with title \"Server\" sound name \"Pop\""'
vpn-connect &

附註:我試圖使用 pf.conf 來阻止 en0(此裝置上的乙太網路)上的所有 torrent 流量,但是當我封鎖時,它不會讓我連接到我的 VPN。我不知道如何允許它。我能夠允許 ssh、https、螢幕共享等。

答案1

不是答案,而是解決方法。

背景:我有一台較舊的 Macbook,我將其用作無頭 Plex 伺服器。我希望它始終保持與 VPN 的連線。我還希望在連接和斷開連接時收到通知。

我最終創建了一個事件處理應用程式。然後我使用 Apple Remote Events 來呼叫它並傳入參數。傳遞參數並且事件處理程序運行後,我告訴應用程式退出。這可以防止它在後台閒置。最後,我透過編輯 plist 從擴充座中隱藏了通知。我創建處理程序應用程式而不是僅使用 Finder 來顯示通知的原因是因為我希望為我的通知提供自訂圖標。

通知助手(事件處理程序)的程式碼:

on run
    idle
end run

on idle argv
    try
        eHandler(item 1 of argv, item 2 of argv, item 3 of argv)
    end try
end idle

on eHandler(message, title, soundName)
    set theMessage to message as string
    set theTitle to title as string
    set theSoundName to the soundName as string
    display notification theMessage with title theTitle sound name theSoundName
end eHandler

ip-down shell 腳本:

#!/bin/sh

# kill applications 
killall someApp1  
killall someApp2
killall someApp3     
killall someApp4

# Open Notification Helper
osascript <<EOF
set remoteMachine to "eppc://{userName}:{password}@{address}"
tell application "Finder" of machine remoteMachine
    open ("/Applications/Notification Helper.app" as POSIX file)
end tell
EOF

# Sends Notification Helper arguments
osascript <<EOF
tell application "Notification Helper" of machine "eppc://{userName}:{password}@{address}"
    TestHandler("The VPN has been disconnected.", "Media Server", "Pop")
    quit
end tell
EOF

# Calls applescript which reconnects my VPN. 
# The & Stops script from waiting to end
vpn-connect &

對於不知道 ip-down 腳本的人來說,它會進入您的 /etc/ppp/ 目錄並在 VPN 斷開連接時運行。您也可以建立一個 ip-up 腳本,該腳本會在您連接到 VPN 時執行。我的 ip-up 只是打開我的所有服務,然後向我發送通知,讓我知道 VPN 已備份。

感謝評論、建議。仍然有興趣了解為什麼這會起作用,因為我有另一個腳本,當 x 透過 ssh 從另一個程式發生時通知我。對pf.conf也還是很有興趣。它的語法讓我很困惑。

相關內容