PHP non-blocking SSH client instance

Source: Internet
Author: User
Tags chr fread public key authentication failed stdin strlen ssh

Value:

    • Based on command, the problem of network latency and packet loss is resolved to the maximum extent.
    • Windows and Linux test pass.

Insufficient:

    • No auto-complement function
    • No other features like SFTP and SCP
    • No color and bold display
    • It's not perfect in individual cases.
    • Because now basically do not use it, so for the time being no improvement.

Linux Run effect

Running effects under Windows


Because it's a class in the framework, individual generic functions (such as debug_print ()) need to be provided by themselves, and I don't rewrite them here.

The code is as follows Copy Code

<?php
Class fssh{
Private $conn;
Private $shell;

/**
* key=string Password Authentication, Key=array (' Pub ' =>, ' pri ' =>, ' type ' =>, ' phrase ' => ') key authentication
* Key authentication type is divided into two types: Ssh-rsa,ssh-dss
* $host [addr]=string address, $host [' FP ']=array () server fingerprint
*/
function __construct ($host, $user, $key) {
if (Empty ($host [' addr '])) {
Debug_print (' Host cant ' t be empty ', e_user_error);
}
if (Empty ($host [' FP '])) {
Debug_print (' Finger print is not specified ', e_user_error);
}
$this->stdin=fopen (' Php://stdin ', ' R ');
$this->stdout=fopen (' Php://stdout ', ' W ');
if (False!==strpos ($host [' addr '], ': ')) {
$temp =explode (': ', $host [' addr ']);
$host [' addr ']= $temp [0];
$port = $temp [1];
}else{
$port = 22;
}
if (is_string ($key) | | | empty ($key [' type '])) {
$methods =null;
}else{
$methods =array (' hostkey ' => $key [' type ']);
}
$conn =ssh2_connect ($host [' addr '], $port, $methods, Array (' Disconnect ' =>array ($this, ' disconnect '));
$FP =ssh2_fingerprint ($conn, SSH2_FINGERPRINT_MD5);
$success =false;
$fpOK =false;
if (In_array ($fp, $host [' FP '])) {
$fpOK =true;
}else{
Fwrite ($this->stdout, "$fpnIs fingerprint OK?" ( y/n) ");
$input =strtolower (Stream_get_line ($this->stdin,1));
if ($input = = ' Y ') {
$fpOK =true;
}else{
$fpOK =false;
}
}
if ($fpOK) {
if (Is_array ($key)) {
if (Ssh2_auth_pubkey_file ($conn, $user, $key [' Pub '], $key [' pri '], $key [' Phrase '])} {
$success =true;
}else{
Debug_print (' Public Key authentication Failed ', e_user_error);
}
}elseif (is_string ($key)) {
if (Ssh2_auth_password ($conn, $user, $key)) {
$success =true;
}else{
Debug_print (' Password authentication Failed ', e_user_error);
}
}
}else{
Debug_print (' Fingerprint is invalid ', e_user_error);
}
if ($success) {
$this->conn= $conn;
$this->shell=ssh2_shell ($conn, null,null,1024);
}
return $success;
}

function Shell () {
Last command
$last = ';
End Shell first, then end while
$signalTerminate =false;
while (true) {
$cmd = $this->fread ($this->stdin);
$out =stream_get_contents ($this->shell,1024);
if (!empty ($out) and!empty ($last)) {
$l 1=strlen ($out);
$l 2=strlen ($last);
$l = $l 1> $l 2 $l 2: $l 1;
$last =substr ($last, $l);
$out =substr ($out, $l);
}
Echo LTrim ($out);
if ($signalTerminate) {
Break
}
if (Trim ($cmd), Array (' exit ')) {In_array
$signalTerminate =true;
}
if (!empty ($cmd)) {
$last = $cmd;
Fwrite ($this->shell, $cmd);
}
}
}

There is no other way to solve the problem of reading Windows command line.
Private Function Fread ($FD) {
static $data = ';
$read = Array ($FD);
$write = Array ();
$except = Array ();
$result = Stream_select ($read, $write, $except, 0,1000);
if ($result = = False)
Debug_print (' Stream_select failed ', e_user_error);
if ($result!== 0) {
$c = Stream_get_line ($FD, 1);
if ($c!=CHR (13))
$data. = $c;
if ($c ==CHR (10)) {
$t = $data;
$data = ';
return $t;
}
}
}

function __destruct () {
Fclose ($this->stdin);
Fclose ($this->stdout);
$this->disconnect ();
}

Private Function Disconnect () {
if (Is_resource ($this->conn)) {
unset ($this->conn);
Fclose ($this->shell);
}
}
}


Demo

The code is as follows Copy Code

$ssh =new fssh (' addr ' => ' x.x.x.x:22 ', ' FP ' =>array (') '), ' Tunnel ', Array (' Pub ' => ' e:identity.pub '), ' Pri ' => ' e:identity ', ' type ' => ' Ssh-rsa ');
$ssh =new fssh (Array (' addr ' => ' 192.168.2.205 ', ' FP ' =>array (' 54ecc700b844dcf0d40554a56c57c01e ')), ' Root ', ' 123456 ');
$ssh->shell ();

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.