PHP使用ssh获取aruba设备AC上面的数据;主要函数ssh2_connect,ssh2_auth_password,ssh2_shell,fwrite;
由于aruba的ac登录之后需要输入enable;enable;才能操作命令
<?php /** * Created by PhpStorm. * User: xiaoxie * Date: 2018/2/7 * Time: 12:21 **/ namespace App\Http\Controllers; class SshexecController extends Controller { private $username = 'admin'; private $ip = '192.168.1.99'; private $port = 22; private $pwd = 'admin123'; public $connection; public $shell; /** * sshexec constructor.初始化ssh */ public function __construct() { $this->connection = ssh2_connect($this->ip, $this->port); $result = ssh2_auth_password($this->connection, $this->username, $this->pwd); if ($result) { //登陆成功 $this->shell = ssh2_shell($this->connection,"vanilla"); stream_set_blocking($this->shell,true); fwrite($this->shell,"enable\n"); // sleep(1); fwrite($this->shell,"enable\n"); } } /** * Created by PhpStorm. * function: exec * Description:执行命令的函数 * User: Xiaoxie * Email 736214763@qq.com * @param $preg_match 正则匹配的字符串 * @param $cmd 要执行的命令 * @return array * */ public function exec($preg_match,$cmd) { fwrite($this->shell,$cmd."\n"); $arr = array(); while ($buf = fgets($this->shell)) { flush(); $arr[] = $buf; if (strripos($buf, $preg_match) !== false) { break; } } return $arr; } /** * Created by PhpStorm. * function: getEssid * Description: 获取ssid列表 各SSID客户端数量排行 * User: Xiaoxie * Email 736214763@qq.com * @return array * */ public function getEssid() { //接收原始数据 $data = $this->exec('Num ESSID','show ap essid'); $length = count($data); $arr = array(); for ($i = 0;$i<$length;$i++){ if($i<19){ //销毁前面没用的参数 unset($data[$i]); }elseif ($i == $length-1) { unset($data[$i]); } else{ $tmparr = explode(' ',$data[$i]); $tmparrs = []; if ($tmparr) { $tmparrs['ESSID'] = $tmparr[0]; $tmparrs['APs'] = $tmparr[1]; $tmparrs['Clients'] = $tmparr[2]; $tmparrs['VLAN'] = $tmparr[3]; $tmparrs['Encryption'] = $tmparr[4]; $arr[] = $tmparrs; } } } return $arr; } /** * Created by PhpStorm. * function: getUserTable * Description:获取在线用户 客户端系统类型和数量排行 * User: Xiaoxie * Email 736214763@qq.com * @return array * */ public function getUserTable() { $data = $this->exec('User Entries','show user-table'); $length = count($data); $arr = array(); for ($i = 0;$i<$length;$i++){ if($i<19){ //销毁前面没用的参数 unset($data[$i]); }elseif ($i == $length-1) { unset($data[$i]); } else{ $tmparr = explode(' ',$data[$i]); $tmparrs = []; if (count($tmparr)>5) { $tmparrs['IP'] = $tmparr[0]; $tmparrs['MAC'] = $tmparr[1]; $tmparrs['Role'] = $tmparr[7]; $tmparrs['Age'] = $tmparr[9]; $tmparrs['AP name'] = $tmparr[19]; $tmparrs['Roaming'] = $tmparr[20]; $tmparrs['Essid/Bssid/Phy'] = $tmparr[21]; $tmparrs['Profile'] = $tmparr[22]; $tmparrs['Forward mode'] = $tmparr[23]; $tmparrs['Type'] = $tmparr[27]; $arr[] = $tmparrs; } } } $arr = explode(' ',$data[19]); //echo $data[19]; print_r($arr); //return $arr; } /** * 析构函数 */ public function __destruct() { // TODO: Implement __destruct() method. fclose($this->shell); } }