1

I need to parse an avahi browse result for some information in PHP. I am NOT basing this on "parsable" Avahi browse (-p) as this seems more difficult to parse than the unparsable, unless I am mistaken.

As I can limit the browse to certain types , I think that I need only draw the IP:port and device name from the strings. Prefer IPV4. The example does not have an IPV6 but some searches do, but I think there will always be an IPV4, correct? It is probably safe to use IPV4 only.

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

edit-------------------------------- @Martn Zetler yes that works in originally posted string, but not in this one. Also as commenteed to another poster, the order does change, so using the parsable format with no descriptors would be unreliable.

+   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"]
Mark
  • 39
  • 1
  • 6
  • Your assumption is flawed. Reading CSV is way simpler. – mario May 09 '19 at 05:08
  • Reading CSV assumes that the CSV is consistent across devices , If the order is changed then ts a problem. Many of thenames titles are not present in the CSV version. – Mark May 09 '19 at 05:23

1 Answers1

0

A regex pattern alike this should work as cookie cutter:

$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>');
}

in /etc/avahi/avahi-daemon.conf you can possibly disable IPv6 with use-ipv6=no.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • thanks for that . I was concerned that as the names are not present in the CSV version if the order changes from device to device that would make the code fail but happy with this solution but had to add a ^ at the end – Mark May 09 '19 at 15:09
  • thanks . Now working on capabilities from txt string – Mark May 09 '19 at 15:26
  • Zetler see the added strng in op it will not parse – Mark May 11 '19 at 02:45