Forum Moderators: coopster

Message Too Old, No Replies

Help converting this Powershell Script to PHP

         

john2k

3:36 pm on Feb 2, 2015 (gmt 0)

10+ Year Member



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.

Many thanks in advance.

Here is the PowerShell code that works:



$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/authentication/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");





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



$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);


rainborick

8:31 pm on Feb 3, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You may need to use cURL [php.net] if you want to work with authentication data attached to a particular "user".

john2k

9:13 pm on Feb 7, 2015 (gmt 0)

10+ Year Member



I tried using cURL. My cURL code is below but it also doesn't seem to work. Any idea what's wrong?



function SendGET($url, $params=array()){
$url = $url.'?'.http_build_query($params, '', '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}



function SendPOST($url, $fields){
$post_field_string = http_build_query($fields, '', '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_field_string);
curl_setopt($ch, CURLOPT_POST, true);
$response = curl_exec($ch);
curl_close ($ch);
return $response;
}





$x = SendGET('http://192.168.1.170/properties/authentication/login.php', array('param1'=>'value1'));
$x = SendPOST('http://192.168.1.170/userpost/xerox.set', array('_fun_function'=>'HTTP_Authenticate_fn','NextPage'=>'/properties/authentication/luidLogin.php','webUsername'=>'admin','webPassword'=>'password','frmaltDomain'=>'default'));
$x = SendPOST('http://192.168.1.170/userpost/xerox.set', array('_fun_function'=>'HTTP_Machine_Reset_fn','NextPage'=>'/status/rebooted.php'));



rainborick

3:06 pm on Feb 8, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I suggested cURL because I suspected that this process would require supporting cookies. Without more information about the problem you encountered and the log-in process on the system you're working with, it's impossible to say for sure.