For some reason I have he following scenario:
On boot-up I'm launching a script which waits for a given amount of time and checks whether an SSH connection was astablished during this time window or not. If a connection is open, the script does action A, else it kills sshd and does B.
What would be the best way to detect an open connection? (The script can be written in Bash or Ruby)
thx
Answer
If you want to detect a current SSH session, use lsof -i :22
and look for it returning more than 2 lines or grep for ESTABLISHED:[root@nemo ~]# lsof -i :22
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
sshd 3772 root 3u IPv6 9906 TCP *:ssh (LISTEN)
sshd 21376 root 3r IPv6 159072 TCP myserver:ssh->someip:27813 (ESTABLISHED)
sshd 21381 james 3u IPv6 159072 TCP myserver:ssh->someip:27813 (ESTABLISHED)
To see if a session was opened at all, look for something similar to the following in /var/log/secure (on redhat/centos/fedora):Sep 27 05:05:28 nemo sshd[21376]: Accepted password for james from some_ip port 27813 ssh2
If you allow authentication by means other than password, the log entries may be slightly different.
No comments:
Post a Comment