bash 设备的 ioctl 不合适

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29430986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:43:16  来源:igfitidea点击:

Inappropriate ioctl for device

phpbashsshphpseclib

提问by Toby Mellor

I'm trying to execute SSH commands through a PHP application.

我正在尝试通过 PHP 应用程序执行 SSH 命令。

Through the PHP application, I type "./SSDownload.sh". Through my terminal SSH, I type "./SSDownload.sh".

通过 PHP 应用程序,我键入“./SSDownload.sh”。通过我的终端 SSH,我输入“./SSDownload.sh”。

SSDownload.sh contains:

SSDownload.sh 包含:

cd /var/www/html/dev/media/mp3/
spotify-to-mp3 songs.txt

Through my terminal SSH, I get this response:

通过我的终端 SSH,我得到以下响应:

Resolving "fancy iggy azalea"
Searching "fancy iggy azalea" on Grooveshark
"Iggy Azalea - Fancy (feat. Charli XCX)" will be downloaded

Downloading tracks...
[1/1] Iggy Azalea - Fancy (feat. Charli  100% [==================================] Time: 00:00:01
Download complete

The file downloads successfully, and places the files in the correct directory.

文件下载成功,并将文件放置在正确的目录中。

I tried the same with PHP using the following code:

我使用以下代码对 PHP 进行了相同的尝试:

include('Net/SSH2.php');
$ssh = new Net_SSH2('iphere');
if (!$ssh->login('root', 'password')) {
exit('Login Failed');
}

echo "1 : " . $ssh->exec('./SSDownload.sh ' . $_GET['player']) . "<br />";

Output:

输出:

Resolving "fancy iggy azalea" Searching "fancy iggy azalea" on Grooveshark "Iggy Azalea - Fancy (feat. Charli XCX)" will be downloaded Downloading tracks... [0;31;49mInappropriate ioctl for device[0m [0;32;49mDownload complete[0m

The file does not download successfully, and does not place the files in the correct directory.

该文件未成功下载,并且未将文件放置在正确的目录中。

I'd like to bring your attention to "Inappropriate ioctl for device", whatever it means.

我想让您注意“设备的 ioctl 不合适”,无论它是什么意思。

Why are the responses different? How can I go about fixing it?

为什么反应不同?我该如何修复它?

回答by Kenster

An "ioctl" is a POSIX operation to send a control command to a device ("I/O control"). The "Inappropriate ioctl" error means that the program sent an ioctl to a device that wasn't correct in some way. The question is what ioctls this spotify program may be using that are dependent on how it's run.

“ioctl”是一种 POSIX 操作,用于向设备发送控制命令(“I/O 控制”)。“不适当的 ioctl”错误意味着程序向某个设备发送的 ioctl 以某种方式不正确。问题是这个 Spotify 程序可能使用的 ioctl 取决于它的运行方式。

When you run the spotify program from the command line, it has a TTY. When your PHP code launches the program, it doesn't have a TTY. It's probably sending an ioctl to its standard output for some reason, expecting standard output to be a tty. My guess is that it's using an ioctl to get the terminal width so that it can display that progress bar graphic.

当您从命令行运行 spotify 程序时,它有一个 TTY。当您的 PHP 代码启动程序时,它没有 TTY。它可能出于某种原因向其标准输出发送了一个 ioctl,期望标准输出是一个 tty。我的猜测是它使用 ioctl 来获取终端宽度,以便它可以显示该进度条图形。

There are two ways to fix this. You could check the documentation for this spotify program to see if it has a noninteractive mode or batch mode or quiet mode that disables printing the progress bar. That might avoid performing the ioctl that's failing.

有两种方法可以解决这个问题。您可以查看此 Spotify 程序的文档以查看它是否具有禁用打印进度条的非交互模式或批处理模式或安静模式。这可能会避免执行失败的 ioctl。

The other fix is to have PHP request a pty (pseudo-TTY) for the ssh session. I'm not a PHP coder, but this pageappears to describe the Net_SSH2 API, and it lists a function to enable requesting a PTYfor calls to exec().

另一个解决方法是让 PHP 为 ssh 会话请求一个 pty(伪 TTY)。我不是一个 PHP 编码员,但这个页面似乎描述了 Net_SSH2 API,它列出了一个函数来启用请求 PTY以调用 exec()。

回答by hanshenrik

try

尝试

echo "1 : " . $ssh->exec('/bin/bash -c '.escapeshellarg('./SSDownload.sh ' . escapeshellarg($_GET['player']))) . "<br />";