Php 解析avahi浏览结果

Php 解析avahi浏览结果,php,avahi,Php,Avahi,我需要在PHP中解析avahi浏览结果以获取一些信息。我不是基于“可解析的”Avahi浏览(-p),因为这似乎比不可解析的更难解析,除非我弄错了 由于我可以将浏览限制为某些类型,我认为我只需要从字符串中提取IP:port和设备名。首选IPV4。这个例子没有IPV6,但有些搜索有,但我认为总会有IPV4,对吗?仅使用IPV4可能是安全的 + wlp61s0 IPv6 Canon MG5700 series _uscan._tcp

我需要在PHP中解析avahi浏览结果以获取一些信息。我不是基于“可解析的”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是的,在最初发布的字符串中有效,但在这个字符串中无效。另外,正如另一张海报所评论的那样,顺序确实发生了变化,因此使用没有描述符的parsable格式是不可靠的

+   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"]

与此类似的正则表达式模式应该起到cookie cuter的作用:

$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>');
}
$subject=。。。
$pattern='^=\s\w+\sIPv4\s(.*)\s+\u uscan\.\u tcp\s+\w+\nhostname\s=\s\[(.*)\]\naddress\s=\s\[(.*)\]\nport\s=\s\[(.*)\]\ntxt\s=\s\[(.*)\]\n$;
if(预匹配($pattern,$subject,$matches)){
模具(''.print_r($匹配,正确)。'';
}

/etc/avahi/avahi daemon.conf
中,您可以使用
使用-IPv6=no
禁用IPv6

$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>');
}
$subject=。。。
$pattern='^=\s\w+\sIPv4\s(.*)\s+\u uscan\.\u tcp\s+\w+\nhostname\s=\s\[(.*)\]\naddress\s=\s\[(.*)\]\nport\s=\s\[(.*)\]\ntxt\s=\s\[(.*)\]\n$;
if(预匹配($pattern,$subject,$matches)){
模具(''.print_r($匹配,正确)。'';
}

/etc/avahi/avahi daemon.conf
中,您可以使用
use-IPv6=no

禁用IPv6,您的假设是错误的。读取CSV要简单得多。读取CSV假设CSV在不同设备之间是一致的,如果顺序发生变化,则可能会出现问题。CSV版本中没有许多名称标题。你的假设是错误的。读取CSV要简单得多。读取CSV假设CSV在不同设备之间是一致的,如果顺序发生变化,则可能会出现问题。CSV版本中没有许多名称标题。谢谢。我担心的是,由于CSV版本中不存在名称,如果设备之间的顺序发生变化,可能会导致代码失败,但对此解决方案感到满意,但不得不在末尾添加一个“^”,谢谢。现在正在使用txt stringZetler的功能,请参阅op中添加的strng,它将不会解析。谢谢。我担心的是,由于CSV版本中不存在名称,如果设备之间的顺序发生变化,可能会导致代码失败,但对此解决方案感到满意,但不得不在末尾添加一个“^”,谢谢。现在正在使用txt stringZetler的功能,请参阅op中添加的strng,它将不会解析