简体   繁体   English

php 向端口发送命令(通过 tcp)

[英]php send command to port (via tcp)

I am looking for a php script to trigger commands to a specific port via tcp.我正在寻找一个 php 脚本来通过 tcp 触发到特定端口的命令。 The script is running on the same machine that I would like to send those commands to, so the ip is 127.0.0.1 or localhost.该脚本在我想将这些命令发送到的同一台机器上运行,因此 IP 是 127.0.0.1 或 localhost。

The command will be defined be whatever is written behind the url.该命令将被定义为 url 后面写的任何内容。 For example, trigger.php?screen will send command 'screen', trigger.php?sound will send command 'sound' etc.例如,trigger.php?screen 将发送命令 'screen',trigger.php?sound 将发送命令 'sound' 等。

Is this possible?这可能吗? I don't have much coding knowledge.我没有太多的编码知识。

This is it in a nutshell: when I open 127.0.0.1/trigger.php?sound I would like command sound to be sent to 127.0.0.1 port 33000 via tcp.简而言之:当我打开 127.0.0.1/trigger.php?sound 时,我希望通过 tcp 将命令声音发送到 127.0.0.1 端口 33000。 If this is easier to implement with html or cgi, that would be fine, too.如果使用 html 或 cgi 更容易实现,那也很好。

Thanks in advance for your help.在此先感谢您的帮助。

Your trigger.php would go like this (changing an example from http://php.net/manual/en/sockets.examples.php to fit your problem;)您的 trigger.php 会像这样(从http://php.net/manual/en/sockets.examples.php更改示例以适应您的问题;)

<?php
$DEBUG=1;

if ($DEBUG) {
     print "<pre>\n";
     error_reporting(E_ERROR | E_WARNING | E_PARSE);
     ini_set("display_errors", 1);
     ini_set('display_startup_errors',1);
}
if (isset($_SERVER['QUERY_STRING'])
    $cmd=$_SERVER['QUERY_STRING']; //get the command name
else
    $cmd="";

print "Command to trigger: $cmd\n";
if ($cmd!="") {
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if ($socket === false) {
        echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
    } else {
        if ($DEBUG) print "Socket created successfully\n";
        $result = socket_connect($socket, "127.0.0.1", 33000);
        if ($result === false) {
            echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
        } else {
            if ($DEBUG) print "Connected successfully\n";
            socket_write($socket, $cmd, strlen($cmd));
            //Read and output possible response from the command sent
            echo "Response: ";
            while ($out = socket_read($socket, 2048)) {
                echo $out;
            }
        }
        socket_close($socket);
    }
}
if ($DEBUG) print "The end.</pre>\n";

?>

(Note i did not run it or do syntax check but looks like it might be what you want) (注意我没有运行它或做语法检查,但看起来它可能是你想要的)

So I did some research about sending a telnet command via php and found a script similar to what I need.所以我做了一些关于通过 php 发送 telnet 命令的研究,并找到了一个类似于我需要的脚本。 I have modified it and added user3134164's line to get the command name.我修改了它并添加了 user3134164 的行来获取命令名称。

Script looks like this and works perfectly.脚本看起来像这样并且完美运行。 I open phptrigger.php?command and whatever I put behind the question mark will be executed just as planned.我打开 phptrigger.php?command ,我在问号后面的任何内容都将按计划执行。 Thanks a lot for your help, user3134164 :)非常感谢您的帮助,user3134164 :)

<?php
    $socket = fsockopen("localhost", "33000", $errno, $errstr); 
    $cmd=$_SERVER['QUERY_STRING']; 

    if($socket) 
    { 
        echo "Connected. <br /><br />"; 
    }
    else 
    { 
        echo "Connection failed!<br /><br />"; 
    }

    fputs($socket, "get /$cmd \r\n"); 
    $buffer = ""; 

    while(!feof($socket)) 
    { 
        $buffer .=fgets($socket, 4096); 
    } 

    print_r($buffer); 
    echo "<br /><br /><br />"; 

    var_dump($buffer); 

    fclose($socket); 
?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM