Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

STEP 2. Run remote command via SSH

Example 1) Rebooting remote windows machine

Below code connects to windows machine, and run "shutdown -r -f 0" command remotely.

...

Code Block
$ php reboot.php

...


Example 2) Get command result and display it

Below code connects to windows machine, run "dir" command remotely, and display its call results.

Code Block
languagephp
titledir.php
<?php // programmed by Chun Kang


$s = ssh2_connection( '192.168.10.8', 22);


if (ssh2_auth_password( $s, 'your_id', 'your_password')) {
	echo "Authentication successful!\n";
} else {
	die( "Authentication failed...\n");
}

$stream=ssh2_exec( $s, "dir");
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
echo stream_get_contents( $stream_out);

?>


Now you can run your command by web or command line like below

Code Block
$ php dir.php