php - Can I use the SSH vi code editor with phpseclib?
问题描述
Hey I am wondering if I can use the vi code editor while connected with phpseclibs SSH2. My goal is to be able to run a PHP script and update values of a config file. I can connect via SSH just fine, but nothing I enter into the script afterwords seems to be changing anything in the targeted config file.
This is what I have so far.
<?php
require __DIR__ . '/vendor/autoload.php';
use phpseclib\Net\SSH2;
//simulate pressing the escape key
$esc = chr(27);
$ssh = new SSH2('ip address');
if(!ssh->login('username', 'password')){
exit('Login Failed');
}
sleep(5);
//enter vi editor
$ssh->write("vi file address\n");
sleep(5);
//"/"enables seach mode in vi editor
$ssh->write("/seachword\n");
sleep(1);
//clears line in vi editor
$ssh->write("cc");
sleep(1);
//enter in new word on that empty line
$ssh->write("replacement word");
sleep(1);
//simulates pressing the esc key
$ssh->write("$esc");
sleep(1);
//saves and closes the vi editor
$ssh->write(":wq\n");
?>
解决方案
我最终使用 sed 来编辑配置文件,效果很好,感谢 neubert。这是我正在使用的。
$ssh = new SSH2('IP');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}
$ssh->exec("sed -i -e 's/text to find/replacement text/g' /path/and/name");
推荐阅读
- javascript - 我想将给定的 JSON 数据打印成 HTML。我想从给定的 JSON 数据中打印“文章”数组。这是 Postman Visualizer 部分
- python - 熊猫:对特定行应用过滤器
- sql - N1QL Couchbase 更新嵌套对象的属性,其中键是动态的
- c# - System.BadImageFormatException:错误的二进制签名。在 Azure 应用服务中
- html - 平面选择器日期以意外格式显示
- javascript - 在 JavaScript 中使用 @type 声明类方法的类型
- python - 将数据框单元格作为变量传递
- android - Android Debug Bridge 强制 RTL 选项在没有 root 的情况下应用
- powershell - 获取定义为别名的文件的位置
- javascript - 如何找到算术运算符并将其及其索引分配给全局可访问变量?