Jump to content

Shell


unidox

Recommended Posts

This is my code:

 

<?

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("localhost", 22))){
    echo "fail: unable to establish connection\n";
} else {
    // try to authenticate with username root, password secretpassword
    if(!ssh2_auth_password($con, "user", "password")) {
        echo "fail: unable to authenticate\n";
    } else {
        // allright, we're in!
        echo "okay: logged in...<br /><br />\n";
        
        function execshell ($command) {
        	global $con;
		if(!($stream = ssh2_exec($con, $command, "vt102")) ){
	        return false;
	    } else{
	        // collect returning data from command
	        stream_set_blocking( $stream, true );
	        $data = "";
	        while( $buf = fread($stream,4096) ){
	            $data .= $buf . "\n<br />";
	        }
	        return $data;
	        fclose($stream);
	    }
	}
        
        // execute a command
        if ($data = execshell("screen -X -S amitydm kill ; screen -dmS amitydm")) {
		echo $data;
	}
    }
}
?>

 

That works fine, it terminates, and creates the screen.

 

However, I need to execute the following commands in the screen:

 

cd /server/srv102/

./srcds_run -game cstrike

 

and then after those commands are run, I need the session to detach. How would I go about doing that? Thanks!

Link to comment
Share on other sites

So I would do this?:

 

// execute a command
        if ($data = execshell("screen -X -S amitydm kill ; screen -dmS amitydm")) {
		echo $data;
	}
	if ($data = execshell("screen -r amitydm /server/srv102/srcds_run -game cstrike")) {
		echo $data;
	}

 

?

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.