Voici ma réponse:
Au lieu d'utiliser set ProcessList to name of every process
. Utilisez un script shell en tant que root à la place.
set kill_pid to do shell script "ps ax | grep sshd | grep -v grep | awk '{ print $1 }'" password "<PASSWORD>" with administrator privileges
Mais cela pose un problème parce que quand quelqu'un accède à distance de votre ordinateur avec SSH trois différents utilisateurs exécutent le processus sshd.
Les trois utilisateurs sont:
racine _sshd
En outre, le _sshd utilisateur disparaît au bout d'environ dix à vingt secondes. Donc, cela signifie que pour les premières secondes kill_pid
contient trois chiffres à cinq chiffres, mais après ce temps kill_pid
ne contient que deux nombres à cinq chiffres. De plus, ces nombres sont séparés par un espace.
Le correctif pour cela est assez facile:
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
Il est encore plus de problèmes avec ce au total. Si je veux que le script vérifie sshd et pas seulement une fois, je dois mettre tout le programme en boucle. Cependant, lorsque le processus sshd n'est pas en cours d'exécution, ceci:
set kill_pid to do shell script "ps ax | grep sshd | grep -v grep | awk '{ print $1 }'" password "<password>" with administrator privileges
renvoie une erreur.
De plus, ceci est une solution assez simple, il vous suffit d'utiliser try
. dans le contexte cela ressemble à ceci:
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
end try
end repeat
Le dernier problème est que répéter ce qui provoque le script de prendre 127% sur 400% de mon CPU.
Encore une fois, une solution très simple:
delay 1
Voici mon code entier:
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
Maintenant, si vous voulez que cela fonctionne comme une application et exécuter à la connexion, procédez comme suit:
Copiez et collez le script dans votre éditeur de script et allez dans Fichier> Exporter et enregistrez-le en tant qu'application.
![enter image description here](https://i.stack.imgur.com/lGpBF.png)
Ensuite, pour l'exécuter à la connexion, vous devez allez dans Préférences Système> Utilisateurs Groupes &> Éléments de connexion et le mettre à courir à la connexion.
![enter image description here](https://i.stack.imgur.com/au0TQ.png)
Enfin, si vous voulez une icône d'application personnalisée simplement copier et coller une partie d'un fichier d'image dans l'icône dans l'onglet Obtenir des informations.
Copie (Commande + c) d'une partie d'une image.
![enter image description here](https://i.stack.imgur.com/hgYBV.png)
Faites un clic droit sur l'application.
![enter image description here](https://i.stack.imgur.com/fjJXY.png)
Cliquez sur la petite icône (il devrait mettre en évidence en bleu) et coller (Ctrl + v) l'image copiée.
![enter image description here](https://i.stack.imgur.com/KwElL.png)
Et maintenant, vous avez une application qui vérifie pour toutes les SSHS en cours d'exécution sur votre ordinateur!
![enter image description here](https://i.stack.imgur.com/Vo4Gr.jpg)
EDIT
J'ai ajouté une fonctionnalité afin que vous puissiez voir ce que IP est SSHed dans votre ordinateur.
Voici le nouveau code:
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