I am trying to run kubectl virt commands to manage my virtual machine via PHP. First, I log in to my server with phpseclib with the following code:

$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:

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:

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(). I also tried to check the $PATH via terminal and I get a different output:

/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:

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 . See https://serverfault.com/questions/936746/bashrc-is-not-sourced-on-ssh-command and Running command via ssh also runs .bashrc? for more details.

So by default, krew modifies your PATH variable, and appends it's bin path to it, i.e. my config contains export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH". But what exactly is kubectl plugin? Usually it's just a single binary, with kubectl-plugin_name name. So by invoking which kubectl-virt you can easily know where is your virt binary located and invoke it directly, so something like

$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

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.

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. See https://unix.stackexchange.com/questions/228314/sudo-command-doesnt-source-root-bashrc for more info

Php相关问答推荐

在WooCommerce Checkout帐单地址字段后插入自定义按钮

PHP DOMDocument忽略第一个表S结束标记

WooCommerce在store 页面上显示兼容多语言的产品属性

为什么正则表达式与得到的文本块之前得到的也行?

如何在Foreach语句中使用php和li按降序排序?

有没有办法为整个PHP应用程序启用严格类型

返回WooCommerce中的所有已启用变体

如何使用MS Graph PHP SDK V2下载文件

如何在WordPress REST API中判断应用程序密码

在WooCommerce上设置简单和可变产品的最小订单量

在WooCommerce管理中为订单项目添加自定义字段价格值

Woocommerce 临时购物车税未根据第一个请求正确计算

用于更新 WooCommerce 管理产品列表中库存数量的自定义输入字段

WooCommerce 按产品运输插件:仅收取较高的运输费用

循环中未序列化数组显示的问题

Composer自动加载器重复了子类的类文件路径

如何使用 WhatsApp Cloud API 向 WhatsApp 发送消息,而无需在发送消息之前注册收件人号码?

Laravel:ArgumentCountError: 函数 App\Mail 的参数太少

为什么在解压缩具有混合字符串和 int 键的关联数组时出现错误消息无法使用位置参数...?

使用 splat 运算符时按引用传递 (...)