我需要解析avahi浏览结果以获取PHP中的某些信息。我并不是基于“可解析的” Avahi浏览器(-p),因为这似乎比不可解析的解析更难解析,除非我弄错了。
由于我可以将浏览限制为某些类型,因此我认为只需要从字符串中绘制IP:port和设备名称。首选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"]
答案 0 :(得分:0)
类似的正则表达式模式应该可以用作Cookie切割器:
$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
中,您可以使用use-ipv6=no
禁用IPv6。