alejho / Arduino-Telnet-Client

a minimal (but real) telnet client for Arduino, actually able to login and send commands to a generic telnet server
GNU General Public License v3.0
24 stars 24 forks source link

Question: How can I add new user using this library? #4

Closed TEAMBTG closed 5 years ago

TEAMBTG commented 5 years ago

This is my sample command: /ip hotspot user add name=newuser password=newuser limit-uptime=00:10:00 profile=hotspot-user

It doesn't add to my router.

alejho commented 5 years ago

In the header file try to increase MAX_OUT_BUFFER_LENGTH to a value bigger than the longest string you need to send (it's a uint8_t so max 255 or you can change data type to uint16_t). For example:

const uint8_t MAX_OUT_BUFFER_LENGTH = 150;

Let me know if it works!

TEAMBTG commented 5 years ago

In the header file try to increase MAX_OUT_BUFFER_LENGTH to a value bigger than the longest string you need to send (it's a uint8_t so max 255 or you can change data type to uint16_t). For example:

const uint8_t MAX_OUT_BUFFER_LENGTH = 150;

Let me know if it works!

Even you increase the MAX_OUT_BUFFER_LENGTH. It still the same. Below is the response from the serial monitor.


[admin@MikroTik] >                                                            
[admin@MikroTik] >ip
[admin@MikroTik] >ip

[admin@MikroTik] /ip>                                                         
[admin@MikroTik] /ip>hotspot
[admin@MikroTik] /ip>hotspot

[admin@MikroTik] /ip hotspot>                                                 
[admin@MikroTik] /ip hotspot>user
[admin@MikroTik] /ip hotspot>user

[admin@MikroTik] /ip hotspot user>                                            
[admin@MikroTik] /ip hotspot user>add name=newuser password=newuser limit-uptim[admin@MikroTik] /ip hotspot user>add name=newuser password=newuser limit-upt><ame=newuser password=newuser limit-uptim                                      
<ame=newuser password=newuser limit-uptim
alejho commented 5 years ago

can you share your sketch code?

alejho commented 5 years ago

From your output it seems you get and asnswer from router after this truncated string:

/ip hotspot user>add name=newuser password=newuser limit-uptim

that is a 64 chars string...are you sure you increased the buffer, compiled again and downloaded the new code?

TEAMBTG commented 5 years ago

can you share your sketch code? Here you go:

    if(tc.login(mikrotikRouterIp, "admin", "wasaL@K123")){
      tc.sendCommand("ip");
      tc.sendCommand("hotspot");
      tc.sendCommand("user");  
      tc.sendCommand("add name=newuser password=newuser limit-uptime=00:10:00");     
      //tc.sendCommand("set newuser limit-uptime=00:10:00");
      //tc.sendCommand("set newuser profile=hotspot-btg");
      tc.disconnect();
    } 

And I'm sure that the buffer is increased to 150.

//how long the command sent may be long
const uint8_t MAX_OUT_BUFFER_LENGTH = 150;
alejho commented 5 years ago

can you please send me the output of this code with TNDBG defined in the header file...may be there are some additional useful info...

TEAMBTG commented 5 years ago

TNDBG

Here you go:

login|connecting...
login|connected!
negotiate|server:IAC
negotiate|server:DO
negotiate|server opt: 
24
negotiate|client:IAC
negotiate|client:WONT
negotiate|server:IAC
negotiate|server:DO
negotiate|server opt: 
32
negotiate|client:IAC
negotiate|client:WONT
negotiate|server:IAC
negotiate|server:DO
negotiate|server opt: 
35
negotiate|client:IAC
negotiate|client:WONT
negotiate|server:IAC
negotiate|server:DO
negotiate|server opt: 
39
negotiate|client:IAC
negotiate|client:WONT
negotiate|server:IAC
negotiate|server:WILL
negotiate|server opt: 
3
negotiate|client:IAC
negotiate|client:DO
negotiate|server:IAC
negotiate|server:DO
negotiate|server opt: 
1
negotiate|client:IAC
negotiate|client:WONT
negotiate|server:IAC
negotiate|server:DO
negotiate|server opt: 
31
negotiate|client:IAC
negotiate|client:WONT
negotiate|server:IAC
negotiate|server:WILL
negotiate|server opt: 
5
negotiate|client:IAC
negotiate|client:DO
negotiate|server:IAC
negotiate|server:DO
negotiate|server opt: 
33
negotiate|client:IAC
negotiate|client:WONT
negotiate|server:IAC
negotiate|server:WILL
negotiate|server opt: 
1
negotiate|client:IAC
negotiate|client:DO

MikroTik v6.44.3 (stable)
Login: login|sending username
admin

Password:listenUntil|TERMINATOR RECEIVED
login|sending password

  MMM      MMM       KKK                          TTTTTTTTTTT      KKK

  MMMM    MMMM       KKK                          TTTTTTTTTTT      KKK

  MMM MMMM MMM  III  KKK  KKK  RRRRRR     OOOOOO      TTT     III  KKK  KKK

  MMM  MM  MMM  III  KKKKK     RRR  RRR  OOO  OOO     TTT     III  KKKKK

  MMM      MMM  III  KKK KKK   RRRRRR    OOO  OOO     TTT     III  KKK KKK

  MMM      MMM  III  KKK  KKK  RRR  RRR   OOOOOO      TTT     III  KKK  KKK

  MikroTik RouterOS 6.44.3 (c) 1999-2019       http://www.mikrotik.com/

[?]             Gives the list of available commands

command [?]     Gives help on the command and list of arguments

[Tab]           Completes the command/word. If the input is ambiguous,

                a second [Tab] gives possible options

/               Move up to base level

..              Move up one level

/command        Use command at the base level


Z  

[admin@MikroTik] >listenUntil|TERMINATOR RECEIVED
waitPrompt|FALSE PROMPT DETECTED

[admin@MikroTik] >listenUntil|TERMINATOR RECEIVED
ip
send|WRONG ECHO FROM SERVER
[admin@MikroTik] >listenUntil|TERMINATOR RECEIVED
waitPrompt|FALSE PROMPT DETECTED
ip

[admin@MikroTik] /ip>listenUntil|TERMINATOR RECEIVED
waitPrompt|FALSE PROMPT DETECTED

[admin@MikroTik] /ip>listenUntil|TERMINATOR RECEIVED
hotspot
send|WRONG ECHO FROM SERVER
[admin@MikroTik] /ip>listenUntil|TERMINATOR RECEIVED
waitPrompt|FALSE PROMPT DETECTED
hotspot

[admin@MikroTik] /ip hotspot>listenUntil|TERMINATOR RECEIVED
waitPrompt|FALSE PROMPT DETECTED

[admin@MikroTik] /ip hotspot>listenUntil|TERMINATOR RECEIVED
user
send|WRONG ECHO FROM SERVER
[admin@MikroTik] /ip hotspot>listenUntil|TERMINATOR RECEIVED
waitPrompt|FALSE PROMPT DETECTED
user

[admin@MikroTik] /ip hotspot user>listenUntil|TERMINATOR RECEIVED
waitPrompt|FALSE PROMPT DETECTED

[admin@MikroTik] /ip hotspot user>listenUntil|TERMINATOR RECEIVED
add name=newuser password=newuser limit-uptimsend|WRONG ECHO FROM SERVER
[admin@MikroTik] /ip hotspot user>listenUntil|TERMINATOR RECEIVED
waitPrompt|FALSE PROMPT DETECTED
add name=newuser password=newuser limit-upt>listenUntil|TERMINATOR RECEIVED
waitPrompt|FALSE PROMPT DETECTED
<ame=newuser password=newuser limit-uptim                                      
<ame=newuser password=newuser limit-uptimlistenUntil|TIMEOUT OCCURED
alejho commented 5 years ago

set in header:

const uint16_t PROMPT_REC_TOUT = 300;

and try again...I want to be sure you're working with the same version I tested!

TEAMBTG commented 5 years ago

set in header:

const uint16_t PROMPT_REC_TOUT = 300;

and try again...I want to be sure you're working with the same version I tested!

Still the same. I re-download and import again the library. But still the same. Do you have your mikrotik and arduino in you?

alejho commented 5 years ago

I can't really do a test since I don't have a mikrotik router now (I can virtualize it but not now). For some reasons mikrotik router send back a lot of (apparently) useless stuff compared to other telnet servers. This makes particularly difficult to detect when the server is ready to receive a new command. Try to change the sendCommand function like this in the .cpp file, in order to disable the "echo control" of sent characters that in your case fails for an unexpected answer of the server while the client is still sending a command.

bool telnetClient::sendCommand(const char* cmd){

    this->send(cmd, false);
    //negotiation until the server show the command prompt again
    if (strcmp(cmd, "exit") != 0){
        return this->waitPrompt();
    }

}
alejho commented 5 years ago

...and try to banally put a delay of some seconds between one command and the next one!

alejho commented 5 years ago

without changes in the sendCommand function you can try to just increase a lot this:

const uint16_t PROMPT_REC_TOUT = 5000;

alejho commented 5 years ago

Well, this is not exaclty the place to ask it but, here you are:

https://www.programmingsimplified.com/c-program-concatenate-strings

did you fix your problem?How? Thank you for your feedback!

On Mon, May 20, 2019 at 3:04 PM TEAMBTG notifications@github.com wrote:

Also is there any way to put string on this command?

String username = "testuser"; tc.sendCommand("add name="+ username+" password=newuser limit-uptime=00:10:00 profile=hotspot-btg");

Sorry, just new with C++.

no known conversion for argument 1 from 'StringSumHelper' to 'const char*'

Thanks!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/alejho/Arduino-Telnet-Client/issues/4?email_source=notifications&email_token=AEAVEQT6MTQ5XL5I4FR6ALDPWKOV5A5CNFSM4HN6S222YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVYYGXI#issuecomment-493978461, or mute the thread https://github.com/notifications/unsubscribe-auth/AEAVEQXWJHJ7GHBYGF7B6ZLPWKOV5ANCNFSM4HN6S22Q .

TEAMBTG commented 5 years ago

I can't really do a test since I don't have a mikrotik router now (I can virtualize it but not now). For some reasons mikrotik router send back a lot of (apparently) useless stuff compared to other telnet servers. This makes particularly difficult to detect when the server is ready to receive a new command. Try to change the sendCommand function like this in the .cpp file, in order to disable the "echo control" of sent characters that in your case fails for an unexpected answer of the server while the client is still sending a command.

bool telnetClient::sendCommand(const char* cmd){

  this->send(cmd, false);
  //negotiation until the server show the command prompt again
  if (strcmp(cmd, "exit") != 0){
      return this->waitPrompt();
  }

}

This makes it.