diff options
| -rwxr-xr-x[-rw-r--r--] | client_ls.py | 10 | ||||
| -rw-r--r-- | client_manager.py | 16 | ||||
| -rwxr-xr-x | cron_setup.sh | 22 | ||||
| -rw-r--r-- | docs | 6 |
4 files changed, 48 insertions, 6 deletions
diff --git a/client_ls.py b/client_ls.py index 6aa3a7e..fdcfc78 100644..100755 --- a/client_ls.py +++ b/client_ls.py | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | 1 | #!/usr/bin/env python3 | |
| 2 | #lists client information | 2 | #lists client information |
| 3 | 3 | ||
| 4 | import fcntl | 4 | import fcntl |
| @@ -12,10 +12,14 @@ def read_data(): | |||
| 12 | # Get shared lock for reading | 12 | # Get shared lock for reading |
| 13 | fcntl.flock(f.fileno(), fcntl.LOCK_SH) | 13 | fcntl.flock(f.fileno(), fcntl.LOCK_SH) |
| 14 | try: | 14 | try: |
| 15 | return json.load(f) | 15 | data = json.load(f) |
| 16 | for pid, info in data.items(): | ||
| 17 | print(info) | ||
| 18 | return {} | ||
| 16 | finally: | 19 | finally: |
| 17 | fcntl.flock(f.fileno(), fcntl.LOCK_UN) | 20 | fcntl.flock(f.fileno(), fcntl.LOCK_UN) |
| 18 | except (FileNotFoundError, ValueError): | 21 | except (FileNotFoundError, ValueError): |
| 22 | print("file /tmp/ssh_sessions.json not found") | ||
| 19 | return {} | 23 | return {} |
| 20 | 24 | ||
| 21 | print(read_data()) | 25 | read_data() |
diff --git a/client_manager.py b/client_manager.py index 84bf3df..ab29e16 100644 --- a/client_manager.py +++ b/client_manager.py | |||
| @@ -15,6 +15,7 @@ import fcntl | |||
| 15 | 15 | ||
| 16 | def get_ssh_port(pid): | 16 | def get_ssh_port(pid): |
| 17 | ''' | 17 | ''' |
| 18 | /var/log/secure | ||
| 18 | Jan 31 07:50:28 vultr sshd[43690]: Accepted publickey for root from 210.10.76.5 port 43730 ssh2: ED25519 SHA256:qz9ffMCb3vPlabn3ZHee00qIPBxkDiUiVSorcUkGdII | 19 | Jan 31 07:50:28 vultr sshd[43690]: Accepted publickey for root from 210.10.76.5 port 43730 ssh2: ED25519 SHA256:qz9ffMCb3vPlabn3ZHee00qIPBxkDiUiVSorcUkGdII |
| 19 | Jan 31 07:50:28 vultr sshd[43690]: pam_unix(sshd:session): session opened for user root(uid=0) by root(uid=0) | 20 | Jan 31 07:50:28 vultr sshd[43690]: pam_unix(sshd:session): session opened for user root(uid=0) by root(uid=0) |
| 20 | Jan 31 07:50:29 vultr sshd[43693]: Received disconnect from 210.10.76.5 port 43730:11: disconnected by user | 21 | Jan 31 07:50:29 vultr sshd[43693]: Received disconnect from 210.10.76.5 port 43730:11: disconnected by user |
| @@ -22,6 +23,13 @@ def get_ssh_port(pid): | |||
| 22 | Jan 31 07:50:29 vultr sshd[43690]: pam_unix(sshd:session): session closed for user root | 23 | Jan 31 07:50:29 vultr sshd[43690]: pam_unix(sshd:session): session closed for user root |
| 23 | ''' | 24 | ''' |
| 24 | ''' | 25 | ''' |
| 26 | lsof -i -n | ||
| 27 | sshd 1845 root 4u IPv4 23137 0t0 TCP 45.32.108.159:ssh->210.10.76.5:45460 (ESTABLISHED) | ||
| 28 | sshd 1848 root 4u IPv4 23137 0t0 TCP 45.32.108.159:ssh->210.10.76.5:45460 (ESTABLISHED) | ||
| 29 | sshd 1848 root 8u IPv4 23259 0t0 TCP *:44699 (LISTEN) | ||
| 30 | sshd 1848 root 9u IPv6 23260 0t0 TCP *:44699 (LISTEN) | ||
| 31 | ''' | ||
| 32 | ''' | ||
| 25 | # less efficient but readable | 33 | # less efficient but readable |
| 26 | pid = '33216' | 34 | pid = '33216' |
| 27 | pids = [] | 35 | pids = [] |
| @@ -100,11 +108,13 @@ def handle_log_change(event): | |||
| 100 | keyname = get_keyname(line.split()[15]) | 108 | keyname = get_keyname(line.split()[15]) |
| 101 | srcip = line.split()[10] | 109 | srcip = line.split()[10] |
| 102 | #print(pid, port, keyname, srcip) | 110 | #print(pid, port, keyname, srcip) |
| 103 | ssh_sessions[pid] = [srcip, keyname, port] | 111 | #ssh_sessions[pid] = [srcip, keyname, port] |
| 104 | ssh_sessions[pid] = { | 112 | |
| 113 | ssh_sessions[srcip] = { | ||
| 105 | 'srcip': srcip, | 114 | 'srcip': srcip, |
| 115 | 'pid' : pid, | ||
| 106 | 'key': keyname, | 116 | 'key': keyname, |
| 107 | 'pubport': port | 117 | 'pubport': port, |
| 108 | } | 118 | } |
| 109 | write_data(ssh_sessions) | 119 | write_data(ssh_sessions) |
| 110 | if "pam_unix(sshd:session): session closed" in line: | 120 | if "pam_unix(sshd:session): session closed" in line: |
diff --git a/cron_setup.sh b/cron_setup.sh new file mode 100755 index 0000000..ca2fdbd --- /dev/null +++ b/cron_setup.sh | |||
| @@ -0,0 +1,22 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | |||
| 3 | # add client_manager python to crontab | ||
| 4 | |||
| 5 | if [ "$EUID" -ne 0 ]; then | ||
| 6 | echo "ERROR: This script must be run as root" | ||
| 7 | exit 1 | ||
| 8 | fi | ||
| 9 | |||
| 10 | # Get script's directory | ||
| 11 | SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" | ||
| 12 | |||
| 13 | # Create a temporary file with the cron entry | ||
| 14 | echo "@reboot /usr/bin/python3 $SCRIPT_DIR/client_manager.py" > temp_cron | ||
| 15 | |||
| 16 | # Append this to the user's crontab | ||
| 17 | crontab -l > current_cron 2>/dev/null || true # Get current crontab or empty if none exists | ||
| 18 | cat temp_cron >> current_cron | ||
| 19 | crontab current_cron | ||
| 20 | |||
| 21 | # Clean up temporary files | ||
| 22 | rm temp_cron current_cron | ||
| @@ -0,0 +1,6 @@ | |||
| 1 | |||
| 2 | client_ls.py client_manager.py cron_setup.sh sys_init.py | ||
| 3 | |||
| 4 | -you can run client_manager.py as standalone | ||
| 5 | -cron_setup.py just adds client_manager.py to crontab | ||
| 6 | |||
