0

Looking for a script what will show all logged users sorted by FreeBSD jails where they're logged in. So, need run the who command in all currently running FreeBSD jails and in the main host too.

I make this:

who    #main host
jls | grep -v JID | while read jid ip host path
do
    echo $jid $host
    jexec $jid who
done

but the jexec need root execution and i'm logging in usually as non-root and make su everytime is painfull...

Is here any other simple way?

novacik
  • 1,497
  • 1
  • 9
  • 19

2 Answers2

0

The who command in FreeBSD knows a file argument from where read informations about the logged-in users, the default is /var/run/utx.active - and the file is usually world-readable...

Probably will be enough the next script:

#!/usr/local/bin/bash

while read  jpath
do
    echo JWHO: ${jpath:-$(hostname)}
    who "${jpath}/var/run/utx.active"
done < <( jls -h path | sed '1s:.*::' )

example output:

JWHO: marvin.example.com
smith              pts/0     7 nov 20:55 (adsl2343-some-another.example.com)
JWHO: /jails/jail1
JWHO: /jails/testjail
root              pts/2     7 nov 20:55 (someother.example.com)
JWHO: /jails/dbjail

steps:

  • show the path to "root filesystem" for all running jails
  • run the who for the /var/run/utx.active for the given jail
  • skip the header line from the jls, - so the 1st output will be the host.

Maybe someone know much simpler solution, e.g. by sorting the ps output or something like...

Comments: you usually don't want to use constructions like command | while read - the pipe forks new shell and you losing values of the variables set inside of the loop, the done < <( commands ) is usually better...

clt60
  • 62,119
  • 17
  • 107
  • 194
0

You can enable sudo in your system change your script just a little to:

sudo jexec $jid who

Then your srcipt can run as normal user.

Jimmy Koerting
  • 1,231
  • 1
  • 14
  • 27