PHP登录SSH到中兴交换机


不知道有没有人试过中兴交换机的SSH登录,我发现很奇怪,使用标准的Linnx Bash登录都很困难,使用xshell之类的智能工具倒是可以正常的登录,当使用ssh加上完全接受对方证书的时候的确能使用Bash命令下登录,但是PHP却不能做了,最后使用的telnet登陆管理的交换,但是telnet特别慢,执行一个操作得十几秒的样子~~

我是用的参数是

   
  ssh - y # Always accept remote host key if unknown
 

比较特殊的问题,希望有经验的能够分享经验~~

PS:我知道PHP有ssh的库可以用,但是那个只能操作标准的SSH,中兴交换机的有没有人试过如何的操作~~~ 目前我使用php使用telnet方式成功了(哎,也算不上标准的telnet,就是socket发送读取,还是一个字符一个字符的,效率慢的要死)

php ssh

q2a5a5 12 years, 7 months ago

嘿嘿,分享个 telnet 类吧

   
  <?php
  
error_reporting(-1);

class Telnet {
var $sock = NULL;

function telnet($host,$port) {
$this->sock = fsockopen($host,$port);
socket_set_timeout($this->sock,2,0);
}

function close() {
if ($this->sock) fclose($this->sock);
$this->sock = NULL;
}

function write($buffer) {
$buffer = str_replace(chr(255),chr(255).chr(255),$buffer);
fwrite($this->sock,$buffer);
}

function getc() {
return fgetc($this->sock);
}

function read_till($what) {
$buf = '';
while (1) {
$IAC = chr(255);

$DONT = chr(254);
$DO = chr(253);

$WONT = chr(252);
$WILL = chr(251);

$theNULL = chr(0);

$c = $this->getc();

if ($c === false) return $buf;
if ($c == $theNULL) {
continue;
}

// if ($c == "1") {
// continue;
// }

if ($c != $IAC) {
$buf .= $c;

if ($what == (substr($buf,strlen($buf)-strlen($what)))) {
return $buf;
}
else {
continue;
}
}

$c = $this->getc();

if ($c == $IAC) {
$buf .= $c;
}
else if (($c == $DO) || ($c == $DONT)) {
$opt = $this->getc();
// echo "we wont ".ord($opt)."\n";
fwrite($this->sock,$IAC.$WONT.$opt);
}
elseif (($c == $WILL) || ($c == $WONT)) {
$opt = $this->getc();
// echo "we dont ".ord($opt)."\n";
fwrite($this->sock,$IAC.$DONT.$opt);
}
else {
// echo "where are we? c=".ord($c)."\n";
}
}
}
}

$telnet = new telnet("ip", $port);
echo $telnet->read_till("login: ");
$telnet->write("user\n");
echo $telnet->read_till("password: ");
$telnet->write("password\n");
echo $telnet->read_till(">");
//echo $telnet->read_till(":> ");
$telnet->write("show arp\n");
echo $telnet->read_till(">");
// TODO

echo $telnet->close();

小强要加油哦,
斑驳敬上

右代宫莉莎 answered 12 years, 7 months ago

Your Answer