Php Remote copy and command execution php Remote copy file and command execution in remote server when executing command, the module used is ssh2, all subsequent operations will be completed based on the ssh2 connection handle. 1. install the SSH2 Module 1.1 to install the required extension package wge php Remote copy and execute commands
Php Remote copy and command execution
The module used for php remote file copy and command execution on the remote server is ssh2, and all subsequent operations are completed based on the ssh2 connection handle.
1. install the SSH2 module1.1 install the required extension package
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 install
1.2 modify php configuration information
Add cd/etc/php5/cgivim php. ini: extension =/usr/lib/php5/20090626/ssh2.so ssh2.so is the module obtained when ssh2 is compiled, where the module is located.
Add cd/etc/php5/clivim php. ini: extension =/usr/lib/php5/20090626/ssh2.so ssh2.so is the module obtained when ssh2 is compiled, where the module is located.
1.3 Restart the web server
/etc/init.d/lighttpd restart
1.4 check whether ssh2 is loaded
[root@localhost ~]php -m | grep ssh2ssh2
2. connection application of the SSH2 module
There are two methods for SSH2 connection: user name and password, and ssh key form.
2.1 user name and password
$connection = ssh2_connect("192.168.6.222",22);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', 22, 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 on to the server 192.168.6.229 as root.
#ssh-keygen
If you have any questions, just press Enter.
The following public key is in ~ /. Ssh/id_rsa.pub
If you want to log on to 229 through key verification on 222, set id_rsa.pub cp of 229 to 222, log on to root manually, and then run:
cat id_rsa.pub >> ~/.ssh/authorized_keys
In this way, you can log on to ssh 229 without entering a password on ssh 192.168.6.229. The above php code can also be verified on 229.
3. specific application of the SSH2 module
After the verification is passed through SSH2, the resulting connector is $ connection.
3.1 Remote file copy
Remote server file copy to local:
BoolSsh2_scp_recv(Resource $ session, string $ remote_file, string $ local_file)
Ps: When receiving a file, the following file name can be blank, for example:
ssh2_scp_recv($connection, '/home/xiaozl/veno1.exe', '/home/xiaozl/')
Copy local files to remote servers
BoolSsh2_scp_send(Resource $ session, string $ local_file, string $ remote_file [, int $ create_mode])
Ps: When a file is sent, the file name following it cannot be blank, for example:
ssh2_scp_send($connection, '/home/xiaozl/package.xml', '/home/xiaozl/package.xml');
3.2 execute commands on the remote server and return values
ResourceSsh2_exec(Resource $ session, string $ command [, string $ pty [, array $ env [, int $ width [, int $ height [, int $ width_height_type])
Run the following command on 229:
$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);
Attachment: php code applied in 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/'); // when receiving the message, the following file name can be empty // $ flag = ssh2_scp_send ($ connection, '/home/xiaozl/package. xml ','/home/xiaozl/package. xml '); // The name of the file to be sent cannot be blank // echo $ flag; $ tcmd = "cd/home/veno/gateway/radiusclient;"; $ tcmd. = ". /nastool. sh get-status app = success "; $ 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 ('2017. 168.6.222 ', 22); 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" success "; stream_set_blocking ($ stream, true); echo stream_get_contents ($ stream); echo" ----------------------------------------------- "; echo"
";