0

The following bash command (run on Ubuntu 18.04) displays the required XRDP information in a table, except for the resolution (geometry) of each XRDP session

{ echo "user pid Xdisplay port"; { ps -ef | awk '/Xvnc :[[:digit:]]+/ {print $1,$2,$9}' | while read tu tpid tvnc; do sudo netstat -tlpn | awk -v "tpid=${tpid}" '$0 ~ tpid {print $4;}' | sed -r -e 's/^.*://;' -e "s/^/${tu} ${tpid} ${tvnc} /;" ; done ; } | sort -k3 ; } | column -c4 -t

gives the output, such as

user   pid     Xdisplay  port
baz    127229  :11       5911
bar    27817   :12       5912
baz    48659   :13       5913

How can we use to display the RDP session resolution (geometry) in addition to the existing details of the user name, pid, Xdisplay and port number?


Desired output format

user   pid     Xdisplay  port     geometry
bar    108415  :1        5901     1440x900
foo    17056   :2        5902     1920x1200
baz    127229  :11       5911     1920x1200
bar    27817   :12       5912     2560x1440
baz    48659   :13       5913     1920x1200

Example output of ps -ef | grep 'Xvnc'

foo      17056      1  0 Jun18 ?        07:00:08 Xvnc4 :2 -desktop myubuntupc:2 (foo) -auth /home/foo/.Xauthority -geometry 1920x1200 -depth 16 -rfbwait 30000 -rfbauth /home/foo/.vnc/passwd -rfbport 5902 -pn -fp /usr/X11R6/lib/X11/fonts/Type1/,/usr/X11R6/lib/X11/fonts/Speedo/,/usr/X11R6/lib/X11/fonts/misc/,/usr/X11R6/lib/X11/fonts/75dpi/,/usr/X11R6/lib/X11/fonts/100dpi/,/usr/share/fonts/X11/misc/,/usr/share/fonts/X11/Type1/,/usr/share/fonts/X11/75dpi/,/usr/share/fonts/X11/100dpi/ -co /etc/X11/rgb
bar      27817  27805  0 Aug06 ?        00:00:21 Xvnc :12 -geometry 2560x1440 -depth 24 -rfbauth /home/bar/.vnc/sesman_bar_passwd -bs -ac -nolisten tcp -localhost -dpi 96
baz      48659  48657 12 Sep02 ?        12:16:26 Xvnc :13 -auth .Xauthority -geometry 1920x1200 -depth 32 -rfbauth /home/baz/.vnc/sesman_passwd-baz@myubuntupc:13 -bs -nolisten tcp -localhost -dpi 96
bar     108415      1  0 Jul16 ?        02:09:36 Xvnc4 :1 -desktop myubuntupc:1 (bar) -auth /home/bar/.Xauthority -geometry 1440x900 -depth 16 -rfbwait 30000 -rfbauth /home/bar/.vnc/passwd -rfbport 5901 -pn -fp /usr/X11R6/lib/X11/fonts/Type1/,/usr/X11R6/lib/X11/fonts/Speedo/,/usr/X11R6/lib/X11/fonts/misc/,/usr/X11R6/lib/X11/fonts/75dpi/,/usr/X11R6/lib/X11/fonts/100dpi/,/usr/share/fonts/X11/misc/,/usr/share/fonts/X11/Type1/,/usr/share/fonts/X11/75dpi/,/usr/share/fonts/X11/100dpi/ -co /etc/X11/rgb
bar     115933 115125  0 15:17 pts/29   00:00:00 grep --color=auto Xvnc
baz     127229 127227  0 Sep01 ?        00:02:56 Xvnc :11 -auth .Xauthority -geometry 1920x1200 -depth 16 -rfbauth /home/baz/.vnc/sesman_passwd-baz@myubuntupc:11 -bs -nolisten tcp -localhost -dpi 96

Output of sudo netstat -tlpn

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:41603         0.0.0.0:*               LISTEN      88147/ssh       
tcp        0      0 127.0.0.1:38609         0.0.0.0:*               LISTEN      87886/ssh       
tcp        0      0 0.0.0.0:6001            0.0.0.0:*               LISTEN      108415/Xvnc4    
tcp        0      0 0.0.0.0:6002            0.0.0.0:*               LISTEN      17056/Xvnc4     
tcp        0      0 127.0.1.1:53            0.0.0.0:*               LISTEN      7966/dnsmasq    
tcp        0      0 127.0.0.1:3350          0.0.0.0:*               LISTEN      106498/xrdp-sesman
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      43940/sshd      
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      17827/cupsd     
tcp        0      0 127.0.0.1:5911          0.0.0.0:*               LISTEN      127229/Xvnc     
tcp        0      0 127.0.0.1:5912          0.0.0.0:*               LISTEN      27817/Xvnc      
tcp        0      0 127.0.0.1:5913          0.0.0.0:*               LISTEN      48659/Xvnc      
tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN      105821/master   
tcp        0      0 0.0.0.0:3389            0.0.0.0:*               LISTEN      106523/xrdp     
tcp6       0      0 ::1:41603               :::*                    LISTEN      88147/ssh       
tcp6       0      0 :::5901                 :::*                    LISTEN      108415/Xvnc4    
tcp6       0      0 :::5902                 :::*                    LISTEN      17056/Xvnc4     
tcp6       0      0 ::1:38609               :::*                    LISTEN      87886/ssh       
tcp6       0      0 :::22                   :::*                    LISTEN      43940/sshd      
tcp6       0      0 ::1:631                 :::*                    LISTEN      17827/cupsd     
tcp6       0      0 :::25                   :::*                    LISTEN      105821/master  
  • 1
    @EdMorton Updated the question with the requested information, thank you for helping to improve the question. – Athena Wisdom Sep 6 at 19:25
2

Using cat netstatout in place of sudo netstat -tlpn and cat psout in place of ps -ef for demonstration purposes:

$ cat tst.awk
BEGIN {
    OFS = "\t"
    print 0, 0, "user", "pid", "Xdisplay", "port", "geometry"
}
NR==FNR {
    port = $4; sub(/.*:/,"",port)
    pid = $NF; sub("/.*","",pid)
    pid2port[pid] = port
    next
}
/Xvnc[0-9]* :[[:digit:]]/ {
    port = pid2port[$2]
    match($0,/geometry [^ ]+/)
    geometry = substr($0,RSTART+9,RLENGTH-9)
    display = $9; sub(/:/,"",display)
    print 1, display, $1, $2, $9, port, geometry
}

.

$ awk -f tst.awk <(cat netstatout) <(cat psout) | sort -k1,1n -k2,2n | cut -f3- | column -t
user  pid     Xdisplay  port  geometry
bar   108415  :1        5901  1440x900
foo   17056   :2        5902  1920x1200
baz   127229  :11       5911  1920x1200
bar   27817   :12       5912  2560x1440
baz   48659   :13       5913  1920x1200
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.