Created
May 20, 2015 18:37
-
-
Save scr34m/14b10cc3d336c7b4e6ed to your computer and use it in GitHub Desktop.
APC UPS status read over network
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 | |
function htons($i) | |
{ | |
return pack('n', $i); | |
} | |
function nstoh($d) | |
{ | |
$length = unpack('n', $d); | |
return $length[1]; | |
} | |
$fp = fsockopen("127.0.0.1", 3551, $errno, $errstr); | |
if (!$fp) { | |
return; | |
} | |
stream_set_blocking($fp, 1); | |
stream_set_timeout($fp, 5); | |
fwrite($fp, htons(6), 2); | |
fwrite($fp, 'status', 6); | |
$status = ''; | |
while (!feof($fp)) { | |
$length = nstoh(fread($fp, 2)); | |
if ($length == 0) { | |
break; | |
} | |
$status .= fread($fp, $length); | |
} | |
fclose($fp); | |
echo $status; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment