使用PHP发送SSH命令并在网页中打印结果

时间:2013-08-16 15:09:44

标签: php html mysql ssh

我几乎完成了一个网站,允许Field Engineers访问我们控制的某些服务器并通过Python脚本运行某些命令。在大多数情况下,该网站正在工作,并做我需要它做的事情。我在MAMP上创建的数据库工作正常,是我存储用户注册数据的地方。

我的问题在于我名为" commandspg.php"这是用户输入各种命令以提交给服务器的地方。使用Python和一些Paramiko,使用SSH shell发送命令是必要的,而且我已经在那里撞墙了。

我已经查找了几乎所有可能的编码配置来完成这项工作并运行了很多,但似乎没有人给我我之后的结果。我想要发生的是,用户可以使用我们的GUI并输入用户密码'密码'服务器IP地址和命令 - 我们在他们可以访问的单独页面上列出它们 - 然后提交它。然后,这将允许他们转到" reportspg.php"并阅读和/或打印其命令行动的结果。

非常感谢任何建议和/或指导。哦,我应该指出,我确实必须删除一些关键字,以免让我知道我是谁写的。谢谢!

以下是整个页面的编码内容。

<html>
<head>
<style type="text/css">
 body,td,th {
color: #03F;
 }
 a:link {
color: #29D93F;
 }
 body {
background-color: #DAD5D5;
 }
 </style>
 <meta 
 http-equiv="Page-Enter" content="revealTrans(Duration=3.0,Transition=23)"> 
 <meta http-equiv="Page-Exit" content="revealTrans(Duration=3.0,Transition=23)">
  </head>
<body>
<center>
<p><h1><strong><big>SANDBOX SERVER ACCESS PAGE</big></strong></h1></p></center>
<a href="userlogoutpg.php"><strong>LOGOUT PAGE &rarr; </strong></a><br>
<br>
<a href="commandslistpg.php"><strong>COMMANDS HELP LIST &rarr; </strong></a><br>
    <br>
<form action="commandspg.php" method="post">
   <fieldset {width: 20%; float: left; position: relative;}><legend><strong>Enter IP  
    Manually</strong></legend>
  <input type="text" name="ipaddress" value=" " size="50">       
   </fieldset>
  <fieldset {width: 20%; float: left; position: relative;}><legend><strong>User</strong>
  </legend>
 <br>
<p>
<label>
<input type="text" name="user" value=" " size="50">
</label>
</p>
</fieldset>
<fieldset {width: 20%; float: left; position: relative;}><legend>    
 <strong>Password</strong></legend>
<p>
<label>
<input type="password" name="password" value="" size="50">
</label>
</p>
</fieldset>
<fieldset {width: 20%; float: left; position: relative;}><legend>  
  <strong>command</strong></legend>
<p>
<label>
<input type="text" name="cmd" value=" " size="50">
</label>
</p>
</fieldset>
<p>
<center>
<input type="submit" input name="submit" value="  SUBMIT  "><input type="reset"  
  input name="reset" value="   CLEAR   ">
</p>
 </form>
<?php
 $ip = 'ipaddress'; 
 $user = 'user'; 
 $pass = 'password';
 $cmd = 'cmd'; 

 if(isset($_POST['submit']) ) {

  $connection = ssh2_connect($ip); 
  ssh2_auth_password($connection,$user,$pass); 
  $shell = ssh2_shell($connection,"bash");

  function __construct($host='', $port=''  ) {

 if( $host!='' ) $this->host  = $host;
 if( $port!='' ) $this->port  = $port;

 $this->con  = ssh2_connect($this->host, $this->port);
 if( !$this->con ) {
   $this->log .= "Connection failed !";
 }

 }

 function authPassword( $user = '', $password = '' ) {

 if( $user!='' ) $this->user  = $user;
 if( $password!='' ) $this->password  = $password;

 if( !ssh2_auth_password( $this->con, $this->user, $this->password ) ) {
   $this->log .= "Authorization failed !";
 }

 }

 function openShell( $shell_type = '' ) {

    if ( $shell_type != '' ) $this->shell_type = $shell_type;
 $this->shell = ssh2_shell( $this->con,  $this->shell_type );
 if( !$this->shell ) $this->log .= " Shell connection failed !";

 }

 function writeShell( $command = '' ) {

 fwrite($this->shell, $command."\n");

}

function cmdExec( ) {

    $argc = func_num_args();
    $argv = func_get_args();

 $cmd = '';
 for( $i=0; $i<$argc ; $i++) {
    if( $i != ($argc-1) ) {
      $cmd .= $argv[$i]." && ";
    }else{
      $cmd .= $argv[$i];
    }
  }
  echo $cmd;

    $stream = ssh2_exec( $this->con, $cmd );
  stream_set_blocking( $stream, true );
  return fread( $stream, 4096 );

}

function getLog() {

 return $this->log;

    }
}
?>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

我认为expect就是你所追求的目标。

答案 1 :(得分:0)

试试phpseclib, a pure PHP SSH implementation。例如

<?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('ls -la');
?>

phpseclib提供了许多advantages over the alternatives