Home > Backend Development > PHP Problem > How to use php to achieve remote connection

How to use php to achieve remote connection

藏色散人
Release: 2023-03-11 17:14:01
Original
3809 people have browsed it

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 )
Copy after login

Connect to an SSH server

2.Authentication

ssh2_auth_password ($session, $username, $password)
Copy after login

Use normal on SSH Password for authentication

or

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

Authenticate through public key

3. File transfer

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

Send files through scp protocol

ssh2_scp_recv ( resource $session , string $remote_file , string $local_file )
Copy after login

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)
Copy after login

Execute the command on the remote machine

5.Others

ssh2_fetch_stream ($channel, $streamid) {}
Copy after login

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 )
Copy after login

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);
 
}
Copy after login

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!

Related labels:
php
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template