Page 1 of 1

Help converting this Powershell Script to PHP

Posted: Mon Feb 02, 2015 5:25 am
by john2k
Hi Guys,

I've got the following PowerShell code that logs into my Xerox Copier's web GUI and then it can send a reboot command to it. I am trying to achieve the exact same thing but with PHP. I've put what I have tried with PHP so far but doesnt seem to be working. Maybe I am missing something and hoping someone can help me.

Any help would be much appreciated. Many thanks in advance.

Here is the PowerShell code that works:

[syntax]

$CookieContainer = New-Object System.Net.CookieContainer

function SendGET([string]$url)
{
[net.httpWebRequest] $req = [net.webRequest]::create($url);
$req.Method = "GET";
$req.Accept = "text/html";
$req.CookieContainer = $CookieContainer;
[net.httpWebResponse] $res = $req.getResponse();
$resst = $res.getResponseStream();
$sr = new-object IO.StreamReader($resst);
$result = $sr.ReadToEnd();
return $result;
}

function SendPOST([string]$url, [string]$data)
{
$buffer = [text.encoding]::ascii.getbytes($data)
[net.httpWebRequest] $req = [net.webRequest]::create($url)
$req.method = "POST"
$req.ContentType = "application/x-www-form-urlencoded"
$req.ContentLength = $buffer.length
$req.KeepAlive = $true
$req.CookieContainer = $CookieContainer
$reqst = $req.getRequestStream()
$reqst.write($buffer, 0, $buffer.length)
$reqst.flush()
$reqst.close()
[net.httpWebResponse] $res = $req.getResponse()
$resst = $res.getResponseStream()
$sr = new-object IO.StreamReader($resst)
$result = $sr.ReadToEnd()
$res.close()
return $result;
}

# Request to get session id
$x = SendGET ("http://192.168.1.170/properties/authent ... /login.php");
# Post login to get new session id
$x = SendPOST ("http://192.168.1.170/userpost/xerox.set") ("_fun_function=HTTP_Authenticate_fn&NextPage=%2Fproperties%2Fauthentication%2FluidLogin.php&webUsername=admin&webPassword=password&frmaltDomain=default");

# Post reboot command with loggedin session id
$x = SendPOST ("http://192.168.1.170/userpost/xerox.set") ("_fun_function=HTTP_Machine_Reset_fn&NextPage=/status/rebooted.php");

[/syntax]



And here is the PHP code that doesnt seem to work:

Code: Select all


$xeroxReferer = "http://192.168.1.170";

$postdata = http_build_query(
  array(
    '_fun_function' => 'HTTP_Authenticate_fn',
    'NextPage' => '/properties/authentication/luidLogin.php',
    'webUsername' => 'admin',
    'webPassword' => 'password',
    'frmaltDomain' => 'default'
  )
);

$rebootdata = http_build_query(
  array(
    '_fun_function' => 'HTTP_Machine_Reset_fn',
    'NextPage' => '/status/rebooted.php'
  )
);


$opts = array('http' =>
  array(
    'method' => 'POST',
    'header' => 'Referer: '.$xeroxReferer,
    'content' => $postdata
  )
);

$reboot_opts = array('http' =>
  array(
    'method' => 'POST',
    'header' => 'Referer: '.$xeroxReferer,
    'content' => $rebootdata
  )
);


$context = stream_context_create($opts);
$context_reboot = stream_context_create($reboot_opts);

// Request to get session id
$send = file_get_contents($xeroxReferer.'/properties/authentication/login.php');

// Post login to get new session id
$send = file_get_contents($xeroxReferer.'/userpost/xerox.set', false, $context);

# Post reboot command with loggedin session id
$send = file_get_contents($xeroxReferer.'/userpost/xerox.set', false, $context_reboot);



Re: Help converting this Powershell Script to PHP

Posted: Tue Feb 03, 2015 6:32 pm
by Weirdan
The obvious difference is that your php code doesn't store/send cookies.