php ssh 交换机,PHP-PHP登录SSH到中兴交换机

嘿嘿,分享个 telnet 类吧

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("usern");

echo $telnet->read_till("password: ");

$telnet->write("passwordn");

echo $telnet->read_till(">");

//echo $telnet->read_till(":> ");

$telnet->write("show arpn");

echo $telnet->read_till(">");

// TODO

echo $telnet->close();

小强要加油哦,

斑驳敬上

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值