<?php
require_once('Net/Socket.php');
require_once('libs/class.debug.php');
require_once('engines/abstract.class.base_switch_engine.php');
require_once('engines/interface.class.engine_interface.php');
class powerconnect extends base_switch_engine implements engine_interface {
/*
base_switch_engine methods.
*/
public function __construct($options) {
$this->options = $options;
debug::output("Initiallizing engine",$this->debug,true);
}
public function connect() {
//Because I'm too lazy to change the variables.
$options = $this->options;
$options['switch'] = (!isset($options['switch'])) ? 'localhost' : $options['switch'] ;
$options['port'] = (!isset($options['port'])) ? 23 : $options['port'] ;
$options['username'] = (!isset($options['username'])) ? 'admin' : $options['username'] ;
$options['password'] = (!isset($options['password'])) ? '' : $options['password'] ;
$this->socket = new Net_Socket();
@debug::output("Connecting: {$options['switch']}:{$options['port']}",$this->debug,true);
$this->socket->connect($options['switch'],$options['port'],null,2);
return;
}
public function disconnect() {
$options = $this->options;
@debug::output("Disconnecting: {$options['switch']}:{$options['port']}",$this->debug,true);
$this->socket->disconnect();
return;
}
public function logon() {
debug::output("Sending: username",$this->debug,true);
$this->send($this->options['username']);
$this->read("Password:");
debug::output("Sending: password",$this->debug,true);
$this->send($this->options['password']);
$this->read();
debug::output("End Login",$this->debug,true);
return;
}
public function send($cmd) {
debug::output("Sending: ".$cmd,$this->debug,true);
$this->socket->write($cmd.chr(13));
return;
}
public function read($escape = '#',$a = '0') {
debug::output("Waiting for response...",$this->debug,true);
$line = '';
$el = strlen($escape);
$prompt = "--More-- or (q)uit";
//while(($res = substr(($line.=$this->socket->read(1)), (strlen($line)-1),1))!= "#") {
while(($res = substr(($line.=($err=$this->socket->read(1))), (strlen($line)-$el)))!= $escape) {
//if($a == 'res') { echo __CLASS__.'::'.print_r($line,1)."\r\n"; }
//echo $line."\r\n";
//As far as I know, Dell Powerconnect doesn't have a "terminal length 0", we need to create something that mimics it.
if(substr($line,strlen($line)-strlen($prompt)) == $prompt) {
$this->send("\r");
}
}
debug::output("Data received...processing...",$this->debug,true);
//Since the beginning and the end are meaningless junk lets kill 'em now.
$t = explode("\r\n",$line);
array_shift($t); //first
array_pop($t); //last
//The length 0 mimic leaves some artifacts, so lets get rid of them.
$nt = array();
foreach($t as $k => $v) {
if(preg_match('/or \(q\)uit/si',$v)) { continue; }
if(strlen($v) == 0) { continue; }
$nt[] = $v;
}
return $nt;
}
/*
engine_interface methods.
*/
public function _getMacPortTable() {
$this->send('show bridge address-table');
$list = $this->read();
$macs = array();
foreach($list as $k => $v) {
preg_match_all('/([0-9]{1,})( {2,})([a-fA-F0-9\.]{14})( {2,})([^ ]{1,})( {2,})/si',$v,$mat);
@$m = strtolower(str_replace('.','',$mat[3][0]));
if(@$mat[1][0] === null && strlen($m) == 0 && @$mat[5][0] === null) { continue; }
$macs[] = array('vlan' => $mat[1][0], 'mac' => $m , 'interface' => $mat[5][0]);
}
return $macs;
}
public function _getArpTable() {
$this->send('show arp switch');
$list = $this->read();
$arp = array();
foreach($list as $k => $v) {
preg_match_all('/([a-fA-F0-9\.]{14})( {2,})([0-9\.]{7,15})( {2,})([^ ]{1,})/si',$v,$mat);
@$m = strtolower(str_replace('.','',$mat[1][0]));
if(strlen($m) == 0 && @$mat[3][0] === null && @$mat[5][0] === null) { continue; }
$arp[] = array('mac' => $m, 'ip' => $mat[3][0], 'interface' => $mat[5][0]);
}
return $arp;
}
public function _getInterfaceSettings() {
$this->send('show interfaces status ');
$list = $this->read('Ch Type Link');
/*
Port Type Duplex Speed Neg Mode MDIX Link State
1/g7 Gigabit - Level Full 1000 Auto Auto Up
1/g8 Gigabit - Level N/A Unknown Auto Auto Down
ch18 Link Aggregate Down
1/xg1 10G - Level N/A Unknown Auto Auto Down
1/xg2 10G - Level N/A Unknown Auto Auto Down
*/
$rtn = array();
//$cur = array('interface' =>null,'mode'=>null, 'vlan' => null);
for($i = 0; $i < count($list); $i++) {
$line = $list[$i];
$cur = array('interface' =>null,'mode'=>null, 'vlan' => null);
if(preg_match('/([0-9]\/[a-z0-9]{2,}) {2,}([^ ]* - [^ ]*) {2,}([^ ]*) {2,}([^ ]*) {2,}([^ ]*) {2,}([^ ]*) {2,}([^ ]*)/si', $line, $mat)) {
$rtn[] = array('interface' => $mat[1], 'mode' => null, 'vlan' => null);
}
}
return $rtn;
}
}
?>