2013-08-16 27 views
2

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

我的问题是我的标题为“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> 
+1

你能解释一下你的网页_目前_做什么,这样人们可以看到它可能出错的地方?它是否导致错误?您具体遇到什么问题,以及在哪一行号码? – halfer

+0

我收到一个“致命错误:调用未定义函数ssh2_connect()在/Applications/MAMP/htdocs/sandbox1/commandspg.php第65行”消息。 65行代表读取 - > $ connection = ssh2_connect($ ip)的PHP代码; 现在,我应该补充一点,我最初并没有将libssh2安装到我的系统上,但是自发布此问题后我已经完成了此操作。 –

+0

现在会发生什么? (请注意,如果你收到错误信息,你应该把它发布在你的问题开始,然后如果你在收到建议后纠正某些问题,那么你应该再次解释发生了什么。)那么,你还是会得到相同的结果错误信息? – halfer

回答

0

我认为expect是你所追求的。

+0

这是我目前正在得到的结果 - > 致命错误:调用未定义的函数ssh2_connect()在/Applications/MAMP/htdocs/sandbox1/commandspg.php第65行 –

+0

我应该补充说,行65似乎是涉及到PHP代码行 - > $ connection = ssh2_connect($ ip); –

+0

您没有安装ssh2 php模块,请查看以下说明:http://www.php.net/manual/en/ssh2.installation.php – Marek