[RndTbl] Protip: dealing with remote terminals, and /proc/pid/environ

Scott Balneaves sbalneav at legalaid.mb.ca
Mon Mar 12 11:29:30 CDT 2007


Hello all

Bumped into something I needed to do this morning, thought others may be
interested.

I needed to get an accurate count of how many 15 inch monitors I had on my
LTSP network.  One can, of course, get this from xdpyinfo, but I can't
ssh into clients to do this.  What to do, what to do...

Well, of course, what I need to do is set my $DISPLAY and $XAUTHORITY
environment variables correctly, so I can run xdpyinfo.  But, how
to find out who's on what terminal?

Well, since everyone runs metacity as a window manager:

pgrep metacity

returns a list of process id's.  From there, we can look in
/proc/<pid>/environ, and get some more info!  But hold on:

cat /proc/3660/environ

returns:
SSH_CLIENT=192.168.0.229 
443422USER=foobarMAIL=/var/mail/foobarGNOME_KEYRING_SOCKET=/tmp/keyring-awvD15/socketSSH_AGENT_PID=3510

etc.  Environment variables are null terminated!  So, how to get around that?

As usual, there's a quick easy way to do it: tr to the rescue:

cat /proc/3660/environ | tr '\0' '\n'

SSH_CLIENT=192.168.0.229 4434 22
USER=foobar
MAIL=/var/mail/foobar
GNOME_KEYRING_SOCKET=/tmp/keyring-awvD15/socket

etc.

Hey!  Fit for using with eval!  Here's the whole script, just for fun.

#!/bin/bash

for PROC in $(pgrep metacity); do
         eval $(cat /proc/$PROC/environ | tr '\0' '\n' | grep SSH_CLIENT | awk 
'{print $1}')
         eval $(cat /proc/$PROC/environ | tr '\0' '\n' | grep USER)

         export DISPLAY=$SSH_CLIENT:6
         export XAUTHORITY=/usr/legal/home/$USER/.Xauthority
         echo -n ${USER}
         xdpyinfo | grep dimension
done

Hope someone finds this useful.

Scott

-- 
Scott L. Balneaves | "Let us endeavor so to live that when we come to die
Systems Department |  even the undertaker will be sorry."
Legal Aid Manitoba |    -- Mark Twain, "Pudd'nhead Wilson's Calendar"


More information about the Roundtable mailing list