我正在编写一个脚本,为您提供有关某些思科设备的信息。
class COMMAND
{
...
private $intbrief;
private $core;
private $allcommand;
...
public function __construct()
{
require_once 'class.php';
$this->core = new CLASS();
$this->allcommand = array(
array( "id" => "brief",
"def" => "show ip interface brief | exclude un",
"vrf" => false,
"val" => "",
),
array( "id" => "vrf",
"def" => "show ip interface ".$this->intbrief." | include VPN Routing/Forwarding",
"vrf" => false,
"val" => "",
));
}
...
private function validip($ip)
{
// return true or false
}
public function go($_ip, $_user, $_pass )
{
if($this->validip($_ip))
{
$this->ip = $_ip;
$this->user = $_user;
$this->pass = $_pass;
return $this->logic();
} else {
echo "Invalid Options\n";
exit(1);
}
}
private function logic()
{
foreach ($this->allcommand as $i => &$row)
{
if(method_exists($this, $row['id']))
{
if(!empty($this->intbrief))
{
echo "\n\n\n\t".$this->intbrief."---TRUE\n\n\n";
}
if(isset($this->vrf) and $row['vrf'] == true)
{
$command = $row['def']." vrf ".$this->vrf;
} else {
$command = $row['def'];
}
// Send commands to telnet class
$output = $this->core->GetOutputOf($this->ip, $this->user, $this->pass, $command);
// Send to parse functions
$row['val'] = $this->$row['id']($output);
} else {
echo "Check functions from allcommand array\n";
exit(1);
}
}
return $this->allcommand;
}
private function brief($match)
{
// parsing output ...
$this->intbrief = $output; // e.g. $this->intbrief = "FastEthernet0/1";
}
private function vrf($match)
{
// parsing output ...
$this->vrf = $output;
}
}
$com = new COMMAND();
$f = $com->go($ip, $user, $password);
Loop工作得很好,但是......
数组vrf def是show ip interface | include VPN Routing/Forwarding
- 没有$this->intbrief
,因为它是null ....但为什么呢?我在循环中检查了它......
if(!empty($this->intbrief))
{
echo "\n\n\n\t".$this->intbrief."---TRUE\n\n\n";
}
......而且是真的。那么错误在哪里?
我想执行类似show ip interface FastEthernet0/1 | include VPN ...
我将从私人功能简介中获取“FastEthernet0 / 1”信息
感谢您的帮助
cdpb
答案 0 :(得分:0)
我再次重新思考问题并进行了一些测试,我认为问题在于数组的创建。当数组创建时,将解析数组值中的变量,然后在此数组实例中修复这些值。所以我认为在构造或任何其他函数中放置$ this-> allcommand并不重要。为了解决这个问题而不是过多地改变你的类的结构,我建议在将数组allcommand
声明为
$this->allcommand = array(
array( "id" => "brief",
"def" => "show ip interface brief | exclude un",
"vrf" => false,
"val" => "",
),
array( "id" => "vrf",
"def" => "show ip interface <inf> | include VPN Routing/Forwarding",
"vrf" => false,
"val" => "",
));
然后,您可以在功能逻辑中,在$command = $row['def'];
之后执行preg_replace
或str_replace
转换<inf>
,如果$this->intbrief
的值为{{1}} 。可能不是最佳解决方案,但我可以出来。
答案 1 :(得分:0)
好主意!
我还测试了一下,在一个单独的函数中构建类似while循环的东西,它一次只能给出一部分数组。背后的想法是,在循环的每一轮中,内部的变量将再次设置。
看起来像:
....
private function arraycommand()
{
static $i = 1;
$allcommand = array(
array( "id" => "brief",
"def" => "show ip interface brief | exclude un",
"vrf" => false,
),
array( "id" => "vrf",
"def" => "show ip interface ".$this->intbrief." | include VPN Routing/Forwarding",
"vrf" => false,
),
);
while(count($allcommand) >= $i)
{
$t = $i -1;
$i++;
return $allcommand[$t];
}
return false;
}
private function logic()
{
while(true)
{
$allcommand = array($this->arraycommand());
if(!$allcommand[0] == false)
{
foreach ($allcommand as $i => &$row)
{
...
}
} else break;
}
return output
}
....
它的编码不是很好,但它有效:)
感谢您的帮助!!!