using eth0 to change the configuration

  • 1
  • Question
  • Updated 4 years ago
  • Answered
Sorry if I am posting an already posted question.

We have a requirement where we connect our AP141 to a PC using a Ethernet cable with which we should configure the AP. In this context, Can I use the IP address of eth0 interface and Port 22 or 23(Ports used for SSH/Telnet) in a C++ program to connect to the AP and use a sendto() socket function call to update the configuration?


Photo of Kaushik Naarumanchi

Kaushik Naarumanchi

  • 14 Posts
  • 0 Reply Likes

Posted 4 years ago

  • 1
Photo of Nick Lowe

Nick Lowe, Official Rep

  • 2491 Posts
  • 451 Reply Likes
You can certainly SSH to the AP as per normal in code and configure it there with 'expect' type semantics. (If you wanted to use a socket and sendto(), you would have to implement a library to the SSH 2.0 specification. I have no idea why you would want to do this though...)
Photo of Kaushik Naarumanchi

Kaushik Naarumanchi

  • 14 Posts
  • 0 Reply Likes

Using PuTTY, I am trying to do an SSH to AP using the IP Address assigned to eth0. But it doesn't work. So other than using a serial connection, is there a way we can configure the AP?

Photo of Nick Lowe

Nick Lowe, Official Rep

  • 2491 Posts
  • 451 Reply Likes
It does work and you can use PuTTY to connect, you likely have just got something wrong in your configuration so that APs are denying the connection.

Can I suggest that you check it over? :)

Nick
(Edited)
Photo of Nick Lowe

Nick Lowe, Official Rep

  • 2491 Posts
  • 451 Reply Likes
For the Network Policy you are using, have you checked to ensure that the Traffic Filter you have applied permits SSH?


 
Also check that no firewall is interfering in the process.

Nick
(Edited)
Photo of Kaushik Naarumanchi

Kaushik Naarumanchi

  • 14 Posts
  • 0 Reply Likes
Hi,
We are able to execute CLI commands using PUTTY. But when we are trying to use a ssh library or plink to execute CLI commands through a C program in windows, the commands are not getting executed. Does AeroHive AP141, support this? Do wee need to set anything?

Kaushik.
Photo of Roberto Casula

Roberto Casula, Champ

  • 231 Posts
  • 111 Reply Likes
The CLI on the AP is not a shell executing commands (like bash would be for example). You therefore can't use plink to send "commands", you need to transmit user input as if you were typing interactively.

With plink you can do this easily enough using input redirection, but it's a bit basic:

Create a text file with the "commands" in. As a test, try this (everything between the lines!)

-----------------------
console page 0
show run
exit
n
-----------------------

The "console page 0" will prevent the "Press any key" paged output from occurring. The "n" at the end is just in case there is unsaved config (on exit, the AP will wait for you to say y/n to "Do you want to save changes?"

With an SSH library (e.g. libssh) you need to do the same thing, i.e. send interactive user input rather than trying to execute commands. With libssh specifically, you would need to use ssh_channel_write() to send interactive user input and read the output using ssh_channel_read(), rather than using ssh_channel_request_exec() which may be what you were trying to do?

Another, probably easier and more flexible, option is to use expect scripts. If you are on a UNIX/Linux platform, it's easy peasy. If you're on Windows you'll need to find a port of expect - there are a few, but probably the best one is bundled in ActiveTcl from ActiveState, available here: http://www.activestate.com/activetcl/downloads (the Community edition is free).

Photo of Roberto Casula

Roberto Casula, Champ

  • 231 Posts
  • 111 Reply Likes
Just realised I missed a bit out of this post. To use plink with the text file above, just do:

plink admin@<ip-address> -l admin -pw <password> < test.txt

Photo of Kaushik Naarumanchi

Kaushik Naarumanchi

  • 14 Posts
  • 0 Reply Likes
Thanks much for the input. Now I understood how to pass the commands using plink. And it works !!!

I am also trying to do the same with libssh. As you rightly guessed, I used this function:
ssh_channel_request_exec(channel, "radio profile 4");

And now as you suggested I tried using ssh_channel_write() to run this command like this:
ssh_channel_write(channel, "console page 0",strlen("console page 0"));
ssh_channel_write(channel, "radio profile 4",strlen("radio profile 4"));
ssh_channel_write(channel, "exit",strlen("exit"));
ssh_channel_write(channel, "y",strlen("y"));`

I think this is not the correct way of doing !!! Can you please suggest the right way?

Kaushik.
Photo of Roberto Casula

Roberto Casula, Champ

  • 231 Posts
  • 111 Reply Likes
The most obvious thing is that you will need to send carriage returns - you are essentially sending keystrokes here.

You should also be reading the output and waiting for the command prompt string to be output before issuing the next command (and don't forget that you'll get an echo back of what was typed as well - you are basically operating like a terminal emulator), otherwise you'll potentially hit some buffering issues (plus you won't have a way to handle errors etc.).


Photo of Kaushik Naarumanchi

Kaushik Naarumanchi

  • 14 Posts
  • 0 Reply Likes

Yes. I thought I should be sending carriage returns also. So I tried this way:
ssh_channel_write(channel, "radio profile 4",strlen("radio profile 4"));
ssh_channel_write(channel, "\n",strlen("\n"));

But it didn't work. Also tried:
ssh_channel_write(channel, "radio profile 4",strlen("radio profile 4"));
ssh_channel_read(channel, buffer, sizeof(buffer), 0);

But read is blocked infinitely !!! May be CLI is not responding !!!

Also i would like to confirm the way i am connecting to CLI using libssh.
    my_ssh_session = ssh_new();
    if (my_ssh_session != NULL)
    {
        ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "10.4.72.119");
        ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
        ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "admin");

        ret = ssh_connect(my_ssh_session);

        state = ssh_is_server_known(my_ssh_session);

        ret = ssh_userauth_password(my_ssh_session, NULL, SSH_LOGIN_PWD);
        if (ret != SSH_AUTH_SUCCESS)
        {
            ssh_disconnect(my_ssh_session);
            ssh_free(my_ssh_session);
        }

        channel = ssh_channel_new(my_ssh_session);
        if (channel == NULL)
            return SSH_ERROR;
        ret = ssh_channel_open_session(channel);
        if (ret != SSH_OK)
        {
            ssh_channel_free(channel);
            return ret;
        }

    }

Photo of Kaushik Naarumanchi

Kaushik Naarumanchi

  • 14 Posts
  • 0 Reply Likes

Can we get a sample code where libssh is used to execute CLI commands? :-)

Photo of Roberto Casula

Roberto Casula, Champ

  • 231 Posts
  • 111 Reply Likes
I think that might be stretching the scope of this forum Kaushik to be honest. There are lots of samples out the libssl documentation site and plenty of other samples available via a quick Google.

I would recommend you look at expect - it's probably a much easier way to achieve what you want.
Photo of Kaushik Naarumanchi

Kaushik Naarumanchi

  • 14 Posts
  • 0 Reply Likes
Yes. From google/libssh and CLI documentation I am doing all the necessary steps to connect to Aerohive CLI. But could not succeed. The same set of steps are working with some other linux machine. As you said CLI is not a shell...May be thats why its not working with libssh functions but working with plink command.

I thought something is blocking in CLI w.r.t libssh APIs. So my intention was to know if I need to set anything related to that !!!
Photo of Nick Lowe

Nick Lowe, Official Rep

  • 2491 Posts
  • 451 Reply Likes
All you need to keep in mind, as has previously been said, is that you're working with streams not discrete commands. You need to consume the stream from the AP appropriately, and write back to it appropriately. (There's definitely a feature request possibility here though!)
(Edited)