Jump to content

[Beginner] How would i do a simple ssh connection?.


ChrisMartino

Recommended Posts

Hey there,

 

I'm totally new to php, I know a little bit (I'm talking really basic stuff), And i would like to make a script that connects to my dedicated server via SSH then creates a user on the box, Could someone please show me, Or code for me so i could look at it please?, Thanks for taking the time to read this,

 

Chris

(PHP newbie)

Would this work, I just want it to echo if it connects.

 

<php?
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
if(!($con = ssh2_connect("94.23.204.219", 22))){
    echo "fail: unable to establish connection\n";
} else {
    // try to authenticate with username root, password secretpassword
    if(!ssh2_auth_password($con, "root", "somepassword")) {
        echo "fail: unable to authenticate\n";
    } else {
        // allright, we're in!
        echo "okay: logged in...\n";

    }
}
?>

Here a interesting read, apparently ssh and ssh2 are one off the same, and run from the same dll file so there no,reason your code shouldn't work.

 

Here have a look

http://felipecruz.com/blog_using-ssh2-and-php.php

 

execute way ......

<?php
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
// log in at server1.example.com on port 22
if(!($con = ssh2_connect("server1.example.com", 22))){
    echo "fail: unable to establish connection\n";
} else {
    // try to authenticate with username root, password secretpassword
    if(!ssh2_auth_password($con, "root", "secretpassword")) {
        echo "fail: unable to authenticate\n";
    } else {
        // allright, we're in!
        echo "okay: logged in...\n";
        
        // execute a command
        if(!($stream = ssh2_exec($con, "ls -al" )) ){
            echo "fail: unable to execute command\n";
        } else{
            // collect returning data from command
            stream_set_blocking( $stream, true );
            $data = "";
            while( $buf = fread($stream,4096) ){
                $data .= $buf;
            }
            fclose($stream);
        }
    }
}
?>

 

shell way

<?php
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
// log in at server1.example.com on port 22
if(!($con = ssh2_connect("server1.example.com", 22))){
    echo "fail: unable to establish connection\n";
} else {
    // try to authenticate with username root, password secretpassword
    if(!ssh2_auth_password($con, "root", "secretpassword")) {
        echo "fail: unable to authenticate\n";
    } else {
        // allright, we're in!
        echo "okay: logged in...\n";
        
        // create a shell
        if(!($shell = ssh2_shell($con, 'vt102', null, 80, 40, SSH2_TERM_UNIT_CHARS))){
            echo "fail: unable to establish shell\n";
        } else{
            stream_set_blocking( $shell, true );
            // send a command 
            fwrite($shell,"ls -al\n");
            sleep(1);
            
            // & collect returning data
            $data = "";
            while( $buf = fread($shell,4096) ){
                $data .= $buf;
            }
            fclose($shell);
        }
    }
}
?>

 

Please also read this link it way interesting, the bottom of the page might help you.

big boy read this bro

 

Check the following:

 

- Did you follow every step of the prerequisites & installation how to in this article?

- On the serverside, 'PasswordAuthentication yes' must be enabled in the sshd_config. Default is yes on most servers, but in some cases you will have to turn this on yourself by making sure the following lines are in place in the file: /etc/ssh/sshd_config:

 

# Change to yes to enable tunnelled clear text passwords

PasswordAuthentication yes

 

If you've made any changes, ssh needs a restart

 

/etc/init.d/ssh restart

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.