使用 PHP 从命令行读取/写入数据

发布于 2024-09-12 00:54:58 字数 471 浏览 5 评论 0原文

我有一台服务器(debian lenny)、光谱分析仪,并且我已经从 Steve 下载了源代码集合Sharples 的 网站。有了这些。我可以连接到频谱分析仪 - 输入命令并获得响应。

例如:

my-atom:~/vxi11# ./vxi11_cmd 135.123.106.59
Input command or query ('q' to exit): *IDN?

我得到:Rohde&Schwarz,FSV-7,102004/007,1.50 SP1

我可以使用 PHP 连接到此服务器、编写命令并读取响应吗?我正在考虑套接字,但我不确定是否是最好的选择。

任何帮助将不胜感激。

谢谢, 彼得

I have a server (debian lenny), spectral analyzer and I have downloaded collection of source codes from Steve Sharples's site. With these s. coudes I am able to connect to spectral analyzer - type command and get the responce.

For example:

my-atom:~/vxi11# ./vxi11_cmd 135.123.106.59
Input command or query ('q' to exit): *IDN?

and I get: Rohde&Schwarz,FSV-7,102004/007,1.50 SP1

Am I able to connect to this server, write command and read the responce using PHP? I was thinking about sockets, but I am not sure if the best choice.

Any help would be most appreciated.

Thanks,
Petr

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

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

发布评论

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

评论(3

如何视而不见 2024-09-19 00:54:58

您可以使用 execshell_exec, 系统,或 proc_open

我推荐 proc_open,它允许您为 stdin、stdout 和 stderr 设置管道。

查看 proc_open 链接。

You can execute external programs using exec, shell_exec, system, or proc_open.

I would recommend proc_open, which allows you to set up pipes for stdin, stdout, and stderr.

Check out the proc_open link.

束缚m 2024-09-19 00:54:58

你也可以这样做:

$result = `SOME_COMMAND_HERE`;

Also you can do like:

$result = `SOME_COMMAND_HERE`;
娇柔作态 2024-09-19 00:54:58

您可以将 shell_exec 与 netcat 结合使用:

<?php
$output = shell_exec('echo "TRAC? TRACE1" | netcat -q 1 135.123.106.59 5025');
echo "<pre>$output</pre>";
?>

注意 -q 1 选项,这会导致 netcat 等待 1 秒以获得答案。您不需要“设置”命令的等待时间。另外,*IDN?不需要等待时间,但许多其他查询命令做。

使用 netcat 进行此连接与裸机连接非常危险。您可能想要编写一个捕获常见错误的 shell 脚本。然后您可以使用 shell_exec 调用该脚本。

当然,最好的方法是采用 Steve Sharples 免费库 并修改他的 cmd适合您自己需求的示例。

You can combine shell_exec with netcat:

<?php
$output = shell_exec('echo "TRAC? TRACE1" | netcat -q 1 135.123.106.59 5025');
echo "<pre>$output</pre>";
?>

Note the -q 1 option, which causes netcat to wait for 1 second for the answer. You don't need this wait time on "set" commands. Also, *IDN? does not need the wait time, but many other query commands do.

Using netcat for this connection is dangerously close to the bare metal. You might want to write a shell-script that catches common errors. You can then call that script with shell_exec instead.

The best way of course would be to take Steve Sharples free library and modify his cmd example to suit your own needs.

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