Page 1 of 1

Reading command outout from ssh linux client

Posted: Fri Dec 11, 2020 4:36 pm
by leostereo
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.

Re: Reading command outout from ssh linux client

Posted: Fri Dec 11, 2020 6:08 pm
by pe1chl
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.

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

Posted: Sun Dec 13, 2020 8:52 pm
by sin3vil
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.

Re: Reading command outout from ssh linux client

Posted: Sun Dec 13, 2020 10:21 pm
by pe1chl
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'}
}

Re: Reading command outout from ssh linux client

Posted: Thu Dec 17, 2020 4:29 pm
by leostereo
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);

?>

Re: Reading command outout from ssh linux client

Posted: Thu Dec 17, 2020 6:30 pm
by pe1chl
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.