在路由器上使用ssh2和telnet命令的phpseclib

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在路由器上使用ssh2和telnet命令的phpseclib相关的知识,希望对你有一定的参考价值。

我正在尝试访问网络中的Cisco路由器,但我没有使用phpseclib库获取它

$login = "guest";
$pw = "123456";
$ssh = new Net_SSH2("11.222.333.44");
if(!$ssh->login($login, $pw)){ exit("FAILED !!!!")}
//That I am connected to the network

$ssh->exec("telnet 555.66.77.8");
//Now I need to connect to the telnet router using the same network login and password

echo $ssh->exec("$login");
$ssh->exec('cd /');
echo $ssh->exec("$pw");

echo $ssh->getLog();

已经尝试过:

echo $ssh->exec("$login");
$ssh->exec('cd /');
echo $ssh->exec("$pw");

echo $ssh->exec("$login");
$ssh->exec('
');
echo $ssh->exec("$pw");

$ssh->read(Username:');
echo $ssh->write($login.'
');
$ssh->read('Password:');
echo $ssh->write($pw.'
');

我没有成功,希望得到帮助,请按照日志:

   <- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0936, network: 0.0936s)
00000000  00:00:00:00:00:00:00:0c:0d:0a:55:73:65:72:6e:61  ..........Userna
00000010  6d:65:3a:20                                      me: 

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 30.6541, network: 30.6541s)
00000000  00:00:00:00:00:00:00:1f:0d:0a:25:20:55:73:65:72  ..........% User
00000010  6e:61:6d:65:3a:20:20:74:69:6d:65:6f:75:74:20:65  name:  timeout e
00000020  78:70:69:72:65:64:21                             xpired!

<- NET_SSH2_MSG_CHANNEL_REQUEST (since last: 2.0904, network: 2.0904s)
00000000  00:00:00:00:00:00:00:0b:65:78:69:74:2d:73:74:61  ........exit-sta
00000010  74:75:73:00:00:00:00:01                          tus.....

-> NET_SSH2_MSG_CHANNEL_EOF (since last: 0, network: 0s)
00000000  00:00:00:00                                      ....

-> NET_SSH2_MSG_CHANNEL_CLOSE (since last: 0, network: 0s)
00000000  00:00:00:00                                      ....

<- NET_SSH2_MSG_CHANNEL_EXTENDED_DATA (since last: 0, network: 0s)
00000000  00:00:00:00:00:00:00:01:00:00:00:24:43:6f:6e:6e  ...........$Conn
00000010  65:63:74:69:6f:6e:20:63:6c:6f:73:65:64:20:62:79  ection closed by
00000020  20:66:6f:72:65:69:67:6e:20:68:6f:73:74:2e:0d:0a   foreign host...

<- NET_SSH2_MSG_CHANNEL_EOF (since last: 0, network: 0s)
00000000  00:00:00:00                                      ....

<- NET_SSH2_MSG_CHANNEL_CLOSE (since last: 0, network: 0s)
00000000  00:00:00:00                                      ....

-> NET_SSH2_MSG_CHANNEL_OPEN (since last: 0, network: 0s)
00000000  00:00:00:07:73:65:73:73:69:6f:6e:00:00:00:01:7f  ....session.....
00000010  ff:ff:ff:00:00:40:00                             .....@.

<- NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION (since last: 0.0624, network: 0.0624s)
00000000  00:00:00:01:00:00:00:01:00:00:00:00:00:00:80:00  ................

-> NET_SSH2_MSG_CHANNEL_REQUEST (since last: 0, network: 0s)
00000000  00:00:00:01:00:00:00:07:70:74:79:2d:72:65:71:01  ........pty-req.
00000010  00:00:00:05:76:74:31:30:30:00:00:00:50:00:00:00  ....vt100...P...
00000020  18:00:00:00:00:00:00:00:00:00:00:00:01:00        ..............

<- NET_SSH2_MSG_CHANNEL_SUCCESS (since last: 0.0156, network: 0.0156s)
00000000  00:00:00:01                                      ....

-> NET_SSH2_MSG_CHANNEL_REQUEST (since last: 0, network: 0s)
00000000  00:00:00:01:00:00:00:05:73:68:65:6c:6c:01        ........shell.

<- NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST (since last: 0.0156, network: 0.0156s)
00000000  00:00:00:01:00:20:00:00                          ..... ..

<- NET_SSH2_MSG_CHANNEL_SUCCESS (since last: 0, network: 0s)
00000000  00:00:00:01                                      ....

-> NET_SSH2_MSG_CHANNEL_DATA (since last: 0, network: 0s)
00000000  00:00:00:01:00:00:00:09:6f:69:33:33:34:34:30:36  ........bss1991
00000010  0a                                               .
答案

您提供了4x不同的代码片段,并且您用于生成日志文件的片段并不是很明显。

代替知道我会说这样做:

$login = "guest";
$pw = "123456";
$ssh = new Net_SSH2("11.222.333.44");
if(!$ssh->login($login, $pw)){ exit("FAILED !!!!")}
//That I am connected to the network

$ssh->read('[ssh prompt]');
$ssh->write("telnet 555.66.77.8
");
//Now I need to connect to the telnet router using the same network login and password

$ssh->read('Username:');
$ssh->write("$login
");
$ssh->read('Password:');
$ssh->write("$pw
");
echo $ssh->read();

在你的最后一个代码片段中,你正在做 - 而不是 。我想这是sometimes necessary,但我敢打赌, 将是你想要在大多数时间使用的东西。

你的日志有点奇怪,因为:

   <- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0936, network: 0.0936s)
00000000  00:00:00:00:00:00:00:0c:0d:0a:55:73:65:72:6e:61  ..........Userna
00000010  6d:65:3a:20                                      me: 

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 30.6541, network: 30.6541s)
00000000  00:00:00:00:00:00:00:1f:0d:0a:25:20:55:73:65:72  ..........% User
00000010  6e:61:6d:65:3a:20:20:74:69:6d:65:6f:75:74:20:65  name:  timeout e
00000020  78:70:69:72:65:64:21                             xpired!

这就像你得到了“用户名:”,但你期待别的东西,所以下一条消息需要30秒才能发送。在你的第四个代码片段中你正在做$ssh->read(Username:');(我注意到它有一个语法错误)。如果我正确地推断出您的完整代码可能正在做什么,我希望立即返回,但它似乎不是来自您的日志,这使我认为我的推断可能不完全正确。

以上是关于在路由器上使用ssh2和telnet命令的phpseclib的主要内容,如果未能解决你的问题,请参考以下文章

securecrt怎么设置自动登录

思科路由器如何开telnet

telnet服务怎么开启 telnet服务开启方法

H3C路由器V7版本关于认证的一些知识点

怎么开启路由器上的telnet端口

H3C 路由器 配置将debug信息 输出到telnet上