Linux检索显示器名称

情况:我使用多个显示器,我想在bash中获取他们的名字。 目前我正在使用Ubuntu 10.04。

我知道xrandr。 从这里我只能得到统计数据。 我想要的是读取一个数组中的所有显示器名称,以与他们一起工作。

有没有一个明确的方法来做到这一点,而不切断某种string的名称? 一个明确的方法是从文件中读取它们。 一个不明确的方法是将xrandr输出pipe道输出为某种function来从中删除名称。

sudo get-edid没有为我工作。 (编辑:现在在另一台电脑上工作,Lubuntu 14.10;我会责怪BIOS的差异,但这是一个随机猜测…)

无论如何,在X下, xrandr --verbose打印EDID块。 这是一个快速和肮脏的方式来提取它并传递给parse-edid

 #!/bin/bash xrandr --verbose | perl -ne ' if ((/EDID(_DATA)?:/.../:/) && !/:/) { s/^\s+//; chomp; $hex .= $_; } elsif ($hex) { # Use "|strings" if you dont have read-edid package installed # and just want to see (or grep) the human-readable parts. open FH, "|parse-edid"; print FH pack("H*", $hex); $hex = ""; }' 

受Beni的回答的启发,这将使用xrandr读取EDID数据,并根据EDID规范提取显示器名称,而不需要任何外部工具,如parse-edid

 #!/bin/sh xrandr --verbose | awk ' /[:.]/ && hex { sub(/.*000000fc00/, "", hex) hex = substr(hex, 0, 26) "0a" sub(/0a.*/, "0a", hex) print hex hex="" } hex { gsub(/[ \t]+/, "") hex = hex $0 } /EDID.*:/ { hex=" " }' | xxd -r -p 

使用awk来精确地提取显示名称,并且没有额外的EDID垃圾,因此“幻数”如000000fc000a 。 最后使用xxd将十六进制转换为ASCII,每行打印一个监视器名称。

基于这个解决方案,我做了一个方便的脚本来切换显示器 ,这也可以用来简单地列出显示器信息:

 $ monitor-switch --list Connected monitors: # DFP5 HDMI HT-R391 # DFP7 DVI-I DELL U2412M 

如果你不想分析xrandr输出,用libXrandr编写一个C程序,只能得到你想要的。 如果你想要做的只是查询信息,可以快速完成。 阅读这个文件 。

如果你想获得真正的显示器名称,替代@ dtmilano的解决方案是使用libXrandr获取监视器的EDID属性,然后手动解析并打印(读取EDID规范)。

xrandr源代码 。

我知道这是一个肮脏的方式,但它给了我一些监视器模型的名称甚至比sudo get-edid|parse-edid更好。 它读取数组中的信息,并以可读取的方式输出它,就像读取文件一样。 你可以根据你的需要修改它。

 #!/bin/bash # # # get-monitors.sh # # Get monitor name and some other properties of connected monitors # by investigating the output of xrandr command and EDID data # provided by it. # # Copyright (C) 2015,2016 Jarno Suni <8@iki.fi> # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. See <http://www.gnu.org/licenses/gpl.html> set -o nounset set -o errexit # EDID format: # http://en.wikipedia.org/wiki/Extended_Display_Identification_Data#EDID_1.3_data_format # http://read.pudn.com/downloads110/ebook/456020/E-EDID%20Standard.pdf declare -r us=';' # separator string; # If EDID has more than one field with same tag, concatenate them, # but add this string in between. declare -r fs=$'\x1f' # Field separator for internal use; # must be a character that does not occur in data fields. declare -r invalid_edid_tag='--bad EDID--' # If base EDID is invalid, don't try to extract information from it, # but assign this string to the fields. # Get information in these arrays: declare -a outs # Output names declare -a conns # Connection type names (if available) declare -a names # Monitor names (but empty for some laptop displays) declare -a datas # Extra data; may include laptop display brand name # and model name declare -i no # number of connected outputs (to be counted) # xrandr command to use as a source of information: declare -r xrandr_output_cmd="xrandr --prop" hex_to_ascii() { echo -n "$1" | xxd -r -p } ascii_to_hex() { echo -n "$1" | xxd -p } get_info() { no=0 declare OIFS=$IFS; IFS=$fs while read -r output conn hexn hexd; do outs[no]="${output}" conns[no]="${conn}" names[no]="$(hex_to_ascii "$hexn")" datas[no]="$(hex_to_ascii "$hexd")" (( ++no )) done < <(eval $xrandr_output_cmd | gawk -v gfs="$fs" ' function print_fields() { print output, conn, hexn, hexd conn=""; hexn=""; hexd="" } function append_hex_field(src_hex,position,app_hex, n) { n=substr(src_hex,position+10,26) sub(/0a.*/, "", n) # EDID specification says field ends by 0x0a # (\n), if it is shorter than 13 bytes. #sub(/(20)+$/, "", n) # strip whitespace at the end of ascii string if (n && app_hex) return app_hex sp n else return app_hex n } function get_hex_edid( hex) { getline while (/^[ \t]*[[:xdigit:]]+$/) { sub(/[ \t]*/, "") hex = hex $0 getline } return hex } function valid_edid(hex, a, sum) { if (length(hex)<256) return 0 for ( a=1; a<=256; a+=2 ) { # this requires gawk sum+=strtonum("0x" substr(hex,a,2)) # this requires --non-decimal-data for gawk: #sum+=sprintf("%d", "0x" substr(hex,a,2)) } if (sum % 256) return 0 return 1 } BEGIN { OFS=gfs } /[^[:blank:]]+ connected/ { if (unprinted) print_fields() unprinted=1 output=$1 } /[^[:blank:]]+ disconnected/ { if (unprinted) print_fields() unprinted=0 } /^[[:blank:]]*EDID.*:/ { hex=get_hex_edid() if (valid_edid(hex)) { for ( c=109; c<=217; c+=36 ) { switch (substr(hex,c,10)) { case "000000fc00" : hexn=append_hex_field(hex,c,hexn) break case "000000fe00" : hexd=append_hex_field(hex,c,hexd) break } } } else { # set special value to denote invalid EDID hexn=iet; hexd=iet } } /ConnectorType:/ { conn=$2 } END { if (unprinted) print_fields() }' sp=$(ascii_to_hex $us) iet=$(ascii_to_hex $invalid_edid_tag)) IFS="$OIFS" } get_info # print the colums of each display quoted in one row for (( i=0; i<$no; i++ )); do echo "'${outs[i]}' '${conns[i]}' '${names[i]}' '${datas[i]}'" done 

你可以尝试ddcprobe和/或get-edid

 $ sudo apt-get install xresprobe read-edid $ sudo ddcprobe $ sudo get-edid 

您正在查找通过I²C总线传输并由您的视频驱动程序解释的EDID信息。 正如dtmilano所说,从ddcprobe获得编辑应该工作。

您也可以通过记录您的X开始获取这些信息:

 startx -- -logverbose 6 

几年前,我使用了一个名为read-edid的软件包来收集这些信息。

根据 2009年的这篇博客文章 ,read-edid软件包已经可以在Ubuntu上使用了。

适用于Ubuntu 16.04。 (我知道这个答案太晚了,但是这个解决方案今天是相关的)

 $ sudo apt-get install -y hwinfo ... $ hwinfo --monitor --short monitor: SONY TV AUO LCD Monitor 

我有两个显示器连接。 一个与笔记本电脑和另一个是外部显示器。 只要外接显示器插入或拔出,​​该命令就会反映出这种变化。 你不断需要投票。 删除--short选项提供更详细的信息。

您可以使用以下后台作业轮询状态:

 $ while true; > do > hwinfo --monitor --short; > sleep 2; > done >> monitor.log & 

while true循环运行无限次。 sleep 2暂停循环的每个迭代2秒。 并将hwinfo --monitor --short的输出追加到monitor.log 。 这个日志文件可以给你监视器插件和插件的活动历史记录。

仅供参考:我正在使用上述命令(以及其他类似的命令)使用背景(deamon)python脚本来检测是否有人正在使用计算机实验室中的某些硬件插件和插件。 如果是这样,我会得到适当的通知,几乎实时地将某人插入显示器,鼠标或键盘!

有关hwinfo命令的更多信息在这里 。 它的手册页也是一个很好的来源。