【问题标题】:How to capture and feed telnet using php and shell scripting?如何使用 php 和 shell 脚本捕获和提供 telnet?
【发布时间】:2012-07-12 19:10:46
【问题描述】:

这就是我想要使用 php 完成的(可能使用 exce()?):

  1. 使用名为 proxychains 的程序远程登录至 whois 注册商:

    代理链 telent whois.someregistrar 43

  2. 如果失败 -> 再次尝试 1

  3. 为连接提供一个域名:

    somedomainname.com

  4. 捕获注册商返回给php的数据

我没有使用 shell 脚本的经验,所以我如何捕获事件 在哪个 telnet 连接并挂起输入,我如何“喂”它?

我完全离开这里还是这样做的正确方法?

编辑:我看到 python 有一个很好的方法来处理这个使用expect

【问题讨论】:

  • 对于初学者,请查看popenproc_open。稍后我会尝试发布一个示例。这些函数允许您打开进程并向其写入数据以及读取输出。
  • @drew010 我会看看,感谢你写了一个示例绘制

标签: php bash shell telnet


【解决方案1】:

这是一个基本的工作示例。

<?php

$whois   = 'whois.isoc.org.il';            // server to connect to for whois
$data    = 'drew.co.il';                   // query to send to whois server
$errFile = '/tmp/error-output.txt';        // where stderr gets written to
$command = "proxychains telnet $whois 43"; // command to run for making query

// variables to pass to proc_open
$cwd            = '/tmp';
$env            = null;
$descriptorspec = array(
        0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
        1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
        2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);

// process output goes here
$output  = '';

// store return value on failure
$return_value = null;

// open the process
$process = proc_open($command, $descriptorspec, $pipes, $cwd, $env);

if (is_resource($process)) {
    echo "Opened process...\n";

    $readBuf = '';

    // infinite loop until process returns
    for(;;) {
        usleep(100000); // dont consume too many resources

        // TODO: implement a timeout

        $stat = proc_get_status($process); // get info on process

        if ($stat['running']) { // still running
            $read = fread($pipes[1], 4096);
            if ($read) {
                $readBuf .= $read;
            }

            // read output to determine if telnet connected successfully
            if (strpos($readBuf, "Connected to $whois") !== false) {
                // write our query to process and append newline to initiate
                fwrite($pipes[0], $data . "\n");

                // read the output of the process
                $output = stream_get_contents($pipes[1]);
                break;
            }
        } else {
            // process finished before we could do anything
            $output       = stream_get_contents($pipes[1]); // get output of command
            $return_value = $stat['exitcode']; // set exit code
            break;
        }
    }

    echo "Execution completed.\n";

    if ($return_value != null) {
        var_dump($return_value, file_get_contents($errFile));
    } else {
        var_dump($output);
    }

    // close pipes
    fclose($pipes[1]);
    fclose($pipes[0]);

    // close process
    proc_close($process);
} else {
    echo 'Failed to open process.';
}

这意味着从命令行运行,但并非必须如此。我试图很好地评论它。基本上一开始你可以设置whois服务器,以及要查询的域。

脚本使用proc_open 打开一个调用telnet 的proxychains 进程。它检查进程是否成功打开,如果成功则检查其状态是否正在运行。在运行时,它将 telnet 的输出读取到缓冲区中,并查找字符串 telnet 输出以指示我们已连接。

一旦检测到 telnet 已连接,它将数据写入进程,后跟换行符 (\n),然后从 telnet 数据所在的管道中读取数据。一旦发生这种情况,它就会跳出循环并关闭进程和句柄。

您可以从$errFile 指定的文件中查看代理链的输出。这包含连接信息以及连接失败时的调试信息。

可能需要进行一些额外的错误检查或流程管理以使其更加健壮,但如果您将其放入函数中,您应该能够轻松调用它并检查返回值以查看查询成功。

希望能给你一个好的起点。

还可以查看我的这个答案以获取 proc_open 的另一个工作示例,此示例实现了超时检查,因此如果命令在一定时间内未完成,您可以保释:Creating a PHP Online Grading System on Linux: exec Behavior, Process IDs, and grep

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多