PHP remote Copy and execute commands
PHP remote Copy and execute commands
PHP Remote copy files and when executing commands on a remote server, the module used is SSH2, and all subsequent operations are done according to the SSH2 connection handle.
1. Installation of the SSH2 module
1.1 Install the required expansion pack
wget Http://www.libssh2.org/download/libssh2-1.4.2.tar.gztar zxf libssh2-1.4.2.tar.gzcd libssh2-1.4.2./ Configure && make && make install
wget http://pecl.php.net/get/ssh2-0.11.3.tgzcd ssh2-0.11.3phpize (if the error command is not found, Apt-get install Php5-dev). Configure-with-ssh2 && make && make install
1.2 Modifying PHP configuration information
CD /etc/php5/cgivim php.ini Add-ons: extension=/usr/lib/php5/20090626/ssh2.so Ssh2.so is the module that is obtained when compiling SSH2, which is the position of the module.
CD /etc/php5/clivim php.ini Add-ons: extension=/usr/lib/php5/20090626/ssh2.so Ssh2.so is the module that is obtained when compiling SSH2, which is the position of the module.
1.3 Restarting the Web server
/ETC/INIT.D/LIGHTTPD restart
1.4 See if the SSH2 is loaded
[email protected] ~]php-m | grep SSH2SSH2
2. Connection application of the SSH2 module
SSH2 connections are available in two ways, namely, user name password, SSH key form.
2.1 User name and password
$connection = Ssh2_connect ("192.168.6.222"), if (Ssh2_auth_password ($connection, "Veno", "Ubuntu")) {echo] Authentication successful! ";} Else{die ("Authentication Failed ...");}
2.2 SSH Key
$connection = Ssh2_connect (' 192.168.6.222 ', $, array (' hostkey ' = ' ssh-rsa ')); if (Ssh2_auth_pubkey_file ($ Connection, ' root ', '/root/.ssh/id_rsa.pub ', '/root/.ssh/id_rsa ') { echo ' public Key authentication Successful\n ";} else { echo (' Public Key authentication Failed ');}
Ps: Key generation:
Log in as root on server 192.168.6.229
#ssh-keygen
What questions are you basically going to enter
After the public key is ~/.ssh/id_rsa.pub
If you want to log on to 222 via key verification in 229, put 229 of the public key id_rsa.pub CP to 222 above, manually log in to root first, and then run:
Cat Id_rsa.pub >> ~/.ssh/authorized_keys
This allows the SSH 192.168.6.229 above 229 to log in to 222 without entering a password. The above PHP code on the 229 can also be verified.
3. Specific application of the SSH2 module
After passing through SSH2 validation, the resulting connector is $connection
3.1 Implementing Remote Copy Files
Remote server file copy to Local:
BOOL Ssh2_scp_recv (Resource $session, String $remote _file, String $local _file)
Ps: When you receive a file, the following file name can be empty, such as:
SSH2_SCP_RECV ($connection, '/home/xiaozl/veno1.exe ', '/home/xiaozl/')
Local file copy to remote server
BOOL Ssh2_scp_send (Resource $session, String $local _file, String $remote _file [, int $create _mode])
Ps: When sending a file, the following file name cannot be empty, such as:
Ssh2_scp_send ($connection, '/home/xiaozl/package.xml ', '/home/xiaozl/package.xml ');
3.2 Execute the command on the remote server and take the return value
Resource ssh2_exec (Resource $session, String $command [, String $pty [, array $env [, int $width [, int $height [, int $width _height_ty PE]])
On the 229 above execute the command specific instance on 222:
Attachment: PHP code applied to the test
' Ssh-rsa '); if (Ssh2_auth_pubkey_file ($connection, ' root ', '/root/.ssh/id_rsa.pub ', '/root/.ssh/id_rsa ') {echo "Public Key authentication successful\n"; $flag =ssh2_scp_recv ($connection, '/home/xiaozl/veno1.exe ', '/home/xiaozl/'); Upon receipt, the following file name can be empty//$flag =ssh2_scp_send ($connection, '/home/xiaozl/package.xml ', '/home/xiaozl/package.xml '); When sending, the following file name cannot be empty//echo $flag; $tcmd = "cd/home/veno/gateway/radiusclient;"; $tcmd. = "./nastool.sh get-status app=2b1c5364-db39-c76d-842c-11d4a81d555d"; $stream = Ssh2_exec ($connection, $tcmd); Stream_set_blocking ($stream, true); echo stream_get_contents ($stream); } else {echo (' Public Key authentication Failed ');} echo "
"; $connection = Ssh2_connect (' 192.168.6.222 '); Ssh2_auth_password ($connection, ' Veno ', ' ******* '); $tcmd =" Cd/home /veno/gateway/radiusclient; "; $tcmd. = "./nastool.sh get-status app=2b1c5364-db39-c76d-842c-11d4a81d555d"; $stream = Ssh2_exec ($connection, $tcmd); echo "
"; echo "-----------------------------------------------"; Stream_set_blocking ($stream, true); echo stream_get_contents ($stream); echo "-----------------------------------------------"; echo "
";