检查 ssh 是否在另一个管理员用户上 运行 并杀死它。 (苹果脚本)
Check if ssh is being run on another administrator user and kill it. (AppleScript)
我想检查进程 'sshd' 是否在用户 'ladmin' 上 运行ning,如果是,则从我的用户 'patrick' 处将其终止。我也是管理员。
这是我的代码:
tell application "System Events"
set ProcessList to name of every process
if "sshd" is in ProcessList then
set ThePID to unix id of process "sshd"
do shell script "kill -KILL " & ThePID with administrator privileges
end if
end tell
我的问题是 ProcessList
只包含我用户的进程。此外,它也只包含某些进程,包括我所有的应用程序和系统事件和 Dock。即使进程 sshd 在我的用户上,它也不会显示。
另外,有什么方法可以在 startup/login 将其设置为 运行?
这是我自己的回答:
而不是使用 set ProcessList to name of every process
。
改用 shell 脚本作为 root。
set kill_pid to do shell script "ps ax | grep sshd | grep -v grep | awk '{ print }'" password "<PASSWORD>" with administrator privileges
但这会引发一个问题,因为当有人使用 SSH 远程访问您的计算机时,三个不同的用户 运行 进程 sshd。
三个用户是:
根
_sshd
此外,用户 _sshd 会在大约十到二十秒后消失。
因此,这意味着前几秒 kill_pid
包含三、五位数字,但之后 kill_pid
仅包含两个五位数字。此外,这些数字由 space.
分隔
这个问题的修复相当简单:
set kill_pid to do shell script "echo '" & kill_pid & "'| cut -c 7-"
if length of kill_pid > 5 then
set kill_pid to do shell script "echo '" & kill_pid & "'| cut -c 7-"
end if
总的来说这个问题还是比较多的。如果我希望脚本继续检查 sshd 而不是一次,我必须将整个程序放入循环中。虽然,当进程 sshd 不是 运行ning 时,这:
set kill_pid to do shell script "ps ax | grep sshd | grep -v grep | awk '{ print }'" password "<password>" with administrator privileges
returns 一个错误。
这也是一个非常简单的修复,您只需使用 try
。
在上下文中,它看起来像这样:
repeat
try
set kill_pid to do shell script "ps ax | grep sshd | grep -v grep | awk '{ print }'" password "<password>" with administrator privileges
set kill_pid to do shell script "echo '" & kill_pid & "'| cut -c 7-"
if length of kill_pid > 5 then
set kill_pid to do shell script "echo '" & kill_pid & "'| cut -c 7-"
end if
end try
end repeat
最后一个问题是,重复此操作会导致脚本占用我 CPU 的 400% 中的 127%。
同样,一个非常简单的修复:
delay 1
这是我的全部代码:
repeat
try
set kill_pid to do shell script "ps ax | grep sshd | grep -v grep | awk '{ print }'" password "<PASSWORD>" with administrator privileges
set kill_pid to do shell script "echo '" & kill_pid & "'| cut -c 7-"
if length of kill_pid > 5 then
set kill_pid to do shell script "echo '" & kill_pid & "'| cut -c 7-"
end if
if kill_pid is equal to "" then
end if
if kill_pid is not equal to "" and length of kill_pid is equal to 5 then
tell application "System Events"
set question to display dialog "Do you want SSH to be running right now?" buttons {"No", "Yes"} default button 2
set answer to button returned of question
if answer is equal to "No" then
do shell script "kill -KILL " & kill_pid password "<PASSWORD>" with administrator privileges
end if
if answer is equal to "Yes" then
set wait to display dialog "Press OK when you are done running SSH. Or click Stop to stop checking for SSH." buttons {"Stop", "OK"} default button 2
set ok to button returned of wait
if ok is equal to "OK" then
end if
if ok is equal to "Stop" then
exit repeat
end if
end if
end tell
end if
end try
delay 1
end repeat
现在,如果您希望此 运行 作为应用程序并 运行 在登录时执行以下操作:
将脚本复制并粘贴到脚本编辑器中,然后转到“文件”>“导出”并将其另存为应用程序。
然后在登录时将其设置为 运行,您必须转到系统偏好设置>用户和组>登录项并将其设置为 运行 在登录时。
最后,如果您想要自定义应用程序图标,只需将图像文件的一部分复制并粘贴到“获取信息”选项卡中的图标中即可。
复制 (Command+c) 一张图片。
右键单击该应用程序。
单击小图标(它应该以蓝色突出显示)并粘贴 (Command+v) 复制的图片。
现在您有一个应用程序可以检查计算机上是否存在任何 SSH 运行!
编辑
我添加了一项功能,以便您可以查看通过 SSH 连接到您的计算机的 IP。
这是新代码:
repeat
try
set kill_pid to do shell script "ps ax | grep sshd | grep -v grep | awk '{ print }'" password "PASSWORD" with administrator privileges
set kill_pid to do shell script "echo '" & kill_pid & "'| cut -c 7-"
if length of kill_pid > 5 then
set kill_pid to do shell script "echo '" & kill_pid & "'| cut -c 7-"
end if
if kill_pid is equal to "" then
end if
if kill_pid is not equal to "" and length of kill_pid is equal to 5 then
tell application "System Events"
set userset to do shell script ("w")
set question to display dialog "Do you want SSH to be running right now?" buttons {"Show Users", "No", "Yes"} default button 3
set answer to button returned of question
if answer is equal to "Show Users" then
set userset to do shell script "echo '" & userset & "'| cut -c 56-"
set question2 to display dialog "Current users:
" & userset buttons {"Stop SSH", "Run SSH"} default button 2
set answer2 to button returned of question2
if answer2 is equal to "Stop SSH" then
set answer to "No"
end if
if answer2 is equal to "Run SSH" then
set answer to "Yes"
end if
end if
if answer is equal to "No" then
do shell script "kill -KILL " & kill_pid password "PASSWORD" with administrator privileges
end if
if answer is equal to "Yes" then
set wait to display dialog "Press OK when you are done running SSH. Or click Stop to stop checking for SSH." buttons {"Stop", "OK"} default button 2
set ok to button returned of wait
if ok is equal to "OK" then
end if
if ok is equal to "Stop" then
exit repeat
end if
end if
end tell
end if
end try
delay 1
end repeat
我想检查进程 'sshd' 是否在用户 'ladmin' 上 运行ning,如果是,则从我的用户 'patrick' 处将其终止。我也是管理员。
这是我的代码:
tell application "System Events"
set ProcessList to name of every process
if "sshd" is in ProcessList then
set ThePID to unix id of process "sshd"
do shell script "kill -KILL " & ThePID with administrator privileges
end if
end tell
我的问题是 ProcessList
只包含我用户的进程。此外,它也只包含某些进程,包括我所有的应用程序和系统事件和 Dock。即使进程 sshd 在我的用户上,它也不会显示。
另外,有什么方法可以在 startup/login 将其设置为 运行?
这是我自己的回答:
而不是使用 set ProcessList to name of every process
。
改用 shell 脚本作为 root。
set kill_pid to do shell script "ps ax | grep sshd | grep -v grep | awk '{ print }'" password "<PASSWORD>" with administrator privileges
但这会引发一个问题,因为当有人使用 SSH 远程访问您的计算机时,三个不同的用户 运行 进程 sshd。
三个用户是:
根 _sshd
此外,用户 _sshd 会在大约十到二十秒后消失。
因此,这意味着前几秒 kill_pid
包含三、五位数字,但之后 kill_pid
仅包含两个五位数字。此外,这些数字由 space.
这个问题的修复相当简单:
set kill_pid to do shell script "echo '" & kill_pid & "'| cut -c 7-"
if length of kill_pid > 5 then
set kill_pid to do shell script "echo '" & kill_pid & "'| cut -c 7-"
end if
总的来说这个问题还是比较多的。如果我希望脚本继续检查 sshd 而不是一次,我必须将整个程序放入循环中。虽然,当进程 sshd 不是 运行ning 时,这:
set kill_pid to do shell script "ps ax | grep sshd | grep -v grep | awk '{ print }'" password "<password>" with administrator privileges
returns 一个错误。
这也是一个非常简单的修复,您只需使用 try
。
在上下文中,它看起来像这样:
repeat
try
set kill_pid to do shell script "ps ax | grep sshd | grep -v grep | awk '{ print }'" password "<password>" with administrator privileges
set kill_pid to do shell script "echo '" & kill_pid & "'| cut -c 7-"
if length of kill_pid > 5 then
set kill_pid to do shell script "echo '" & kill_pid & "'| cut -c 7-"
end if
end try
end repeat
最后一个问题是,重复此操作会导致脚本占用我 CPU 的 400% 中的 127%。
同样,一个非常简单的修复:
delay 1
这是我的全部代码:
repeat
try
set kill_pid to do shell script "ps ax | grep sshd | grep -v grep | awk '{ print }'" password "<PASSWORD>" with administrator privileges
set kill_pid to do shell script "echo '" & kill_pid & "'| cut -c 7-"
if length of kill_pid > 5 then
set kill_pid to do shell script "echo '" & kill_pid & "'| cut -c 7-"
end if
if kill_pid is equal to "" then
end if
if kill_pid is not equal to "" and length of kill_pid is equal to 5 then
tell application "System Events"
set question to display dialog "Do you want SSH to be running right now?" buttons {"No", "Yes"} default button 2
set answer to button returned of question
if answer is equal to "No" then
do shell script "kill -KILL " & kill_pid password "<PASSWORD>" with administrator privileges
end if
if answer is equal to "Yes" then
set wait to display dialog "Press OK when you are done running SSH. Or click Stop to stop checking for SSH." buttons {"Stop", "OK"} default button 2
set ok to button returned of wait
if ok is equal to "OK" then
end if
if ok is equal to "Stop" then
exit repeat
end if
end if
end tell
end if
end try
delay 1
end repeat
现在,如果您希望此 运行 作为应用程序并 运行 在登录时执行以下操作:
将脚本复制并粘贴到脚本编辑器中,然后转到“文件”>“导出”并将其另存为应用程序。
然后在登录时将其设置为 运行,您必须转到系统偏好设置>用户和组>登录项并将其设置为 运行 在登录时。
最后,如果您想要自定义应用程序图标,只需将图像文件的一部分复制并粘贴到“获取信息”选项卡中的图标中即可。
复制 (Command+c) 一张图片。
右键单击该应用程序。
单击小图标(它应该以蓝色突出显示)并粘贴 (Command+v) 复制的图片。
现在您有一个应用程序可以检查计算机上是否存在任何 SSH 运行!
编辑
我添加了一项功能,以便您可以查看通过 SSH 连接到您的计算机的 IP。
这是新代码:
repeat
try
set kill_pid to do shell script "ps ax | grep sshd | grep -v grep | awk '{ print }'" password "PASSWORD" with administrator privileges
set kill_pid to do shell script "echo '" & kill_pid & "'| cut -c 7-"
if length of kill_pid > 5 then
set kill_pid to do shell script "echo '" & kill_pid & "'| cut -c 7-"
end if
if kill_pid is equal to "" then
end if
if kill_pid is not equal to "" and length of kill_pid is equal to 5 then
tell application "System Events"
set userset to do shell script ("w")
set question to display dialog "Do you want SSH to be running right now?" buttons {"Show Users", "No", "Yes"} default button 3
set answer to button returned of question
if answer is equal to "Show Users" then
set userset to do shell script "echo '" & userset & "'| cut -c 56-"
set question2 to display dialog "Current users:
" & userset buttons {"Stop SSH", "Run SSH"} default button 2
set answer2 to button returned of question2
if answer2 is equal to "Stop SSH" then
set answer to "No"
end if
if answer2 is equal to "Run SSH" then
set answer to "Yes"
end if
end if
if answer is equal to "No" then
do shell script "kill -KILL " & kill_pid password "PASSWORD" with administrator privileges
end if
if answer is equal to "Yes" then
set wait to display dialog "Press OK when you are done running SSH. Or click Stop to stop checking for SSH." buttons {"Stop", "OK"} default button 2
set ok to button returned of wait
if ok is equal to "OK" then
end if
if ok is equal to "Stop" then
exit repeat
end if
end if
end tell
end if
end try
delay 1
end repeat