1

我需要解析 avahi 浏览结果以获取 PHP 中的一些信息。我不是基于“可解析的”Avahi 浏览(-p),因为这似乎比不可解析的更难解析,除非我弄错了。

由于我可以将浏览限制为某些类型,我认为我只需要从字符串中绘制 IP:端口和设备名称。首选 IPV4。该示例没有 IPV6,但有些搜索有,但我认为总会有 IPV4,对吗?仅使用 IPV4 可能是安全的。

+ wlp61s0 IPv6 Canon MG5700 series                           _uscan._tcp          local
+ wlp61s0 IPv4 Canon MG5700 series                           _uscan._tcp          local

= wlp61s0 IPv6 Canon MG5700 series                           _uscan._tcp          local
hostname = [ED122D000000.local]
address = [192.168.0.252]
port = [80]
txt = ["duplex=F" "is=platen" "cs=grayscale,color" "rs=eSCL" "representation=http://ED122D000000.local./icon/printer_icon.png" "vers=2.5" "UUID=00000000-0000-1000-8000-00BBC1ED122D" "adminurl=http://ED122D000000.local./index.html?page=PAGE_AAP" "note=Rich's office" "pdl=image/jpeg,application/pdf" "ty=Canon MG5700 series" "txtvers=1"]
= wlp61s0 IPv4 Canon MG5700 series                           _uscan._tcp          local
hostname = [ED122D000000.local]
address = [192.168.0.252]
port = [80]
txt = ["duplex=F" "is=platen" "cs=grayscale,color" "rs=eSCL" "representation=http://ED122D000000.local./icon/printer_icon.png" "vers=2.5" "UUID=00000000-0000-1000-8000-00BBC1ED122D" "adminurl=http://ED122D000000.local./index.html?page=PAGE_AAP" "note=Rich's office" "pdl=image/jpeg,application/pdf" "ty=Canon MG5700 series" "txtvers=1"]

编辑-------------------------------- @Martn Zetler 是的,它适用于最初发布的字符串,但不适用于这个字符串。同样正如另一张海报所评论的那样,顺序确实发生了变化,因此使用没有描述符的可解析格式是不可靠的。

+   eth0 IPv6 HP LaserJet MFP M130nw (Bonjour)              _uscan._tcp          local
+   eth0 IPv4 HP LaserJet MFP M130nw (Bonjour)              _uscan._tcp          local
=   eth0 IPv6 HP LaserJet MFP M130nw (Bonjour)              _uscan._tcp          local    
hostname = [NPIA9BA52.local]
address = [192.168.1.2]
port = [8080]
txt = ["note=Vallarta" "duplex=F" "is=platen" "cs=color,grayscale" "pdl=application/pdf,image/jpeg" "uuid=564e4234-4430-3737-3739-c8d3ffa9ba52" "rs=eSCL" "representation=http://NPIA9BA52.local/ipp/images/printer.png" "vers=2.5" "usb_MDL=HP LaserJet MFP M129-M134" "usb_MFG=HP" "mdl=LaserJet MFP M129-M134" "mfg=HP" "ty=HP LaserJet MFP M129-M134" "adminurl=http://NPIA9BA52.local." "txtvers=1"]
=   eth0 IPv4 HP LaserJet MFP M130nw (Bonjour)              _uscan._tcp          local
hostname = [NPIA9BA52.local]
address = [192.168.1.2]
port = [8080]
txt = ["note=Vallarta" "duplex=F" "is=platen" "cs=color,grayscale" "pdl=application/pdf,image/jpeg" "uuid=564e4234-4430-3737-3739-c8d3ffa9ba52" "rs=eSCL" "representation=http://NPIA9BA52.local/ipp/images/printer.png" "vers=2.5" "usb_MDL=HP LaserJet MFP M129-M134" "usb_MFG=HP" "mdl=LaserJet MFP M129-M134" "mfg=HP" "ty=HP LaserJet MFP M129-M134" "adminurl=http://NPIA9BA52.local." "txtvers=1"]
4

1 回答 1

0

像这样的正则表达式模式应该可以作为千篇一律的工具:

$subject = ...                           
$pattern = '^=\s\w+\sIPv4\s(.*)\s+_uscan\._tcp\s+\w+\nhostname\s=\s\[(.*)\]\naddress\s=\s\[(.*)\]\nport\s=\s\[(.*)\]\ntxt\s=\s\[(.*)\]\n$';
if (preg_match($pattern, $subject, $matches)) {
    die('<pre>'.print_r($matches, true).'</pre>');
}

/etc/avahi/avahi-daemon.conf您可能会禁用 IPv6 与use-ipv6=no.

于 2019-05-09T03:42:22.160 回答