Community discussions

MikroTik App
 
User avatar
leostereo
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 83
Joined: Thu Oct 18, 2012 11:36 pm
Location: mendoza , argentina
Contact:

Reading command outout from ssh linux client

Fri Dec 11, 2020 4:36 pm

Hello , I need to find if specific user exist at hotspot users list.
This is what im doing:
sshpass -p 'api_user' ssh api_user\@172.30.7.2 -p22000 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null 'ip hotspot user print where name=60:8F:5C:B8:75:4F'
And this is what I get:
Flags: * - default, X - disabled, D - dynamic
 #   SERVER           NAME         ADDRESS         PROFILE         UPTIME
 0                    60:8F:5C:...                 default         0s
 
But this is what I need:
[api_user@hotspot] > ip hotspot user print where name=60:8F:5C:B8:75:4F
Flags: * - default, X - disabled, D - dynamic
 #   SERVER                                                              NAME                                                              ADDRESS         PROFILE                                                              UPTIME
 0                                                                       60:8F:5C:B8:75:4F                                                                 default                                                              0s

Problem , is that if I get this "collapsed" view , I can not grep the mac address.
Is there some way to change this to get the full view of the command output?
Regards.
 
pe1chl
Forum Guru
Forum Guru
Posts: 7176
Joined: Mon Jun 08, 2015 12:09 pm

Re: Reading command outout from ssh linux client

Fri Dec 11, 2020 6:08 pm

It is better to do this kind of thing via API.
You will have to familiarize your self at first, and maybe download and install a helper library, but then it becomes very easy to retrieve information and process it.
 
sin3vil
just joined
Posts: 8
Joined: Sat May 26, 2018 10:05 pm

Re: Reading command outout from ssh linux client  [SOLVED]

Sun Dec 13, 2020 8:52 pm

Note sure what info you're after but it's better to put the work in once on the Mikrotik to properly format output that's easily parseable on your linux system.

i.e. this produces an array of json objects.
local result "["
#as-value produces an array of dictionary-style  key/value pairs for each line normally printed
foreach item in=[ip hotspot user print as-value where name=60:8F:5C:B8:75:4F] do={
local thisItem "{"
#this loop runs through the key/value pairs for each item in the array
foreach key,value in=$item do={
#concatenate previous $thisItem value + key + : + value 
set thisItem ($thisItem."\"".$key."\":\"".$value."\"},")
}
set result ($result.$thisItem)
}
#strip trailing , from result
set result [pick $result 0 ([len $result]-1)]
#close the open bracket
set result ($result."]")
#dont print result if it's not populated
if ([len $result]>2) do={
put $result
}
You can then pipe this into jq to get what you want. Say,
| jq '.[]|select(."mac-address"=="60:8F:5C:B8:75:4F")|.uptime'
You can also add above script as a function on the Mikrotik so you don't need to call the whole thing each time via SSH.
global getUsers do={
local result "["
foreach item in=[ip hotspot user print as-value where name="$1"] do={
local thisItem "{"
foreach key,value in=$item do={
set thisItem ($thisItem."\"".$key."\":\"".$value."\"},")
}
set result ($result.$thisItem)
}
set result [pick $result 0 ([len $result]-1)]
set result ($result."]")
if ([len $result]>2) do={
put $result
}
}
You would then run '$getUser "60:8F:5C:B8:75:4F"' via SSH.
Of course the function will be lost on reboot, so you need a scheduler to load it on startup.
 
pe1chl
Forum Guru
Forum Guru
Posts: 7176
Joined: Mon Jun 08, 2015 12:09 pm

Re: Reading command outout from ssh linux client

Sun Dec 13, 2020 10:21 pm

When using API you are effectively doing the same but you define and run the functions on the Linux system, using a programming language that you prefer and that potentially is more powerful than the RouterOS scripting language.
I use Perl, others use Python or PHP.
With API you can retrieve the values without having to parse them later (well, the parsing is done in the API support library that likely already exists).

For example, to retrieve the routing table I use something like this in a Perl script:
MikroTik::login($host,$user,$password,0,8728) or die "login failed";

my ($retval,@results) = MikroTik::mtik_query('/ip/route/print',
        {}, {'active' => 'true'});

foreach my $entry (@results) {
        fields are available here as e.g. $$entry{'gateway'}
}
 
User avatar
leostereo
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 83
Joined: Thu Oct 18, 2012 11:36 pm
Location: mendoza , argentina
Contact:

Re: Reading command outout from ssh linux client

Thu Dec 17, 2020 4:29 pm

It is better to do this kind of thing via API.
You will have to familiarize your self at first, and maybe download and install a helper library, but then it becomes very easy to retrieve information and process it.
I totally agree with you , but for some reason, I can not get existing user by name from api, this is what I have:
<?php
require('routeros-api/routeros_api.class.php');

$API = new RouterosAPI();
$API->debug = false;
$API->timeout = 1;
$API->attempts = 3;
$peers_nok=array();
$peers_ok=array();
$peers_nok_list='';
$host= "172.30.7.2";

ob_start();
if ($API->connect($host, 'api_user', 'api_user')) {
   $peers_nok=array();
   $API->write('/ip/hotspot/user/print/where', array(
      "name"     => "peter"
));
   $READ = $API->read(false);
   $ARRAY = $API->parseResponse($READ);
   $API->disconnect();

}

        print_r($ARRAY);

?>
 
pe1chl
Forum Guru
Forum Guru
Posts: 7176
Joined: Mon Jun 08, 2015 12:09 pm

Re: Reading command outout from ssh linux client

Thu Dec 17, 2020 6:30 pm

OK I am not familiar with the PHP API library but I advise you to experiment with some simple commands and at first do not include extra selection condtions, add them later.

Who is online

Users browsing this forum: No registered users and 47 guests