php telnet 脚本没有响应

发布于 2024-08-08 10:07:46 字数 944 浏览 4 评论 0原文

我在 Windows XP Professional 上运行 PHP5。我正在尝试编写一个 telnet php 脚本,它可以简单地连接、发送文本字符串并获取响应缓冲区并将其输出。我使用这里的 telnet 类文件:

http://cvs .adfinis.ch/cvs.php/phpStreamcast/telnet.class.php

我在另一个线程中找到了它。

<?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");
$telnet->connect();
//$telnet->wait_prompt();
$telnet->write('SNRD   1%0d');
echo "3<br>";
$result = $telnet->get_buffer();
        echo $result;
        print_r($result);
//        flush_now();
echo "4<br>";

$telnet->disconnect();

?>

我没有收到任何类型的错误或响应。如果我发送一个无效的字符串,我至少应该得到一个“ERR”响应,但我什至没有得到它。有什么想法我可能做错了什么吗?如果我从命令提示符执行相同的操作,我会收到所需的字符串输出。难道是因为 write 函数正在发送

I'm running PHP5 on Windows XP Professional. I'm trying to write a telnet php script which simply connects, sends a text string and grabs the response buffer and outputs it. I'm using the telnet class file from here:

http://cvs.adfinis.ch/cvs.php/phpStreamcast/telnet.class.php

which i found in another thread.

<?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");
$telnet->connect();
//$telnet->wait_prompt();
$telnet->write('SNRD   1%0d');
echo "3<br>";
$result = $telnet->get_buffer();
        echo $result;
        print_r($result);
//        flush_now();
echo "4<br>";

$telnet->disconnect();

?>

I'm not receiving any kind of errors or response. If I send an invalid string, I should get an 'ERR' response in the least however I don't even get that. Any ideas what i could be doing wrong? If I do the same thing from the command prompt, I receive the string output I need. Could this is because the write function is sending

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

亽野灬性zι浪 2024-08-15 10:07:46

在阅读了源代码和标题中提到的原始(法语)站点后......

<?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();

?>

好吧,解释: get_buffer() 就是这样做的,读取缓冲区中的内容。要在缓冲区中获取某些内容,您必须执行 read_to($match),它将读入缓冲区直至 $match。之后, get_buffer 应该为您提供所需的字符串。

编辑:
如果您找不到您感兴趣的字符串后面的某个字符串,则 read_to 将因 read_to 方法的这一部分而以错误结束(原始法语注释的翻译是我的):这

    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;
    } 

意味着当套接字在没有匹配的情况下关闭时请求的字符串,将返回 TELNET_ERROR。但是,您要查找的字符串此时应该在缓冲区中......您在 read_to 的参数中放入了什么? “\n” 就像我所做的那样还是只是“”?

编辑2:
get_buffer 也有问题。 IMO 这个类并不是真正的省时工具;-)

//------------------------------------------------------------------------
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);
} 

它会丢弃响应的最后一行,在您的情况下,包含以下内容的行:
回答。
我建议向类中添加 get_buffer 的“轻量级”版本,如下所示

//------------------------------------------------------------------------
function get_raw_buffer(){
    return $this->buffer;

}

并自己在结果中进行必要的修剪/搜索。

您可能还想添加以下常量

define ("TELNET_EOF", 3);

并像这样更改 read_to ,

...
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;
} 
...

以便自己处理该特殊情况(在您的情况下,结果代码 TELNET_EOF 不必被视为错误)。所以最后你的代码应该看起来或多或少像这样:

// 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);
}

After some reading in the source code and on the original (french) site referred to in the header....

<?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();

?>

Okay, explanation: get_buffer() does just that, read what's in the buffer. To get something in the buffer you have to execute read_to($match) who will read into buffer up to $match. After that, get_buffer should give you the desired string.

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;
    } 

Meaning that when the socket is closed without a match of the requested string, TELNET_ERROR will be returned. However, the string you're looking for should at that point be in the buffer.... What did you put in read_to's argument? "\n" like what I did or just "" ?

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;

}

and do the necessary trimming/searching in the result yourself.

You might also want to add the following constant

define ("TELNET_EOF", 3);

and change read_to like this

...
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;
} 
...

in order to treat that special case yourself (a result code TELNET_EOF doesn't have to be treated as an error in your case). So finally your code should look more or less like this:

// 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);
}
温柔戏命师 2024-08-15 10:07:46

您检查过 telnet 类是如何工作的吗?也许它不是为在 Windows 下运行而设计的。如果您正在谈论的是一个简单的套接字,也许可以考虑使用常规的套接字连接。

https://www.php.net/sockets

如果你打开了一个你没有打开的套接字close,只要脚本正在运行,您就应该在 netstat 中看到一个条目。

netstat -na|查找“:2002”

Have you checked how the telnet class works? Maybe it wasn't designed to run under windows. If it's a simple socket that you're speaking to, maybe consider using a regular socket-connection instead.

https://www.php.net/sockets

If you open up a socket which you don't close, you should se an entry in netstat as long as your script is running.

netstat -na|find ":2002"

九局 2024-08-15 10:07:46

您不能像这样插入十六进制值。远程进程刚刚看到

SRND   1%0d

,现在正在等待线路终止。试试这个

$telnet->write('SNRD   1' . "\r");

$telnet->write("SNRD   1\xd");

双引号非常关键,请参阅 此处

编辑:

您可能会尝试添加一些错误报告,因为现在您实际上并没有检查太多(error_reporting 不会显示有关 telnet 类中的错误的任何内容)....例如:

<?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());
}

echo "3<br>";
$result = $telnet->get_buffer();
        echo $result;
        print_r($result);
//        flush_now();
echo "4<br>";

$telnet->disconnect();

?>

另外,您确定需要 \r\n 行终止吗? write 被定义为

function write($buffer, $valeurLoggee = "", $ajouterfinLigne = true){

并且

    if ($ajouterfinLigne){
        $buffer .= "\n";
    } 

?

另外,您是否使用命令行 telnet 客户端测试了主机和端口?喜欢

telnet 10.10.5.7 2002

You can't insert hex values like that. The remote process just sees

SRND   1%0d

and now it's waiting for the line to be terminated. Try this

$telnet->write('SNRD   1' . "\r");

or

$telnet->write("SNRD   1\xd");

The double quotes are quite critical, see here

EDIT:

you might try adding some error reporting as right now you don't really check much (error_reporting won't show anything on the errors in the telnet class).... For example:

<?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());
}

echo "3<br>";
$result = $telnet->get_buffer();
        echo $result;
        print_r($result);
//        flush_now();
echo "4<br>";

$telnet->disconnect();

?>

also, are you sure you need \r\n line termination? write is defined as

function write($buffer, $valeurLoggee = "", $ajouterfinLigne = true){

and does

    if ($ajouterfinLigne){
        $buffer .= "\n";
    } 

?

Also, did you test the host and port with the command line telnet client? Like

telnet 10.10.5.7 2002

?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文