ホームページ  >  記事  >  バックエンド開発  >  PHPを使用してリモート接続を実現する方法

PHPを使用してリモート接続を実現する方法

藏色散人
藏色散人オリジナル
2021-07-19 09:49:043572ブラウズ

php を使用してリモート接続を実現する方法: まず SSH2 モジュールをインストールし、次に「ssh2_connect ($host, $port = null, $methods = nullarray, $callbacks = nullarray)」メソッドを通じて接続します。

PHPを使用してリモート接続を実現する方法

この記事の動作環境: Windows7 システム、PHP7.1 バージョン、DELL G3 コンピューター

php を使用してリモート接続を実現する方法?

phpでリモート操作を実現

phpを使用してリモート操作を行う場合、SSH2モジュールをインストールする必要があります。 SSH2 モジュールで使用されるいくつかの関数について簡単に記録してみましょう。

一般的な方法

1.接続

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

SSHサーバーに接続

2.認証

ssh2_auth_password ($session, $username, $password)

SSHパスワードで通常を使用認証用

or

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

公開鍵による認証

3. ファイル転送

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

scpプロトコル経由でファイルを送信

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

取得scp プロトコルを介してファイルを取得します

4. コマンドを実行します

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

リモート マシンでコマンドを実行します

5.その他

ssh2_fetch_stream ($channel, $streamid) {}

拡張されたデータ ストリームを取得します。一般的に使用される $streamid 定義は次のとおりです。

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

ストリームをブロッキング/非ブロッキング状態に設定します。 $mode が true の場合はブロッキングであり、$mode が false の場合は非ブロッキングです。

簡単なアプリケーション

//建立连接
$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);
 
}

推奨学習: 「PHP ビデオ チュートリアル

以上がPHPを使用してリモート接続を実現する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。