LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-10-2019, 11:59 PM   #1
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,312
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
Signal when there are n users logged in via SSH


I'm looking for a simple way to signal when there are n or more users logged into a particular system via SSH. In particular I want to trigger actions when the threshold is passed in either direction. It is easy to trigger an action as root via PAM when someone logs in or out but that by itself is full of race conditions and so I am wondering about the logic required to prevent race conditions with a lot of concurrent users and as well as some people logged into the same account multipe times.

Should I just try to keep a single persistent file containing a counter that is incremented and decremented on login and logout? Or is there a better way. A shell script is preferable.

The following environment variables are available to PAM:
  • DBUS_SESSION_BUS_ADDRESS
  • LANG
  • PAM_RHOST
  • PAM_SERVICE
  • PAM_TTY
  • PAM_TYPE
  • PAM_USER
  • PWD
  • SSH_AUTH_INFO_0
  • XDG_RUNTIME_DIR
  • XDG_SESSION_CLASS
  • XDG_SESSION_ID
  • XDG_SESSION_TYPE

Last edited by Turbocapitalist; 11-11-2019 at 12:01 AM.
 
Old 11-11-2019, 01:56 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,859

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
yes, you can count log in/outs, but why do you need that?
you can avoid race conditions if you protect your increment/decrement with a semaphore.
 
Old 11-11-2019, 05:22 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,312

Original Poster
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
So something like this? It seems to work but I would welcome feedback, even on style, but especially on how to remove that one bashism.

Code:
#!/bin/bash

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin;
DIR="/tmp/foo-test"

test -d "${DIR}" || mkdir -p -m 700 "${DIR}" || exit 1
test -w "${DIR}" || exit 1

umask 027
lock="${DIR}/foo.lock"
exec 20>"${lock}"
flock -x -w 10 20 || exit 1

counter="${DIR}/foo.counter"

if ! test -f "${counter}"; then
        echo 0 > "${counter}"
fi

threshold=101

if [ "$PAM_TYPE" = "open_session" ] ; then
        c=$(cat "${counter}")
        c=$((c+1))
        if [ ${c} -le 0 ]; then
                c=0
        fi

        echo ${c}> "${counter}"
        trigger_script on

elif [ "$PAM_TYPE" = "close_session" ] ; then
        c=$(cat "${counter}")
        c=$((c-1))

        if [ ${c} -le 0 ]; then
                c=0
        fi

        echo ${c}> "${counter}"

        if [ ${c} -le ${threshold} ]; then
                trigger_script off
        fi

fi

exec 20>&-
exit 0
 
Old 11-11-2019, 07:47 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,859

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
more or less yes, that it is. What bashism do you want to remove?
I think you can set lock on counter, there is no need for another lockfile.
 
Old 11-11-2019, 09:17 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,312

Original Poster
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
Thanks. It sounds good to just use one file for both the counter and the lock.

As far as bashisms go, these two seem one type:

Code:
. . .
exec 20>"${lock}"
. . .
exec 20>&-
. . .
Is there a generic POSIX way to do the lock file?
 
Old 11-11-2019, 11:17 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,859

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
probably lockfile is a bit better.
i would lock only the increment/decrement parts, not the whole script.
and also use:
Code:
c=$(<"${counter}") # without cat
but most probably these are not really relevant changes
 
1 members found this post helpful.
Old 11-11-2019, 12:05 PM   #7
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
What about periodic parsing of the output of:
Code:
netstat -tnp | grep <localIP>:22 | sort -k8
or, if you only care about counts, and not who:
Code:
netstat -tn | grep <localIP>:22 | wc -l
no parsing required. If the result is => n, trigger your action. Run as frequently as you want via cron or in a loop with a sleep.

Am I missing something?

Last edited by scasey; 11-11-2019 at 12:08 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
checking for user who are logged in, the display first,last name and time logged in LBP74 Programming 1 01-07-2013 04:23 AM
Why am I told I'm logged out after I've logged in? Airidh LQ Suggestions & Feedback 1 02-25-2011 10:35 AM
IPs logged as D.C.B.A and some times A.B.C.D how to find which format is logged tkmsr Linux - Security 15 11-18-2010 08:29 AM
kde much slower to start when logged in as alan than logged in as root arubin Slackware 0 04-26-2004 04:27 PM
mozilla works fine when logged in as a user but crashes when logged in as root jimi Linux - General 6 04-02-2003 08:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration