Find out how many SSH connections currently exist.

I use a simple Shell script on my Linux server that checks if the rsync job is running or if any client has some directories from the server through Samba. If so, nothing happens, but there are no tasks, and Samba is not used, by which the server goes into sleep mode.

Is there any simple command I can use to check for an ssh connection on the server? I want to add this to my Shell script so that the server does not go into sleep mode if such a connection exists.

+6
source share
2 answers

Scan the process list for sshd: The established connections look something like this: sshd: <username>…

 ps -A x |grep sshd |grep -v grep 

should work for you.

+7
source

use who command

it gives a result, for example

 username pts/1 2013-06-19 19:51 (ip) 

You can analyze this to find out how many nonlocal people have been added and get their usernames (or there are options, see man who for more information.

shows the number of users who are not local users,

 who | grep -v localhost | wc -l 
+7
source

Source: https://habr.com/ru/post/947678/


All Articles