I'm trying to be clever and create a form to create a VM in ESX.
I have found the PowerCLI add-in for Poweshell and i have manually managed to create one.
This is my code but I'm struggling to make it work.
{
// Get the variables submitted by POST in order to pass them to the PowerShell script:
$name = $_POST["name"];
$diskmb = $_POST["diskmb"];
$MemoryMB = $_POST["MemoryMB"];
$NumCPU = $_POST["NumCPU"];
$connectesx = "Connect-VIServer -Server IPADDRESS -Protocol https -User 'USERNAME' -password 'PASSWORD'";
$createvm = "New-VM -Name $name -DiskMB $diskmb -MemoryMB $MemoryMB -NumCPU $NumCPU";
$psScriptPath = "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\esxdhb.psc1";
// Execute the PowerShell script, passing the parameters:
shell_exec("powershell -psc $psScriptPath -command $connectesx && $createvm");
echo ("$name Created Successfully.");
Related
I'm trying to pass arguments from a PHP page's POST request into a powershell script.
This is the relevant PHP snippet:
$selectedPartner = $_POST['partner'];
$selectedGroup = $_POST['group'];
$script = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe AddRemPartners";
if (isset($_POST['partner']) && isset($_POST['group'])){
if (isset($_POST['AddButton']) && $selectedPartner !== "Select Partner" && $selectedGroup !== "Select Group") {
echo "<br>";
echo "Adding " . $selectedPartner . " to " . $selectedGroup . "...<br>";
$cmd = $script . " -Add $selectedPartner $selectedGroup";
echo "command is:<br>" . $cmd;
shell_exec($cmd);
//shell_exec('C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe C:\\xampp\\htdocs\\admin\\AddRemPartners.ps1 -Add $selectedPartner //$selectedGroup');
//$command = shell_exec('C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe telnet 10.11.14.32 4444');
//echo "User added successfully!";
}
if (isset($_POST['RemoveButton']) && $selectedPartner !== "Select Partner" && $selectedGroup !== "Select Group") {
echo "<br>";
//echo "selection was REMOVE";
}
}
And this is my powershell script:
Param([switch]$Add, [switch]$Remove, [string]$User, [string]$Group)
$secpasswd = ConvertTo-SecureString "P#sSw0rd" -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ("userPortal", $secpasswd)
$server = "host.fqdn"
function AddRemPartners
{
if ($Add){
Write-Host "Add var = $Add"
Write-Host "Add was selected"
Add-ADGroupMember -Server $server -Credential $creds -Identity $Group -Member $User
}
if ($Remove) {
Write-Host "Remove var = $Remove"
Write-Host "Remove was selected"
Remove-ADGroupMember -Server $server -Credential $creds -Identity "$Group" -Member "$User" -Confirm:$false
}
}
AddRemPartners -Add $Add -Remove $Remove -User $User -Group $Group
Things I know:
The php post parameters are good. I captured the request in burpsuit and know that all the correct args are getting sent
The resultant ps query that is built is also good. I output it to the screen and it looks like this:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe AddRemPartners -Add Dude1 Dude1Group
No network issues. If I run the above command directly from within PowerShell is executes correctly.
I've sourced my script using . .\AddRemPartners.ps1 so I can call my function directly as such:
PS > AddRemPartners -Add User Group
PS > AddRemPartners -Remove User Group
I've narrowed it down to this: I can't execute my ps script from cmd like this:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe AddRemPartners -Add User Group
As that errors out with this:
AddRemPartners : The term 'AddRemPartners' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and try again.
I believe this to be suspect/culpable since I need run that in PHP. So PHP is probably running into the same issue. So why won't cmd recognize my script if I call powershell at the same time as execution?
Any tips and guidance greatly appreciated. I'm at wits end unfortunately. :/
First parameter for PS should be full path of script. Then you should respect parameter definition of PS, on command line as in the script.
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\whereis\AddRemPartners.ps1 -Add -user User -group Group
First line of the script should be parameter definition and defaults.
param([string]$user, [string]$group, [switch]$Add, ...);
I am trying to login and push Docker images from a PHP script as part of our CICD process. Here is the code:
<?php
include '../php/database.php';
$duser = 'username';
$dpass = 'password';
$dmail = 'email';
$tag = 'from system';
function tagImage($tag) {
$getImageID = "SELECT `imageID` FROM `docker_images` WHERE `tag` = :tag ";
$params = array(':tag' => $tag);
$results = dataQuery($getImageID, $params);
if(!empty($results)) {
$image = $results[0]['imageID'];
global $repo = $results[0]['repo']; // I know this is a bad idea, will change it when all else is working
$last = system("sudo docker tag -f $image $repo 2>&1", $retval);
}
return $retval;
}
$tagStatus = tagImage($tag);
if(0 == $tagStatus) {
echo '<pre>';
$login = system("sudo docker login --username=$duser", $retval);
var_dump($login);
var_dump($retval);
// push it real good
$last = system("sudo docker push $repo 2>&1", $retval1);
var_dump($last);
var_dump($retval1);
}
?>
This returns the following:
string(0) ""
int(1)
The push refers to a repository [app/ap-name] (len: 1)
21d623eb89a9: Image push failed
Please login prior to push:
Username: EOF
string(13) "Username: EOF"
int(1)
The push is failing because the login is not working from the PHP script, however, when I login from the command line the login is successful.
What am I doing wrong? Can I login to Docker Hub with PHP like this? Or should the technique be different?
EDIT: The PHP script will be called via AJAX, effectively making it run as if it were being run from the browser. I am running it from the browser for testing purposes.
The login requires all of the credentials including password and email address associated with the Docker hub repo:
$login = system("sudo docker login --username $duser --password $dpass --email $dmail 2>&1", $retval);
var_dump($login);
var_dump($retval);
Using this syntax returns the following (expected):
WARNING: login credentials saved in /root/.dockercfg.
Login Succeeded
string(15) "Login Succeeded"
int(0)
Past that point the push works properly and returns no errors.
I am making a website for a device so that the user can change the primary and secondary IP that it connects to. I am using 'exec()' to get the IP's for me. i made a function for that called 'File_read' that work great. the problem I am having is with saving it back to the same file. i still use 'exec()', the function is called 'file_write'. Below is my code for the two
function file_read()
{
$arr_out = "";
$arr_test_1 = "";
$arr_test_2 = "";
exec(#'cat /usr/triton/config/protman.conf | grep "Primary ="',$arr_test_1);
exec(#'cat /usr/triton/config/protman.conf | grep "Secondary ="',$arr_test_2);
$arr_out[0] = IP_edit(end($arr_test_1));
$arr_out[1] = IP_edit(end($arr_test_2));
$arr_out[2] = end($arr_test_1);
$arr_out[3] = end($arr_test_2);
return $arr_out;
}
function file_write($arr_in)
{
$save_1 = "";
$save_2 = "";
$build_1 = "";
$build_2 = "";
$build_1 = 'Primary = "'.$arr_in[0].'";';
$build_2 = 'Secondary = "'.$arr_in[1].'";';
$save_1 = #"sed -i 's/".$arr_in[2]."/".$build_1."/' /usr/triton/config/protman.conf";
$save_2 = #"sed -i 's/".$arr_in[3]."/".$build_2."/' /usr/triton/config/protman.conf";
echo "<br/>test save 1 = ".$save_1;
echo "<br/>test save 2 = ".$save_2;
exec($save_1);
exec($save_2);
}
i tested the string it builds to write to the file but and it is as follows.
sed -i 's/Primary = "10.200.26.144";/Primary = "10.98.227.30";/' /usr/triton/config/protman.conf
This is for '$save_1'. When i use it on putty it works so why does it not work with 'exec()'? am i missing something or is their a easier way to do this?
has PHP the rights to execute this command and access to this file ?
Also, you could try giving a second argument $out (as array) to the exec function to handle details/info
I am using exec function to run my php file in background from another like below
<?php
$username = 'Test';
exec(PHP_BINDIR."/php /opt/lampp/htdocs/myscript/test.php >/dev/null &" );
?>
i want to send a value to the file which runs in background.
i tried below code
<?php
$username = 'Test';
exec(PHP_BINDIR."/php /opt/lampp/htdocs/myscript/test.php?user=".$username." >/dev/null &" );
?>
Test.php
<?php
var_dump($_REQUEST);
?>
but i got null as the value. can any one help me. how i can pass a value to a file which is running in background.
Pass it as command line argument:
$command = sprintf('%s/php /opt/lampp/htdocs/myscript/test.php %s >/dev/null &',
PHP_BINDIR,
escapeshellarg($username));
exec($command);
In the file:
$username = $argv[1];
You can't use URL-style query parameters because you're not using a URL, you're calling an executable.
I'm scheduling dynamic cron jobs based on the contents of an Excel file with php, and passing those values to a script in argv. My code is as follows:
$data = new SpreadSheet_Excel_Reader("sample-data.xls", false);
$surveyToken = "Token";
$email = rawurlencode($data->val(2, "B"));
$dateCompleted = rawurlencode($data->val(2, "C"));
$followUpOne = rawurlencode($data->val(2, "E"));
$followUpTwo = rawurlencode($data->val(2, "F"));
$programName = rawurlencode($data->val(2, "G"));
$subject = rawurlencode($data->val(2, "H"));
$firstName = rawurlencode($data->val(2, "I"));
$lastName = rawurlencode($data->val(2, "J"));
$learningGoal = rawurlencode($data->val(2, "K"));
$importanceScore = rawurlencode($data->val(2, "L"));
$confidenceScore = rawurlencode($data->val(2, "M"));
$strategies = rawurlencode($data->val(2, "N"));
$timeLine = rawurlencode($data->val(2, "O"));
$measures = rawurlencode($data->val(2, "P"));
$resources = rawurlencode($data->val(2, "Q"));
$output = shell_exec("crontab -l");
file_put_contents("crontab.txt", $output . "39 * * * * /usr/local/php53/bin/php mail.php $surveyToken $email $dateCompleted $followUpOne $followUpTwo $programName $subject $firstName $lastName $learningGoal $importanceScore $confidenceScore $strategies $timeLine $measures $resources" . PHP_EOL);
exec("crontab crontab.txt");
I'm using a raw URL decode in the mail.php script and sending that email by getting the variables from argv.
I have tested this script by simply executing it on the command line with the outputted "url" and its arguments, not scheduling a cron job for it.
When I execute it like that, it works fine. When I run the code above, the cron job works well and the php file still emails me, but all the variables are empty from argv.
When you send arguments to the command line / shell_exec, you should not use rawurlencode() as it is not a url.
Instead, you should use escapeshellarg(); this escapes the variables correctly to be used in shell_exec.
Apart from that I would try to avoid dynamic scheduling like this; I would just run a cron-job every hour that reads the Excel file and includes the mail file. You could use a database of something similar if you want to generate a job queue.
One thing that does occur to me is that you should check that you have register_argc_argv set in your php.ini or using ini_set().