Thusitha Sumanadasa
Thusitha Sumanadasa

Reputation: 1759

restart remote app using PHP SSH2

How can I restart if a directory 'xyz' exist in the remote SSH server using PHP-SSH2?

 <?php
 function fsmoke_ping($command_name1, $menu_name1, $title_name1, $host_ip1)
 {
    $connection = ssh2_connect('http://xxxx.com', 22);
    ssh2_auth_password($connection, 'username', 'password');     

    $sftp = ssh2_sftp($connection);
    $handle = file_exists('ssh2.sftp://' . $sftp . '/etc/xyz/devices/$menu_name1', 'w+')  or die('Cannot open file:  '.$menu_name1);


    $data = "++$menu_name1\nprobe = $command_name1\n menu = $menu_name1\n title = $title_name1\n host = $host_ip1";
    fwrite($handle, $data);
    fclose($handle);

    exec(`sudo /etc/init.d/xyz reload`);

}
?>

in this,

 exec(`sudo /etc/init.d/xyz reload`); 

is not working.so how can i restart it in here??

Upvotes: 1

Views: 1387

Answers (3)

message
message

Reputation: 4603

Obviously ssh2_exec. exec will execute statement on your local machine.

Upvotes: 1

neubert
neubert

Reputation: 16802

$connection = ssh2_connect('http://xxxx.com', 22);

Why do you have http:// there?

Also, there are a few gotcha's with sudo that can't easily be resolved with the PECL ssh2 extension. I'd recommend using phpseclib, a pure PHP SSH2 library, instead. There's a specific example on their website of how to use sudo:

http://phpseclib.sourceforge.net/ssh/examples.html#sudo

Upvotes: 1

Ali Mohammadi
Ali Mohammadi

Reputation: 1324

use /etc/init.d/xyz reload in newfile (by chmod 755) and append newfile nobody nobody in /etc/sudoers and use exec(sudo newfile reload);

Upvotes: 0

Related Questions