Linux 找出当前存在多少 SSH 连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17199089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:15:30  来源:igfitidea点击:

Find out how much SSH-connections currently exist

linuxbashshell

提问by Chrispie

I'm using a simple Shell-script on my Linux-server which checks, if a rsync-job is running or if any client some directories from the server via Samba. If this is the case then nothing happens, but are there no jobs and no Samba is used, than the server goes into hibernate.

我在我的 Linux 服务器上使用一个简单的 Shell 脚本来检查 rsync 作业是否正在运行,或者是否有任何客户端通过 Samba 从服务器获取某些目录。如果是这种情况,那么什么都不会发生,但是没有作业并且没有使用 Samba,然后服务器进入休眠状态。

Is there any simple command, which I can use to check if a ssh-connection to the server exist? I want to add this to my Shell-script, so that the server doesn't go to hibernate, if such a connection exist.

是否有任何简单的命令可以用来检查是否存在与服务器的 ssh 连接?我想将它添加到我的 Shell 脚本中,这样服务器就不会进入休眠状态(如果存在这样的连接)。

采纳答案by TimWolla

Scan the processlist for sshd:. Established connections look something like this: sshd: <username>…

扫描进程列表中的sshd:. 已建立的连接如下所示:sshd: <username>…

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

should work out for you.

应该适合你。

回答by exussum

use whocommand

使用who命令

it gives output like

它给出了类似的输出

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

You could parse that to see how many non locals are added and get their usernames (or there are options see man whofor more info

您可以解析它以查看添加了多少非本地人并获取他们的用户名(或者有选项查看man who更多信息

gives a count of how many non localhost users there are

计算有多少非本地主机用户

who | grep -v localhost | wc -l