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
Share on other sites

idk, its always worked for me. What I am trying to do is:

 

1- Create a screen session named server

2- Change directory to scripts dir

3- Run Script

 

The need for a screen is important, so that I can kill the screen which kills the script if I need to. Any ideas?

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.