Last active
December 14, 2015 22:29
-
-
Save ryanrhanson/5159035 to your computer and use it in GitHub Desktop.
Retrieve all CCI KVM IPs and ports on an account, along with root password on file (as this should be kvm password)
This file contains hidden or 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 | |
/* | |
/ ccikvm.php - Used to pull KVM credentials for all cloud instances on an account. | |
/ With single_use true, you need to supply the api username and password as the first and second arguments. | |
/ This assumes that you have ccikvm.php in the same directory as the 'SoftLayer' folder in the PHP api client. | |
/ Syntax: php ccikvm.php $apiUsername $apiKey | |
/ | |
/ If single_use is false, this will look for configuration/apiconfig.php, which will need to contain the | |
/ apiUsername, apiKey, and the path to the SoftLayer php client directory. Only useful if you have a lot | |
/ of scripts to work with. | |
*/ | |
$single_use = true; | |
if ($single_use) { | |
if(!isset($_SERVER['argv'][1]) || !isset($_SERVER['argv'][2])){ | |
die("single_use is set, please supply API Username and Key"); | |
} | |
require_once('SoftLayer/SoapClient.class.php'); | |
$apiUsername = $argv[1]; | |
$apiKey = $argv[2]; | |
} | |
else { | |
require_once('configuration/apiconfig.php'); | |
} | |
/* | |
/ Connect to the account service, as we'll be pulling the CCIs from here. | |
/ We'll set an object mask for the info we need out of the SoftLayer_Virtual_Guest data. | |
/ We pull the FQDN, OperatingSystem (for passwords), and consoleIpAddressRecord info in this script. | |
*/ | |
$client = Softlayer_SoapClient::getClient('SoftLayer_Account', null, $apiUsername, $apiKey); | |
$kvmMask = new SoftLayer_ObjectMask(); | |
$kvmMask = 'mask[fullyQualifiedDomainName,operatingSystem[passwords],consoleIpAddressRecord[ipAddress[ipAddress]]]'; | |
$client->setObjectMask($kvmMask); | |
/* | |
/ Loop through all CCIs on the account, checking for consoleIpAddressRecord data, and | |
/ password data. We'll return various error messages if these are not found, otherwise | |
/ we can display the fqdn, kvm ip, port, and password. | |
*/ | |
try { | |
$result = $client->getVirtualGuests(); | |
foreach ($result as $kvm) { | |
//Check to see if consoleIpAddressRecord exists. If not, unconfigured KVM. | |
if (!array_key_exists('consoleIpAddressRecord',$kvm)){ | |
echo $kvm->fullyQualifiedDomainName . " does not have a configured KVM.\n\r"; | |
} | |
//Special case. If no root password is set for the OS, KVM will fail. | |
elseif (!array_key_exists('0',$kvm->operatingSystem->passwords)){ | |
echo "No password found for " . $kvm->fullyQualifiedDomainName . " - KVM will not work until one is set in portal.\n\r"; | |
} | |
else { | |
//Dump the KVM info for the Cloud Instances on the account. | |
echo "KVM Info for " . $kvm->fullyQualifiedDomainName . ": IP:" . $kvm->consoleIpAddressRecord->ipAddress->ipAddress . " Port:" . $kvm->consoleIpAddressRecord->port . " Password:" . $kvm->operatingSystem->passwords[0]->password . "\n\r"; | |
} | |
} | |
} catch ( Exception $e) { | |
die( $e->getMessage()); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment