如何使用ssh登录到嵌入式ubuntu服务器


How to use ssh to login to an embedded ubuntu server

我一直在使用Python来完成我想要完成的大部分事情。但是,为了拓宽我的视野,我也在尝试学习PHP。我怎么能ssh到一个ubuntu服务器使用php?下面是几行代码(要求用户输入ip地址的表单):

 <form name="form1" method="post" action="connect.php">
 <label>Connect:
 <input type="text" name="connect" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'Enter IP Address To Connect':this.value;" value="Enter IP Address To Connect"/>
  </label>
  <label>
  <input type="submit" name="Submit" value="Submit"/>
  </label>
</form>

此外,是否有一些东西可以显示让用户知道他/她登录了(例如…)已连接:绿色单选按钮;断开连接:红色单选按钮)?谢谢。

阅读一些关于PHP中SSH的知识

为什么不ssh与bash?
这很简单,你很快就会上手的!除非你的程序不需要在Windows(或其他非*Nix系统)上运行,否则尽量编写可移植的bash代码。

注::如果您使用ssh存储库(例如ssh库)自动执行ssh -ing任务,将会容易得多。E:ssh-agent using ssh-add);

@Kounavi -不是所有的主机都允许你执行exec()或system()之类的操作。

我的建议是使用phpseclib,一个纯PHP SSH实现。例子:

<?php
include('Net/SSH2.php');
$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}
echo $ssh->exec('pwd');
echo $ssh->exec('cd /');
echo $ssh->exec('pwd');
?>