3

I need to ssh to the server using the username user has entered on my webform.

How can this be done?

3
  • Why do you think you need to open putty? Commented Sep 20, 2012 at 5:54
  • When I click on a link, I need to ssh to the server using the username provided. Commented Sep 20, 2012 at 5:58
  • What would you do then? Do you expect it to be interactive? Commented Sep 20, 2012 at 6:03

5 Answers 5

5

If what you mean is, "How do I connect via SSH from my website (to another server)", then you can do this with the PECL ssh2 library.

See: http://pecl.php.net/package/ssh2

Walkthrough (untested): http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/

Sign up to request clarification or add additional context in comments.

Comments

3

At first, there are no PuTTy commands. These are shell commands.

To run PHP script in shell, you need to use php-cli:

Comments

0

maybe you could use Command Line Scripting in PHP, it depends on what you want. http://php.net/manual/en/features.commandline.php

Comments

0

I am not sure but I think(correct me if I am wrong) that you want to click somewhere on a link on the webpage and open putty(on the user's computer) to connect to a server.

You can configure Putty to handle ssh:// links. How to do it you can find out here.

When that is configured all you have to do is to have a link similar to this:

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

Have in mind that this will work only on systems that are configured to handle the ssh:// link type

I hope that this answers your question.

Comments

0

This is how you use putty via PHP (not dependent of cli). Note that the passwords are not protected and that an interactive ssh session would be much more involved. However, HTTPS and mcrypt (if needing to store passwords and/or bash scripts) can make this a safe solution.

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

?>

1 Comment

also note that you need to use escapeshellcmd() to prevent command line injection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.