2

我几乎完成了一个站点,该站点将允许现场工程师访问我们控制的某些服务器并通过 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>
4

2 回答 2

0

Try phpseclib, a pure PHP SSH implementation. eg.

<?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 offers a number of advantages over the alternatives.

于 2013-08-16T16:26:37.520 回答
0

我认为期望是你所追求的。

于 2013-08-16T15:19:43.773 回答