This commit is contained in:
Christoffer Martinsson 2025-10-12 18:58:07 +02:00
parent c312916687
commit c8c91bdfec

View File

@ -559,7 +559,9 @@ impl ServiceCollector {
} }
async fn get_ssh_active_users(&self) -> Option<String> { async fn get_ssh_active_users(&self) -> Option<String> {
let output = Command::new("who") // Use ss to find established SSH connections on port 22
let output = Command::new("ss")
.args(["-tn", "state", "established", "sport", "= :22"])
.stdout(Stdio::piped()) .stdout(Stdio::piped())
.stderr(Stdio::piped()) .stderr(Stdio::piped())
.output() .output()
@ -571,32 +573,21 @@ impl ServiceCollector {
} }
let stdout = String::from_utf8_lossy(&output.stdout); let stdout = String::from_utf8_lossy(&output.stdout);
let mut ssh_users = Vec::new(); let mut connections = 0;
for line in stdout.lines() { // Count lines excluding header
let parts: Vec<&str> = line.split_whitespace().collect(); for line in stdout.lines().skip(1) {
if parts.len() >= 2 { if !line.trim().is_empty() {
let user = parts[0]; connections += 1;
let terminal = parts[1];
// SSH sessions typically show pts/X terminals
if terminal.starts_with("pts/") {
ssh_users.push(user);
}
} }
} }
if ssh_users.is_empty() { if connections == 0 {
None None
} else if connections == 1 {
Some("1 SSH connection".to_string())
} else { } else {
let unique_users: std::collections::HashSet<&str> = ssh_users.into_iter().collect(); Some(format!("{} SSH connections", connections))
let count = unique_users.len();
let users: Vec<&str> = unique_users.into_iter().collect();
if count == 1 {
Some(format!("1 active user: {}", users[0]))
} else {
Some(format!("{} active users: {}", count, users.join(", ")))
}
} }
} }