如何捕获和使用PHP和shell脚本饲料的telnet? [英] How to capture and feed telnet using php and shell scripting?

查看:176
本文介绍了如何捕获和使用PHP和shell脚本饲料的telnet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想使用PHP来实现(可能使用EXCE()):

This is what i want to accomplish using php (possibly using exce()?):


  1. telnet来使用程序调用proxychains一个域名注册的注册商:

  1. telnet to a whois registrar using a program called proxychains:

proxychains TELENT whois.someregistrar 43

如果失败 - >再次尝试1

if failed -> try 1 again

养活一个域名的连接:

somedomainname.com

我有shell脚本那么,如何捕获事件没有经验
其中的telnet连接,并挂起输入和我如何养活了?

I have no experience with shell scripting so how do i capture the event in which telnet is connected and hangs for input and how do i "feed" it?

难道我完全以在这里下车,或这是去了解它的正确方法?

Am i totaly off here or is this the right way to go about it?

编辑:我看到蟒蛇有韩德尔此使用预计

i see python have a good way to handel this using expect

推荐答案

下面是一个基本工作示例。

Here is a basic working example.

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

此是指被从命令行运行,但它并不必须如此。我试图评论它相当好。基本上在一开始可以设置域名服务器,并查询域。

This is meant to be run from the command line, but it doesn't have to be. I tried to comment it fairly well. Basically at the beginning you can set the whois server, and the domain to query.

该脚本使用 proc_open 打开 proxychains 进程调用远程登录。它检查是否该过程被成功打开,如果是检查其状态正在运行。虽然其运行时,它从远程登录的输出到缓冲区,然后查找字符串的telnet输出指示,我们都连接。

The script uses proc_open to open a proxychains process that calls telnet. It checks to see if the process was opened successfully, and if so check that its status is running. While its running, it reads the output from telnet into a buffer and looks for the string telnet outputs to indicate we are connected.

一旦检测的telnet连接时,将数据写入过程中跟着一个换行符( \\ n ),然后读取其中的telnet数据去管数据。一旦出现这种情况是爆发循环和关闭的过程和处理。

Once it detects telnet connected, it writes the data to the process followed by a newline (\n) and then reads the data from the pipe where the telnet data goes. Once that happens it breaks out of the loop and closes the process and handles.

您可以查看proxychains从 $ ERRFILE 指定的文件输出。这包含连接信息,以及在连接失败的情况下,调试信息。

You can view the output from proxychains from the file specified by $errFile. This contains the connection information as well as debug information in the event of a connection failure.

有可能是一些附加的,可能需要做,使之更加强劲,但如果你把这个变成一个功能,您应该能够轻松地调用它,并检查返回值,看看是否错误检查或流程管理查询成功。

There is probably some additional error checking or process management that may need to be done to make it more robust, but if you put this into a function you should be able to easily call it and check the return value to see if the query was successful.

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

Hope that gives you a good starting point.

还检查了我的这个答案 proc_open 的另一个工作例子,这个例子实现了一个超时检查,以便您可以保释,如果命令尚未在一定完成的时间:<一href=\"http://stackoverflow.com/questions/9356250/creating-a-php-online-grading-system-on-linux-exec-behavior-process-ids-and-g/9357331#9357331\">Creating一个PHP在线分级系统在Linux上:EXEC行为,进程ID,和grep

Also check out this answer of mine for another working example of proc_open, this example implements a timeout check so you can bail if the command hasn't completed in a certain amount of time: Creating a PHP Online Grading System on Linux: exec Behavior, Process IDs, and grep

这篇关于如何捕获和使用PHP和shell脚本饲料的telnet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆