Skip to content

Instantly share code, notes, and snippets.

@zhsso
Created November 16, 2019 07:04
Show Gist options
  • Save zhsso/c3c8079f7e51ed6bde5d74c9a013202f to your computer and use it in GitHub Desktop.
Save zhsso/c3c8079f7e51ed6bde5d74c9a013202f to your computer and use it in GitHub Desktop.
<?php
//登陆https://console.online.net/en/api/access,那一串最黑的字符串
const API_CODE='4279482ea1d251b8b40fd5ee83c1a';
//自己设置的密码
const USERNAME='123456';
const PASSWORD='123456';
class online{
static $host='https://api.online.net/api/v1';
static $api=[
'user'=>'/user',
'server'=>'/server',
];
public $key;
public function __construct($key)
{
$this->key=$key;
}
function curl($url, $type = 'get', $data = [])
{
$url = $this->getApiUrl($url);
$header = [
"Authorization:Bearer {$this->key}"
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if ($type == 'post') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$output = curl_exec($ch);
curl_close($ch);
$output= json_decode($output);
if($output!=null)
return $output;
else
return false;
}
function getApiUrl($key)
{
return online::$host.$key;
}
/**
* 获取个人信息
* @return bool|mixed
*/
function getUser()
{
$result = $this->curl(online::$api['user']);
if(is_object($result) && !isset($result->error))
return $result;
else
return false;
}
/**
* 获取服务器列表,每台服务器都有详细信息
* @return array|bool
*/
function getServerList()
{
$result = $this->curl(online::$api['server']);
$server_list = $result;
if(empty($server_list))
{
return false;
}
$data=[];
foreach($server_list as $server)
{
$arr = explode('/',$server);
$server_id = $arr[4];
$data[]=$this->getServerInfo($server_id);
}
return $data;
}
/**
* 获取单个服务器的信息
* @param $id
*
* @return bool|mixed
*/
function getServerInfo($id)
{
$result = $this->curl('/server/'.$id);
if(is_object($result) && !isset($result->error))
return $result;
else
return false;
}
/**
* 正常启动/救护启动
* @param $id
*
* @return bool|mixed
*/
function setBoot($id,$mode='normal')
{
$result = $this->curl('/server/boot/'.$mode.'/'.$id);
}
/**
重启
* @param $id
*
* @return bool|mixed
*/
function setReBoot($id)
{
$result = $this->curl('/server/reboot/'.$id);
}
/**
* 关机
* @param $id
*/
function shutdown($id)
{
$result = $this->curl('/server/shutdown/'.$id);
}
}
#业务流程开始
auth();
$obj = new online(API_CODE);
if(isset($_GET) && !empty($_GET))
{
if($_GET['op']=='normal')
{
$id=intval($_GET['id']);
$obj->setBoot($id,'normal');
echo "<font color='blue'>操作已执行,请耐心等待</font>";
}
elseif($_GET['op']=='rescue')
{
$id=intval($_GET['id']);
$obj->setBoot($id,'rescue');
echo "<font color='blue'>操作已执行,请耐心等待</font>";
}
elseif($_GET['op']=='reboot')
{
$id=intval($_GET['id']);
$obj->setReboot($id);
echo "<font color='blue'>已发重启命令</font>";
}
elseif($_GET['op']=='shutdown')
{
$id=intval($_GET['id']);
$obj->shutdown($id);
echo "<font color='blue'>已发关机命令</font>";
}
//@todo:这里加各项操作,重装啥的,自己补充。。
}
$server_list = $obj->getServerList();
$url = 'http://'.$_SERVER["SERVER_NAME"].'/'.$_SERVER["REQUEST_URI"];
foreach($server_list as $server)
{
$link =[
'重启'=>$url.'?op=reboot&id='.$server->id,
'关机'=>$url.'?op=shutdown&id='.$server->id,
'正常模式'=>$url.'?op=normal&id='.$server->id,
'救援模式'=>$url.'?op=rescue&id='.$server->id,
];
echo '<hr>';
echo '<br>机器ID:'.$server->id;
echo '<br>产品规格:'.$server->offer;
echo '<br>主机名:'.$server->hostname;
echo '<br>操作系统:'.$server->os->name.' '.$server->os->version;
echo '<br>电源状态:'.$server->power;
echo '<br>当前模式:'.$server->boot_mode;
echo '<br>滥用报告邮箱:'.$server->abuse;
echo '<br>机器位置:'.$server->location->datacenter.' / '.$server->location->room.' / '.$server->location->rack.' / '.$server->location->block;
echo '<br>网络信息:'.json_encode($server->network->ip);
echo '<p>';
foreach($link as $k=>$v)
{
echo "<a href='{v}'>{$k}</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
}
}
function auth()
{
$AUTH_USER = USERNAME;
$AUTH_PASS = PASSWORD;
header('Cache-Control: no-cache, must-revalidate, max-age=0');
$has_supplied_credentials = !(empty($_SERVER['PHP_AUTH_USER']) && empty($_SERVER['PHP_AUTH_PW']));
$is_not_authenticated = (
!$has_supplied_credentials ||
$_SERVER['PHP_AUTH_USER'] != $AUTH_USER ||
$_SERVER['PHP_AUTH_PW'] != $AUTH_PASS
);
if ($is_not_authenticated) {
header('HTTP/1.1 401 Authorization Required');
header('WWW-Authenticate: Basic realm="Access denied"');
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment