无法在 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 将通知从 dock 中隐藏起来。我创建处理程序应用程序而不是仅使用 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 非常感兴趣。它的语法对我来说非常混乱。

相关内容