检查ssh是否正在另一个管理员用户上运行,并将其杀死。(AppleScript)

检查ssh是否正在另一个管理员用户上运行,并将其杀死。(AppleScript),ssh,applescript,kill-process,multiple-users,Ssh,Applescript,Kill Process,Multiple Users,我想检查进程“sshd”是否在用户“ladmin”上运行,如果是,则从我的用户“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

我想检查进程“sshd”是否在用户“ladmin”上运行,如果是,则从我的用户“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在我的用户上,它也不会显示


另外,有没有办法将其设置为在启动/登录时运行?

以下是我自己的答案:

不要使用
将ProcessList设置为每个进程的名称
。 改用shell脚本作为根

set kill_pid to do shell script "ps ax | grep sshd | grep -v grep | awk '{ print $1 }'" password "<PASSWORD>" with administrator privileges
总的来说,这方面还有更多的问题。如果我想让脚本继续检查sshd,而不是只检查一次,我必须将整个程序放入一个循环中。但是,当进程sshd未运行时,这:

set kill_pid to do shell script "ps ax | grep sshd | grep -v grep | awk '{ print $1 }'" password "<password>" with administrator privileges
最后一个问题是,重复此操作会导致脚本占用我400%的CPU中的127%

同样,一个非常简单的解决方案:

delay 1
这是我的全部代码:

repeat
    try
        set kill_pid to do shell script "ps ax | grep sshd | grep -v grep | awk '{ print $1 }'" 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
delay 1
repeat
    try
        set kill_pid to do shell script "ps ax | grep sshd | grep -v grep | awk '{ print $1 }'" 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
repeat
    try
        set kill_pid to do shell script "ps ax | grep sshd | grep -v grep | awk '{ print $1 }'" 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