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.
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.
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,
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.
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'}
}
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.