Kaynak kodu ve orijinal (Fransız) sitedeki bazı okuma başlığına atıfta sonra ....
<?php
error_reporting(255);
ini_set('display_errors', true);
echo "1<br>";
require_once("telnet_class.php");
$telnet = new Telnet();
$telnet->set_host("10.10.5.7");
$telnet->set_port("2002");
if ($telnet->connect() != TELNET_OK) {
printf("Telnet error on connect, %s\n",$telnet->get_last_error());
}
//$telnet->wait_prompt();
if ($telnet->write('SNRD 1' . "\xd") != TELNET_OK) {
printf("Telnet error on write, %s\n",$telnet->get_last_error());
}
// read to \n or whatever terminates the string you need to read
if ($telnet->read_to("\n") != TELNET_OK) {
printf("Telnet error on read_to, %s\n",$telnet->get_last_error());
}
echo "3<br>";
$result = $telnet->get_buffer();
echo $result;
print_r($result);
// flush_now();
echo "4<br>";
$telnet->disconnect();
?>
Tamam, açıklama: get_buffer () sadece, tampon içinde ne okuma. Tampon bir şey almak için $ maçına kadar tampon içine okuyacak read_to ($ maç) yürütmek zorunda. Bundan sonra, get_buffer size istenen string vermelidir.
EDIT:
if you cannot find some string that follows the string you are interested in read_to will end in an error due to this part of the read_to method (translation of original french comment is mine):
if ($c === false){
// plus de caracteres a lire sur la socket
// --> no more characters to read on the socket
if ($this->contientErreur($buf)){
return TELNET_ERROR;
}
$this->error = " Couldn't find the requested : '" . $chaine . "', it was not in the data returned from server : '" . $buf . "'" ;
$this->logger($this->error);
return TELNET_ERROR;
}
Soket istenen dize bir maç olmadan kapatıldığında, TELNET_ERROR iade edilecektir anlamına gelir. Ancak, aradığınız dize bu noktada read_to iddiasına ne koydunuz .... tampon olmalıdır? Ben ne yaptım gibi ya da sadece "" "\ n"?
EDIT2 :
there's also a problem with get_buffer. IMO this class is not really a timesaver ;-)
//------------------------------------------------------------------------
function get_buffer(){
$buf = $this->buffer;
// cut last line (is always prompt)
$buf = explode("\n", $buf);
unset($buf[count($buf)-1]);
$buf = join("\n",$buf);
return trim($buf);
}
It will throw away the last line of the response, in your case the one that contains the
answer.
I suggest to add a "light" version of get_buffer to the class, like this
//------------------------------------------------------------------------
function get_raw_buffer(){
return $this->buffer;
}
ve gerekli sonucu kendinizi arama / düzeltme yok.
Ayrıca şu sürekli eklemek isteyebilirsiniz
define ("TELNET_EOF", 3);
ve bu gibi read_to değiştirmek
...
if ($c === false){
// plus de caracteres a lire sur la socket
if ($this->contientErreur($buf)){
return TELNET_EOF;
}
$this->error = " Couldn't find the requested : '" . $chaine . "', it was not in the data returned from server : '" . $buf . "'" ;
$this->logger($this->error);
return TELNET_EOF;
}
...
Bu özel durum kendinizi tedavi etmek için (bir sonuç kodu TELNET_EOF sizin durumunuzda bir hata olarak tedavi edilmesi gerekmez). Sonunda kodunuzu bu gibi daha fazla veya daha az bakmak gerekir:
// read to \n or whatever terminates the string you need to read
if ($telnet->read_to("\n") == TELNET_ERROR) {
printf("Telnet error on read_to, %s\n",$telnet->get_last_error()); }echo "3<br>";
}else {
$result = $telnet->get_raw_buffer();
echo $result;
print_r($result);
}