温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php管理虚拟机,通过代理连接

发布时间:2020-06-24 10:04:13 来源:网络 阅读:428 作者:巴啦 栏目:系统运维

1、        进入安装目录

Cd C:\Program Files\Oracle\VirtualBox

设置web认证库为null:

VboxManagesetproperty websrvauthlibrary null

然后开启服务

 vboxwebsrv --host 0.0.0.0

2、在浏览器输入http://127.0.0.1:18083

在博客下方附件,提供vbox接口文件

写代码实现时,vbox_oper操作帮助类

include_once('vboxServiceWrappers.php');
class Vbox_oper
{
   protected $serverIp;
   protected $serverPort = "18083";
   protected $proxy_uname;
   protected $proxy_password;
   protected $proxy_host;
   protected $proxy_port;
   protected $item;
   protected $connection;
   /*
    * 模块名称:得到连接
    * 参数说明:
    * 作者:csl
   */
   public function getConn()
   {
       $url = "http://" . $this->serverIp . ":" . $this->serverPort . "/";
       //$this->connection = new SoapClient("vboxwebService.wsdl", array('location' => $url, 'connection_timeout' => 5,));
       $this->connection = new SoapClient("vboxwebService.wsdl", array('location' => $url,'connection_timeout'=>5,
           'proxy_host' => $this->proxy_host, 'proxy_port' => $this->proxy_port,
           'proxy_login' => $this->proxy_uname, 'proxy_password' => $this->proxy_password));
       return $this->connection;
   }
   /*
    * 模块名称:初始化代理信息
    * 参数说明:
    * init_proxy 包含:ip,port,username,password
    * 作者:csl
    */
   public function init_proxy($proxy){
       if(!empty($proxy['ip']))
           $this->proxy_host = $proxy['ip'];
       if(!empty($proxy['port']))
           $this->proxy_port = (int)$proxy['port'];
       if(!empty($proxy['username']))
           $this->proxy_uname = $proxy['username'];
       if(!empty($proxy['password']))
           $this->proxy_password = $proxy['password'];
   }
   /*
    * 模块名称:初始化服务器信息
    * 参数说明:
    * init_proxy 包含:server_ip,sbmc
    * 作者:csl
    */
   public function init_data($item, $proxy = array())
   {
       $this->serverPort = "18083";
       if(!empty($item['server_ip']))
           $this->serverIp = $item['server_ip'];
       if(!empty( $item['sbmc']))
           $this->name = $item['sbmc'];
       if (!empty($proxy)) {
           $this->init_proxy($proxy);
       }
   }
   /*
    * 模块名称:启动虚拟机
    * 参数说明:
    * 作者:csl
    */
   public function start()
   {
       $this->getConn();
       $websessionManager = new IWebsessionManager($this->connection);
       $virtualbox = $websessionManager->logon("", "");
       $machine = $virtualbox->findMachine($this->name);
       $session = $websessionManager->getSessionObject($virtualbox->handle);
       $state = (string)$machine->state;
       if ($state != 'Running' && $state != 'Paused') {
           $progress = $machine->launchVMProcess($session->handle, "headless", "");
       }
   }
   /*
       * 模块名称:关闭虚拟机
       * 参数说明:
       * 作者:csl
       */
   public function stop()
   {
       $this->getConn();
       $websessionManager = new IWebsessionManager($this->connection);
       $virtualbox = $websessionManager->logon("", "");
       $machine = $virtualbox->findMachine($this->name);
       $session = $websessionManager->getSessionObject($virtualbox->handle);
       $state = (string)$machine->state;
       if ($state == 'Running' || $state == 'Paused') {
           $lockType = new LockType($session, 'Shared');
           $machine->lockMachine($session, $lockType->NameMap[1]);
           $iconsole = $session->getConsole();
           $progress = $iconsole->powerDown();
       }
   }
   /*
       * 模块名称:重启虚拟机
       * 参数说明:
       * 作者:csl
       */
   public function restart()
   {
       $this->getConn();
       $websessionManager = new IWebsessionManager($this->connection);
       $virtualbox = $websessionManager->logon("", "");
       $machine = $virtualbox->findMachine($this->name);
       $session = $websessionManager->getSessionObject($virtualbox->handle);
       $state = (string)$machine->state;
       if ($state == 'Running' || $state == 'Paused') {
           $lockType = new LockType($session, 'Shared');
           $machine->lockMachine($session, $lockType->NameMap[1]);
           $iconsole = $session->getConsole();
           $progress = $iconsole->reset();
       } else if ($state == 'PoweredOff') {
           $progress = $machine->launchVMProcess($session->handle, "headless", "");
       }
   }
   /*
   * 模块名称:获取vbox里面所有虚拟机
   * 参数说明:
   * 作者:csl
   */
   public function get_machines(){
       $this->getConn();
       $websessionManager = new IWebsessionManager($this->connection);
       $virtualbox = $websessionManager->logon("", "");
       $machines = $virtualbox->machines;
       return $machines;
   }
}



附件:http://down.51cto.com/data/2366921
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI