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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download
The most popular open source editor
