简体   繁体   English

virtctl 在通过命令行执行而不是从 php exec() 执行时有效

[英]virtctl works when executed via command line but not from php exec()

I am trying to run kubectl virt commands to manage my virtual machine via PHP.我正在尝试运行 kubectl virt 命令来通过 PHP 管理我的虚拟机。 First, I log in to my server with phpseclib with the following code:首先,我使用 phpseclib 使用以下代码登录到我的服务器:

$ssh = new SSH2('localhost');
if (!$ssh->login('root', 'rootPassword')) {
    throw new \Exception('Login failed');
}

This part works fine, and when I try to run $ssh->exec('whoami && echo $PATH') , I get the following output:这部分工作正常,当我尝试运行$ssh->exec('whoami && echo $PATH')时,我得到以下输出:

root
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

But, whenever I try to run kubectl virt via PHP, I get the following output:但是,每当我尝试通过 PHP 运行kubectl virt时,都会得到以下输出:

error: unknown command "virt" for "kubectl"

kubectl and kubectl virt work perfectly fine when I run them via terminal but somehow do not work with PHP exec().当我通过终端运行kubectlkubectl virt时,它们工作得非常好,但不知何故不适用于 PHP exec()。 I also tried to check the $PATH via terminal and I get a different output:我还尝试通过终端检查$PATH并得到不同的输出:

/root/.krew/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

I thought that it may be because of $PATH but the interesting part is when I try to run sudo kubectl virt via terminal I also get the same error:我认为这可能是因为$PATH但有趣的部分是当我尝试通过终端运行sudo kubectl virt时,我也得到了同样的错误:

error: unknown command "virt" for "kubectl"

At that point, I am completely lost and don't even know where to look for a problem.在那一点上,我完全迷失了,甚至不知道在哪里寻找问题。 I am thankful for all the answers.我很感谢所有的答案。

When you are issuing ad-hoc ssh commands, you are not using interactive shell, and depending on your default shell behavior it may or may not load your .bashrc file .当您发出临时 ssh 命令时,您没有使用交互式 shell,并且根据您的默认 shell 行为,它可能会或可能不会加载您的 .bashrc 文件。 See https://serverfault.com/questions/936746/bashrc-is-not-sourced-on-ssh-command and Running command via ssh also runs .bashrc?请参阅https://serverfault.com/questions/936746/bashrc-is-not-sourced-on-ssh-command通过 ssh 运行命令也运行 .bashrc? for more details.更多细节。

So by default, krew modifies your PATH variable, and appends it's bin path to it, ie my config contains export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH" .因此,默认情况下,krew 会修改您的PATH变量,并将它的 bin 路径附加到它,即我的配置包含export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH" But what exactly is kubectl plugin?但是 kubectl 插件到底是什么? Usually it's just a single binary, with kubectl- plugin_name name.通常它只是一个带有 kubectl- plugin_name名称的二进制文件。 So by invoking which kubectl-virt you can easily know where is your virt binary located and invoke it directly, so something like所以通过调用which kubectl-virt你可以很容易地知道你的 virt 二进制文件在哪里并直接调用它,所以像

$ssh->exec('~/.krew/bin/kubectl-virt')

should work应该管用

The other way is to modify PATH all by yourself, setting PATH=$PATH:~/.krew/bin should make it work, at least in my case另一种方法是自己修改 PATH ,设置PATH=$PATH:~/.krew/bin应该使它工作,至少在我的情况下

ssh localhost 'PATH=$PATH:~/.krew/bin kubectl virt'

worked nicely.工作得很好。

You can try to force loading of .bashrc in your shell configuration, but personally i think it's a bad practice, and ssh commands are not usually loading rc files for a reason, command execution speed and consistency between systems are the first things that come to mind.您可以尝试在 shell 配置中强制加载 .bashrc,但我个人认为这是一种不好的做法,并且 ssh 命令通常不会加载 rc 文件是有原因的,首先要考虑的是命令执行速度和系统之间的一致性头脑。

Regarding sudo, it's actually not that surprising, because without -E or -i flags it won't load your current environment / won't start interactive shell.关于 sudo,实际上并不奇怪,因为没有-E-i标志它不会加载您当前的环境/不会启动交互式 shell。 See https://unix.stackexchange.com/questions/228314/sudo-command-doesnt-source-root-bashrc for more info有关更多信息,请参阅https://unix.stackexchange.com/questions/228314/sudo-command-doesnt-source-root-bashrc

暂无
暂无

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

相关问题 PHP exec()在通过浏览器执行时不会执行shell命令 - PHP exec() will not execute shell command when executed via browser 通过命令行执行时,PHP脚本无法正常工作,但从浏览器执行时,PHP脚本可以正常工作 - PHP script wont work when executed via command line but work fine when executed from browser Mac OS X上的phantomjs通过命令行工作,而不是通过exec() - phantomjs on Mac OS X works from the command line, not via exec() Shell命令可在命令行上运行,但不适用于PHP exec - Shell command works on command line but not in PHP exec PHP exec()可在命令行中使用,但不能在Web中使用 - PHP exec() works in command line but not in web PHP ImageMagick 脚本在通过命令行执行时不起作用 - PHP ImageMagick script not working when executed via command line 通过浏览器执行时,用于发送邮件的php脚本无法正常工作,但从命令行执行时,可以正常工作 - php script for sending mail wont work when executed via browser but work fine when executed from command line 通过php中的exec / system执行时获取linux命令退出代码 - getting linux command exit code when executed via exec/system in php PHP exec 命令适用于 shell 和 php 命令行,但不适用于网站 - PHP exec command works in shell and php command line but not on website 从 php 执行时,额外的文本来自 shell_exec 命令 - Extra text comes from shell_exec command when executed from php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM