Php Remote copy and Command Execution

Source: Internet
Author: User
Tags public key authentication failed

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 Module
1.1 install the required extension package

[Plain]
Wget
Tar zxf libssh2-1.4.2.tar.gz
Cd libssh2-1.4.2
./Configure & make install

Wget
Tar zxf libssh2-1.4.2.tar.gz
Cd libssh2-1.4.2
./Configure & make install

[Plain]
Wget
Cd ssh2-0.11.3
Phpize (if the error command is not found, apt-get install php5-dev)
./Configure-with-ssh2 & make install

Wget
Cd ssh2-0.11.3
Phpize (if the error command is not found, apt-get install php5-dev)
./Configure-with-ssh2 & make install
1.2 modify php configuration information

[Plain]
Cd/etc/php5/cgi
Vim php. ini
Add item: extension =/usr/lib/php5/20090626/ssh2.so
Ssh2.so is the module obtained when ssh2 is compiled. The above is the module position.

Cd/etc/php5/cgi
Vim php. ini
Add item: extension =/usr/lib/php5/20090626/ssh2.so
Ssh2.so is the module obtained when ssh2 is compiled. The above is the module position.

[Plain]
Cd/etc/php5/cli
Vim php. ini
Add item: extension =/usr/lib/php5/20090626/ssh2.so
Ssh2.so is the module obtained when ssh2 is compiled. The above is the module position.

Cd/etc/php5/cli
Vim php. ini
Add item: extension =/usr/lib/php5/20090626/ssh2.so
Ssh2.so is the module obtained when ssh2 is compiled. The above is the module position.
1.3 restart the web Server

[Plain]
/Etc/init. d/lighttpd restart

/Etc/init. d/lighttpd restart
1.4 check whether ssh2 is loaded

[Plain]
[Root @ localhost ~] Php-m | grep ssh2
Ssh2

[Root @ localhost ~] Php-m | grep ssh2
Connection application of ssh22. SSH2 Module
There are two methods for SSH2 connection: user name and password, and ssh key form.

2.1 user name and password

[Php]
$ Connection = ssh2_connect ("192.168.6.222", 22 );
If (ssh2_auth_password ($ connection, "veno", "ubuntu "))
{
Echo "Authentication Successful! ";
} Else {
Die ("Authentication Failed ...");
}

$ 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

[Php]
$ 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 ');
}

$ 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.


[Plain]
# Ssh-keygen

# 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:


[Plain]
Cat id_rsa.pub >> ~ /. Ssh/authorized_keys

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:

Bool ssh2_scp_recv (resource $ session, string $ remote_file, string $ local_file)

Ps: when receiving a file, the following file name can be blank, for example:


[Php]
Ssh2_scp_recv ($ connection, '/home/xiaozl/veno1.exe', '/home/xiaozl /')

Ssh2_scp_recv ($ connection, '/home/xiaozl/veno1.exe', '/home/xiaozl /')

 

Copy local files to remote servers

Bool ssh2_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:

[Php]
Ssh2_scp_send ($ connection, '/home/xiaozl/package. xml','/home/xiaozl/package. xml ');

Ssh2_scp_send ($ connection, '/home/xiaozl/package. xml','/home/xiaozl/package. xml ');
 

3.2 execute commands on the remote server and return values
Resource ssh2_exec (resource $ session, string $ command [, string $ pty [, array $ env [, int $ width [, int $ height [, int $ width_height_type])

Run the following command on 229:


[Php]
$ 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 );

$ 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
[Php]
<? Php
Echo "access here ";
$ 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 ";
// $ Flag = ssh2_scp_recv ($ connection, '/home/xiaozl/veno1.exe', '/home/xiaozl /');
// The file name can be blank when receiving
// $ Flag = ssh2_scp_send ($ connection, '/home/xiaozl/package. xml','/home/xiaozl/package. xml ');
// The file name after sending cannot be blank
// 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 "<br/> ";
$ Connection = ssh2_connect ('192. 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 "<br/> ";
Echo "-----------------------------------------------";
Stream_set_blocking ($ stream, true );
Echo stream_get_contents ($ stream );
Echo "-----------------------------------------------";
Echo "<br/> ";

<? Php
Echo "access here ";
$ 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 ";
// $ Flag = ssh2_scp_recv ($ connection, '/home/xiaozl/veno1.exe', '/home/xiaozl /');
// The file name can be blank when receiving
// $ Flag = ssh2_scp_send ($ connection, '/home/xiaozl/package. xml','/home/xiaozl/package. xml ');
// The file name after sending cannot be blank
// 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 "<br/> ";
$ Connection = ssh2_connect ('192. 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 "<br/> ";
Echo "-----------------------------------------------";
Stream_set_blocking ($ stream, true );
Echo stream_get_contents ($ stream );
Echo "-----------------------------------------------";
Echo "<br/> ";


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.