Created
July 7, 2016 09:38
-
-
Save Remz-Jay/c1977750b7d48b85143ad0b64cfccbce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* GET /virtual_machines/:virtual_machine_id/console.xml | |
* GET /virtual_machines/:id.xml | |
* | |
* The first will start the console session | |
* and give you the port number to connect to. | |
* | |
* The second call, you are looking for remote_access_password. | |
*/ | |
if (isset($argv[1])) { | |
$vmid = trim($argv[1]); | |
} else { | |
echo 'What is the hostname/IP/vmid? :'; | |
$handle = fopen('php://stdin', 'r'); | |
$line = fgets($handle); | |
echo "\n"; | |
$vmid = trim($line); | |
} | |
$credentials = parse_ini_file('credentials.ini'); | |
$cpurl = $credentials['cpurl']; | |
$username = $credentials['username']; | |
$password = $credentials['password']; | |
$ch = curl_init('https://' . $cpurl . '/virtual_machines.json'); | |
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
$result = curl_exec($ch); | |
if ($result === FALSE) { | |
die ('Cannot fetch VM list, curl error:' . curl_error($ch)); | |
} | |
$ip = gethostbyname($vmid); | |
$vms = json_decode($result); | |
foreach ($vms as $vm) { | |
$vm = $vm->virtual_machine; | |
if ($vm->hostname == $vmid || $vm->identifier == $vmid) { | |
$vmid = $vm->identifier; | |
break; | |
} else { | |
foreach ($vm->ip_addresses as $ipsource) { | |
$ipsource = $ipsource->ip_address; | |
if ($ipsource->address == $ip) { | |
$vmid = $vm->identifier; | |
break; | |
} | |
} | |
} | |
} | |
if (strpos($vmid, '.') !== FALSE) { | |
die('Invalid vmid (' . $vmid . ') received. Stopping.'); | |
} else { | |
echo 'The vmid = ' . $vmid . PHP_EOL; | |
} | |
curl_setopt($ch, CURLOPT_URL, 'https://' . $cpurl . '/virtual_machines/' . $vmid . '/console.json'); | |
$result = curl_exec($ch); | |
if ($result === FALSE) { | |
die ('Cannot fetch VM console info, curl error:' . curl_error($ch) . PHP_EOL); | |
} | |
$setOne = json_decode($result); | |
curl_setopt($ch, CURLOPT_URL, 'https://' . $cpurl . '/virtual_machines/' . $vmid . '.json'); | |
$result = curl_exec($ch); | |
if ($result === FALSE) { | |
die ('Cannot fetch VM info, curl error:' . curl_error($ch) . PHP_EOL); | |
} | |
$setTwo = json_decode($result); | |
$port = $setOne->remote_access_session->port; | |
echo "PORT:\t" . $setOne->remote_access_session->port . PHP_EOL; | |
echo "REMOTE_KEY:\t" . $setOne->remote_access_session->remote_key . PHP_EOL; | |
$rap = $setTwo->virtual_machine->remote_access_password; | |
echo "REMOTE_ACCESS_PASSWORD:\t" . $rap . PHP_EOL; | |
$un = 'root'; | |
$pw = $setTwo->virtual_machine->initial_root_password; | |
echo "ROOTPASS:\t" . $pw . PHP_EOL; | |
curl_close($ch); | |
$constructedUrl = 'remotix://' . $cpurl . ':' . $port . '/?vncPassword=' . urlencode($rap); | |
echo $constructedUrl . PHP_EOL; | |
shell_exec('open ' . $constructedUrl); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment