Knowledge Base Nr: 00235 telnet.php - http://www.swe-kaiser.de

Downloads:

php: telnetclient - socketverbindung aufbauen und schreiben/lesen

  
<?
header("Pragma: no-cache");
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Thu, 01 Dec 1994 16:00:00 GMT");

if ($read)
$cmd = $rcmd;
elseif ($write && $value)
$cmd = "$wcmd $value";

if ($debug)
{
phpinfo();
}

function spsrequest($server, $port, $timeout, $request)
{
#rückgabeparameter initialisieren und eingabeparameter validieren
$request .= "\r\n";
$ret = "";

if (!is_numeric($port) or !is_numeric($timeout))
{
return "ERROR: spsrequest() falsche parameter\n";
}

$socket = fsockopen($server, $port, $errno, $errstr, $timeout);

socket_set_timeout($socket, 0, $timeout*100*1000); #timeout in mikrosec

#spsserver banner überlesen
while ($line = fgets($socket, 4096))
{
$ret .= $line;
if (feof($socket))
break;
}

#kommando schreiben
fputs($socket, $request);

#antwort lesen
socket_set_timeout($socket, 0, $timeout*100*1000); #timeout in mikrosec
$ret = "";
while ($line = fgets($socket, 4096))
{
$ret .= $line;
if (feof($socket))
break;
}

fclose($socket);

return $ret;
}

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
<title>Telnet zu SPSServer</title>
</head>

<body>

<h1>Telnet zu SPSServer</h1>

<?
echo("<p>Aufruf: server=$server port=$port cmd=$cmd<p>\n");

if ($read || $write)
{
$ret = spsrequest($server, $port, $timeout, $cmd);

echo("<h3>Ausführen</h3><p>Kommando: $cmd<br>Ergebnis:<pre>$ret</pre><p>\n");
}
?>

<h3>Maske:</h3>
<form action="telnet.php?" method="post">
debug: <? echo("<input type=\"edit\" name=\"debug\" value=\"$debug\" size=\"1\">"); ?> &nbsp;&nbsp;
timeout: <? echo("<input type=\"edit\" name=\"timeout\" value=\"$timeout\" size=\"1\">"); ?> <br>

server: <input type="edit" name="server" value="192.168.0.24" size="10"> &nbsp;&nbsp;
port: <input type="edit" name="port" value="4711" size="5"> <br> <br>
cmd: <input type="edit" name="rcmd" value="get 1 sps1 w120.10"> &nbsp;&nbsp;
<input type="submit" name="read" value="Lesen" tabindex="1"> <br> <br>

<input type="edit" name="wcmd" value="set 1 sps1 w120.10" size="10"> &nbsp;&nbsp;
<input type="edit" name="value" value="4321" size="5"> &nbsp;&nbsp;
<input type="submit" name="write" value="Schreiben" tabindex="2">
</form>

<h3>direkte Verlinkungen:</h3>
<a href="telnet.php?debug=0&timeout=1&server=192.168.0.24&port=4711&rcmd=get 1 sps1 w120.0&read=1">Auslesen DB120 Word 0</a><br>
<a href="telnet.php?debug=0&timeout=1&server=192.168.0.24&port=4711&wcmd=set 1 sps1 w120.0&value=1234&write=1">Schreiben DB120 Word 0 mit Wert 1234</a><br>

</body>
</html>