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

时间:2017-09-29 16:03:07

标签: php ssh router 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('\n');
echo $ssh->exec("$pw");
$ssh->read(Username:');
echo $ssh->write($login.'\r');
$ssh->read('Password:');
echo $ssh->write($pw.'\r');

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

   <- 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                                               .

1 个答案:

答案 0 :(得分:2)

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

代替知道我说要做这样的事情:

$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\n");
//Now I need to connect to the telnet router using the same network login and password

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

在您的上一段代码段中,您正在执行\r - 而不是\n。我想那是sometimes necessary,但我谨慎地说\n将是你想要使用大多数的时间。

因为这个原因你的日志有点奇怪:

   <- 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!

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