Home  >  Article  >  Backend Development  >  How to use php to achieve remote connection

How to use php to achieve remote connection

藏色散人
藏色散人Original
2021-07-19 09:49:043620browse

How to use php to achieve remote connection: first install the SSH2 module; then connect through the "ssh2_connect ($host, $port = null, $methods = nullarray, $callbacks = nullarray)" method.

How to use php to achieve remote connection

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to use php to achieve remote connection?

php realizes remote operation

When using php for remote operation, you need to install the SSH2 module. Let’s make a simple record about several functions used in the SSH2 module.

Common methods

1. Connection

ssh2_connect ($host, $port = null, $methods = nullarray , $callbacks = nullarray )

Connect to an SSH server

2.Authentication

ssh2_auth_password ($session, $username, $password)

Use normal on SSH Password for authentication

or

ssh2_auth_pubkey_file ($session, $username, $pubkeyfile, $privkeyfile, $passphrase = null)

Authenticate through public key

3. File transfer

ssh2_scp_send ( resource $session , string $local_file , string $remote_file [, int $create_mode = 0644 ] )

Send files through scp protocol

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

Obtain files through scp protocol

4. Execute the command

ssh2_exec ($session, $command, $pty = null, $env = nullarray , $width = null, $height = null, $width_height_type = null)

Execute the command on the remote machine

5.Others

ssh2_fetch_stream ($channel, $streamid) {}

obtain the expanded data stream . Commonly used $streamid definitions are:

define ('SSH2_STREAM_STDIO', 0);
define ('SSH2_STREAM_STDERR', 1);
stream_set_blocking ( resource $stream , bool $mode )

Set the stream to blocking/non-blocking state. When $mode is true, it is blocking; when $mode is false, it is non-blocking.

Simple Application

//建立连接
$connection = ssh2_connect($host, (int)$port);
if (!$connection) {
 
... ...
 
}
 
//进行认证
 
if (!ssh2_auth_password($connection, $user, $password)) {
 
... ...
 
}
 
//发送文件
if (!ssh2_scp_send($connection, $sourceFile, $targetFile, 0644)) {
 
... ...
 
}else{
 
$stream = ssh2_exec($connection, "stat /tmp/targetFile 2>&1");
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
 
// Enable blocking for both streams
stream_set_blocking($errorStream, true);
stream_set_blocking($stream, true);
 
echo stream_get_contents($stream);
 
// Close the streams
fclose($errorStream);
fclose($stream);
 
}

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to use php to achieve remote connection. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn