Devlog

Openbox pipe menu Network Manager

A little script to add your available networks to the Dropbox menu and to let it connect / disconnect.

<?php
$lines = explode("\n", nmcli -m multiline -f name,active,type,autoconnect c show);
$connections = [];
$currentConnection = null;
foreach ($lines as $line) {
  $match = null;
  if (preg_match("@^(NAME|ACTIVE|TYPE|AUTOCONNECT):\s+(.+)$@", $line, $match)) {
    $key = $match[1];
    $value = $match[2];
    if ($key === 'NAME') {
      $connections[$value] = [];
      $currentConnection = $value;
      continue;
    }
    $connections[$currentConnection][$key] = $value;
  }
}
$output = ["<openbox_pipe_menu"];
foreach ($connections as $name => $info) {
  $output[] = "<item label='" . $name . " / " . $info['TYPE']
    . " / " . $info['ACTIVE'] . "'>";
  $cmd = $info['ACTIVE'] === 'yes'
    ? ($info['AUTOCONNECT'] === 'no' ? 'down' : 'disconnect') : 'up';
  $output[] = "<action name='Execute'><execute>nmcli c $cmd id $name</execute></action>";
  $output[] = "</item>";
}
$output[] = "</openbox_pipe_menu>";
die(implode("\n", $output) . "\n");

Now call the script above in your .config/openbox/menu.xml file:

<menu id="mymenu" label="Networks" execute="php /path/to/myfile.php"/>