用于解析 XRDP 几何/分辨率的“ps -ef”的 Bash 命令

用于解析 XRDP 几何/分辨率的“ps -ef”的 Bash 命令

以下 bash 命令(在 Ubuntu 18.04 上运行)在表格中显示所需的 XRDP 信息,但每个 XRDP 会话的分辨率(几何形状)除外

{ 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

给出输出,例如

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

除了用户名、pid、Xdisplay 和端口号的现有详细信息之外,我们如何使用它来显示 RDP 会话分辨率(几何)?


所需的输出格式

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

示例输出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

输出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

使用cat netstatoutin place ofsudo netstat -tlpncat psoutin place ofps -ef来进行演示:

$ 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

相关内容