2

I'm trying to access a Cisco router within a network but I'm not getting it using phpseclib library

$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();

already tried:

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');

I'm not succeeding and would like help, follow the log:

   <- 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                                               .
neubert
  • 15,947
  • 24
  • 120
  • 212

1 Answers1

2

You've provided 4x different code snippets and it's not immediately obvious which one you used to produce your log file.

In lieu of knowing that I'd say do something like this:

$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();

In your last code snippet you're doing \r - not \n. I guess that's sometimes necessary but I'd wager that \n would be what you'd want to use most of the time.

Your log is a little weird because of this:

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

It's like you're getting "Username:" back but you're expecting something else so it takes 30 seconds for the next message to be sent. In your fourth code snippet you're doing $ssh->read(Username:'); (which I do note has a syntax error). I'd expect that to return immediately if I correctly extrapolated what your full code might be doing but it doesn't seem to be from your logs, which makes me think that my extrapolation might not be entirely correct.

neubert
  • 15,947
  • 24
  • 120
  • 212
  • With that code would be clearer? thanks for listening ! – user8540723 Sep 29 '17 at 18:09
  • `include(str_replace("\\", "/", __DIR__) . '/phpseclib/Net/SSH2.php'); define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX); $login = strtolower("Bss182"); $pw = strtolower("pw1dEfalt"); function elementlan($login, $pw){ $ssh = new Net_SSH2("000.111.22.3"); if(!$ssh->login($login, $pw)){ exit("FAILED !!"); } $ssh->exec("telnet 200.100.200.200"); echo $ssh->read("Usernme:"); $ssh->write("$login".'\n'); echo $ssh->read("Password:"); $ssh->write("$pw".'\n'); echo $ssh->getLog(); } $conn = elementlan($login, $pw);` – user8540723 Sep 29 '17 at 18:11
  • @user8540723 - it'd be better if you added that to your orig post but since you didn't... `$ssh->exec("telnet 200.100.200.200");` I think you'd be better off doing that with `$ssh->write("telnet 200.100.200.200\n");`. Or maybe `$ssh->enablePTY(); $ssh->exec("telnet 200.100.200.200");`. – neubert Sep 29 '17 at 19:19
  • `$ssh->read("Usernme:");` you got a typo there. Should be `Username` - not `Usernme`. – neubert Sep 29 '17 at 19:19
  • `$ssh->write("$login".'\n');` doesn't do what you seem to think it does. `strlen('\n')` is 2 while `strlen("\n")` is 1. – neubert Sep 29 '17 at 19:20
  • 1
    Thank you so much for everything ! You helped me a lot if I could give you back ... – user8540723 Oct 02 '17 at 13:01