Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
来自php的ssh连接_Php_Putty - Fatal编程技术网

来自php的ssh连接

来自php的ssh连接,php,putty,Php,Putty,我需要使用用户在我的Web表单上输入的用户名ssh到服务器 如何做到这一点?也许您可以在PHP中使用命令行脚本,这取决于您想要什么 如果您的意思是“如何通过SSH从我的网站连接(到另一台服务器)”,那么您可以使用PECL ssh2库来实现这一点 见: 演练(未测试):首先,没有PuTTy命令。这些是shell命令 要在shell中运行PHP脚本,您需要使用PHP cli:我不确定,但我认为(如果我错了,请纠正我),您需要单击网页上某个链接的某个位置,然后打开putty(在用户的计算机上)以连接

我需要使用用户在我的Web表单上输入的用户名ssh到服务器


如何做到这一点?

也许您可以在PHP中使用命令行脚本,这取决于您想要什么

如果您的意思是“如何通过SSH从我的网站连接(到另一台服务器)”,那么您可以使用PECL ssh2库来实现这一点

见:


演练(未测试):

首先,没有PuTTy命令。这些是shell命令

要在shell中运行PHP脚本,您需要使用PHP cli:

我不确定,但我认为(如果我错了,请纠正我),您需要单击网页上某个链接的某个位置,然后打开putty(在用户的计算机上)以连接到服务器

您可以将Putty配置为处理ssh://链接。如何做,你可以找到

配置后,您需要做的就是创建一个类似以下内容的链接:

<a href="ssh://user@remoteServer">Click here to connect</a>

请记住,这只适用于配置为处理ssh://链接类型的系统


我希望这能回答您的问题。

这就是如何通过PHP使用putty(不依赖cli)。请注意,密码不受保护,交互式ssh会话将涉及更多内容。但是,HTTPS和mcrypt(如果需要存储密码和/或bash脚本)可以使这成为一个安全的解决方案

<?php
// EDIT: added escapeshellcmd() to following vars
$user = escapeshellcmd($_POST['user']);  // username
$host = escapeshellcmd($_POST['host']);  // domain
$pass = escapeshellcmd($_POST['pass']);  // password

// create a string that will be loaded into a bash file for putty
// String can easily be made dynamically.
$bash_sh = <<<EOF                   #START OF BASH
\#!/bin/bash    
echo "BASH ON SSHD SIDE"
for (( i=1; i<=5; i++ ))            # BASH FOR LOOP
do
   echo "echo \$i times in bash"    #\$i is BASH not PHP, so have to escape
done
EOF;                                #END OF BASH

// creates a temp file called 'bash.sh' using the bash script above
file_put_contents("bash.sh", $bash_sh);

// executes putty using the args -ssh, -pw, -t, -m
// -ssh tells putty to use ssh protocol
// -pw tells putty to enter the password automaticaly
// -t tells putty to use a psudo terminal.
// -m tells putty read and execute bash.sh once logged in
exec("putty.exe -ssh ".$user."@".$host." -pw ".$pass." -t -m bash.sh");

// delete bash file since it has been sent
unlink('bash.sh');

?>


为什么您认为需要打开putty?当我单击链接时,我需要使用提供的用户名通过ssh连接到服务器。然后您会怎么做?你希望它是互动的吗?谢谢你的回复。我现在将检查它。还要注意,您需要使用escapeshellcmd()来防止命令行注入。