简体   繁体   中英

starting a server with exec command

I'm trying to create something based on ssh2 connecting to my vps and then creating some files and running them with exec commands.

I already finished a bigger part which is creating a directory moving files from one directory to another now I need to do something like this

cd directory previously created

nohup ./samp03srv

but how can i do this with php ?

this is what i got so far

    $ssh = new SSH2($request->ip);

    if (!$ssh->login("root", "hpk2o321"))
    {
        return back()->withErrors("Authentication failed! Please check your credentials!");
    }

    $randomNum = substr(str_shuffle("0123456789abcdefghijklmnopqrstvwxyz"), 0, 11);

    $src = $request->path;
    $dest = "/home/servers/$randomNum";

    echo $ssh->exec("cp -r $src $dest");
    echo $ssh->exec('cd '.$dest.' &');
    echo $ssh->exec('chmod +x samp-npc samp03svr announce &');
    echo $ssh->exec('killall samp03svr &');
    echo $ssh->exec('nohup ./samp03svr &');

    echo $ssh->exec('pwd -P');

    $ssh->disconnect();

If you need more info please ask!

这是图像

From your code:

echo $ssh->exec("cp -r $src $dest");
echo $ssh->exec('cd '.$dest.' &');
echo $ssh->exec('chmod +x samp-npc samp03svr announce &');
echo $ssh->exec('killall samp03svr &');
echo $ssh->exec('nohup ./samp03svr &');

Try $ssh->exec('cd ' . $dest . '; chmod +x samp-npc samp03svr announce; killall samp03svr; nohup ./samp03svr &');

The problem with your successive exec's is the fact that the cd doesn't persist. A good way to think about exec (esp when it's not using a PTY) is you're SSH'ing into the box, running the command, and then closing the SSH session. If you do that you wouldn't expect a cd to persist from one session to the next, would you?

And technically that's not what phpseclib is doing. phpseclib is invoking the exec command as described in the SSH specs. A channel is opened, the command sent, the output returned, and then the SSH server, itself, closes the channel, effectively ending the session. The SSH connection remains open so you don't need to do key exchange or authentication again but the channel is closed.

The way PuTTY and OpenSSH work is they use a mode that I think is best understood as interactive mode. exec is not interactive. You can enable interactive mode in phpseclib with $ssh->read('#prompt#'); $ssh->write("cp -r $src $dest"); $ssh->read('#prompt#'); $ssh->write("cp -r $src $dest"); etc.

http://phpseclib.sourceforge.net/ssh/examples.html#chdir elaborates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM