Jump to content


PHP and SSH how?


4 replies to this topic

#1 Magnetronkip

    Young Padawan

  • Members
  • Pip
  • 26 posts

Posted 08 November 2007 - 02:37 PM

Hi guys,

I am searching away to connect from

Server A to server B

and i want to let php execute some commands, like restarting apache etc, i know that it is possible with php, but how i don't now.

Just a tutorial or a little script on how you can restart apache through php would help a lot!

Thanks in advanced

Greetz,

Dion

#2 U1

    Young Padawan

  • Members
  • Pip
  • 245 posts

Posted 08 November 2007 - 08:22 PM

Something like this should do it:

<?php

$submit = $_POST['submit'];

if($submit == "Restart") {

$message = '<p>Apache is being restarted</p>';
exec('/etc/init.d/httpd graceful');
$message .= '<p>Apache was restarted</p>';

}
echo '<html><head><title>Apache Restart</title></head><body>';
echo $message;
echo '<form action="" method="post">
<input type="submit" name="submit" value="Restart"></form>';

?>

or install sudo and add

www-data ALL=(ALL) NOPASSWD: /usr/sbin/apachectl

to /etc/sudoers.

then add this to your page:

passthru("sudo apachectl graceful", $output_var);


#3 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 09 November 2007 - 12:19 AM

itypeos, it sounds to me like this guy may be farily new to the subject. Since i barely understand what you're saying, i'd recommend possibly offering a better explaination...

Secure Shell2 <-- That is the only reference i know of for PHP/SSH stuff.

I've never used/looked into the subject at all myself, so that is all i can offer you. Personally though, i know php wasn't built to preform operations on a remote server (don't get me wrong, it can, but it's functionality is limited). That link i provided should be a bit more enlightening though.

#4 U1

    Young Padawan

  • Members
  • Pip
  • 245 posts

Posted 09 November 2007 - 06:16 AM

My bad, ok well this should be a bit easier.

Make a file called httpd_reload.c and add this:

/***********************************************************
 * make sure to chmod +s the binary after compiling:
 * cc -o httpd_reload httpd_reload.c; chmod +s httpd_reload
 ***********************************************************/

#include <stdio.h>
#include <stdlib.h>

int main() {
  if (!setuid(geteuid())) {
	system("/bin/echo '/sbin/service httpd reload > /dev/null 2>&1' | /usr/bin/at now");
  } else {
	printf("Couldn't set UID to effective UID\n");
	return 1;
  }
  return 0;
}

Put this file anywhere you want then add this to anywhere you want but just make sure you change the patch below to where yo uput the httpd_reload.c:

PHP code:
exec('/path/to/httpd_reload');

Edited by itypeos, 09 November 2007 - 06:18 AM.


#5 Magnetronkip

    Young Padawan

  • Members
  • Pip
  • 26 posts

Posted 09 November 2007 - 09:22 AM

I need to use it from a other server where my control panel is on

i got something like this

Quote

<?php

$script1 = "cd /vent; ./vent.sh;";

$connection = ssh2_connect('server', '22');
ssh2_auth_password($connection, 'user', 'pass');

$stream = ssh2_exec($connection, $script1);
?>

only it doesnt work, why i should'nt know gonna check for more.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users