Jump to content

SSH


unidox

Recommended Posts

I am making an ssh connection with this script:

 

<?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("server.server.com", 22))){
    echo "fail: unable to establish connection\n";
} else {
    // try to authenticate with username root, password secretpassword
    if(!ssh2_auth_password($con, "root", "password")) {
        echo "fail: unable to authenticate\n";
    } else {
        // allright, we're in!
        echo "okay: logged in...\n";
        
        // execute a command
        if(!($stream = ssh2_exec($con, "screen -list" )) ){
            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 .= "<br />" . $buf;
            }
            echo $data;
            fclose($stream);
        }
    }
}
?>

 

That code works, however, I am trying to kill a screen "screen -X -S server kill" which kills the server screen session, and then I am trying to create another screen "screen -S server", which creates the server session for screen. However, I was trying to add a command to change directory and run a script "cd /home/ | script", but that command doesnt run. THis is the whole command:

 

screen -X -S server kill | screen -S server | cd /home/ | script

 

It kills and creates the screen, however it never changes directory or starts the script. What am I doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/165555-ssh/
Share on other sites

If you want to run on eprogram at a time use ; to seperate them. Theres no need to pipe the output of one to the imput of the next in this case.

 

Also, if you only want the next command to run if the first command succeeds use &&, eg;

 

screen -X -S server kill && screen -S server && cd /home/ |&& script

 

ps: Have you tried actually logging in and creating a screen session? Some screen installations show a welcome message when a session is first started by default. You then need to hit enter or space to continue executing any scripts.

 

Link to comment
https://forums.phpfreaks.com/topic/165555-ssh/#findComment-873319
Share on other sites

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.