pfssh: how to add (initial) VLANs?
-
Hi,
as it's a tedious task we do multiple times on various machines, I opted to write a little playback for pfssh.php to add a VLAN. Problem is, if there's no VLAN created on a particular machine, the config.xml doesn't have the <vlan> array inside the <vlans> and I can't seem to add an (initial) new VLAN with the "normal" way that works for adding VLANs afterwards:
$newvlan = array( 'if' => "igbX", 'tag' => 123, 'vlanif' => "igbX.123", 'descr' => "dummy123", ); $config['vlans']['vlan'][] = $newvlan; write_config(); exec
That works without problems, if there's already a $config[vlans][vlan][0] / a first VLAN in existence. But I can't seem to create the initial one as everything I tried (to make the "vlan" appear), seems to fail:
Fatal error: Uncaught Error: Cannot use string offset as an array in /usr/local/sbin/pfSsh.php(371) : eval()'d code:11 Stack trace: #0 /usr/local/sbin/pfSsh.php(371): eval() #1 /usr/local/sbin/pfSsh.php(271): playback_text('$newvlan = arra...') #2 {main} thrown in /usr/local/sbin/pfSsh.php(371) : eval()'d code on line 11 PHP ERROR: Type: 1, File: /usr/local/sbin/pfSsh.php(371) : eval()'d code, Line: 11, Message: Uncaught Error: Cannot use string offset as an array in /usr/local/sbin/pfSsh.php(371) : eval()'d code:11 Stack trace: #0 /usr/local/sbin/pfSsh.php(371): eval() #1 /usr/local/sbin/pfSsh.php(271): playback_text('$newvlan = arra...') #2 {main}
Neither
$config['vlans']['vlan'][0] = $newvlan;
or$config['vlans']['vlan'][] = $newvlan;
or manually trying to add thevlan
node manually in the array seem to work. Something silly I'm obviously overlooking but I'd be happy for anyone to chime in ^^Greets
Jens -
You have to declare each level of the hash/array as you go. Like this:
$config['vlans'] = array(); $config['vlans']['vlan'] = array(); $config['vlans']['vlan'][] = $newvlan;
There is a convenience function that can help:
init_config_arr(array('vlans', 'vlan')); $config['vlans']['vlan'][] = $newvlan;
That function only initializes the arrays if they are unset/don't exist/are not arrays so it's safe to run unconditionally.