Jump to content

SSH2 ... executing a file


Frede

Recommended Posts

well i got this code to execute a file called samp03svr. My problem is just i cant get it to execute from the PHP script.
i have tried using: first "cd samp03" and then "nohup ./samp03svr &" but that didnt work.
Then i tried "nohup ./samp03/samp03svr &" but that didnt help either
Then i tried "cd samp03 & nohup ./samp03svr &"
But i cant get it to work it gives no responds or errors... nothing happends... 
i tried with a command like "find" to see if i could get any respond and that worked fine.. so the connection is fine.
<?php
if($_POST['pass'] == '')
{
echo('<html><head><form name="input" action="startserver.php" method="post">password: <input type="text" name="pass"><input type="submit" value="Submit"></form></head></html>');
}
else
{
if($_POST['pass'] == 'lagerne')
{
$connection = ssh2_connect('localhost', 22);


if(ssh2_auth_password($connection, 'privateuser', 'privatepassword'))
{
echo("EXECUTING samp03svr...<br />");
$stdout_stream = ssh2_exec($connection, "cd samp03 & nohup ./samp03svr &");
sleep(1);
$stderr_stream = ssh2_fetch_stream($stdout_stream, SSH2_STREAM_STDERR);
echo("executed!");
echo "<br />------[ERRORS]------<br />";
while($line = fgets($stderr_stream)) { flush(); echo $line."<br />"; }
echo "------[RESPOND]------<br />";
while($line = fgets($stdout_stream)) { flush(); echo $line."<br />";}
fclose($stdout_stream);
}
else echo("[ERROR]: A error has occured and therefore you cant login(RESPOND WAS: FALSE/".$login.")<br />[SOLUTION]: try pressing F5<br />[SOLUTION]: Contact me: <a href='mailto://frederik.v@live.dk'>frederik.v@live.dk</a><br /><br />[INFO]: Connection:".$connection."");


}
else echo("WRONG PASSWORD!!!");
}
?>
Edited by Frede
Link to comment
Share on other sites

sure: -rwxr-xrwx 1 samp samp 1354628 Jul 13 2012 ./samp03/samp03svr

 

Then this path is wrong - cd samp03 & nohup.

 

Also, if you have to execute a file inside directory executing it from external one you don't have to apply "./"  only the path.

 

Try,

$stdout_stream = ssh2_exec($connection, "cd ~/samp03/samp03svr &");

Or if you want to see the content of the file:

$stdout_stream = ssh2_exec($connection, "vim ~/samp03/samp03svr &");
Edited by jazzman1
Link to comment
Share on other sites

you are right, i can only execute the file from the samp03 folder... but "cd ~/samp03/samp03svr &"  and "vim ~/samp03/samp03svr &" didnt work. Normally when i need to execute the file, i just do "cd samp03" and then "nohub ./samp03svr". I cant do "nohub samp03svr &" then it will announce a error(file/folder not found).

 

the folder is placed in the root folder

Edited by Frede
Link to comment
Share on other sites

Good for you ;)

 

You can use a "&" symbol only if you want see the content of file with some GUI editor like "gedit" for instance, running the view outside of a terminal window.

 

Example:

[jazz@centos-box ~]$ gedit some_file &
Link to comment
Share on other sites

You can use a "&" symbol only if you want see the content of file with some GUI editor like "gedit" for instance, running the view outside of a terminal window.

...Or if you want it to run in the background. Which is what that does.
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.