Community discussions

MikroTik App
 
User avatar
CyB3RMX
Member Candidate
Member Candidate
Topic Author
Posts: 148
Joined: Thu May 26, 2011 7:08 am

Get Identity and adrees from neighbor

Thu Aug 25, 2016 8:02 pm

Hello!
I'm making a script in bash on a linux box to list all CPE's on an AP.

When i:
/ip neigh pr

I get all the info mixed, but i need to get Address and Identity but only of the ones that identity starts with CPE-
I tried everything but just cant make it work.

My way around will be to print terse and then awk it on the linux box.. but i wan to learn how to do it on the mt box.

Any help will be appreciated.

Thanks
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Get Identity and adrees from neighbor

Thu Aug 25, 2016 9:51 pm

/ip neighbor print where identity~"^CPE\\-"
is what you need to filter to only those with identies starting with "CPE-". And if you want to output them in a custom fashion, you can use the "as-value" argument.

e.g.
:local cpes [/ip neighbor print as-value where identity~"^CPE\\-"];
:foreach cpe in=$cpes do={
    :put (($cpe->"address") . " " . $cpe->"identity");
}
Will print lines containing the IP, followed by a space, followed by the identity.
 
User avatar
CyB3RMX
Member Candidate
Member Candidate
Topic Author
Posts: 148
Joined: Thu May 26, 2011 7:08 am

Re: Get Identity and adrees from neighbor

Fri Aug 26, 2016 1:47 am

/ip neighbor print where identity~"^CPE\\-"
is what you need to filter to only those with identies starting with "CPE-". And if you want to output them in a custom fashion, you can use the "as-value" argument.

e.g.
:local cpes [/ip neighbor print as-value where identity~"^CPE\\-"];
:foreach cpe in=$cpes do={
    :put (($cpe->"address") . " " . $cpe->"identity");
}
Will print lines containing the IP, followed by a space, followed by the identity.
Thank you!, The first one worked like charm, the second one doesnt output anything.. :v
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Get Identity and adrees from neighbor

Fri Aug 26, 2016 2:35 am

If you're typing directly into terminal, you need to surround the thing with "{" and "}", so that it is treated as a single unit. The alternative is to enter it in a script at "/system script".
 
User avatar
CyB3RMX
Member Candidate
Member Candidate
Topic Author
Posts: 148
Joined: Thu May 26, 2011 7:08 am

Re: Get Identity and adrees from neighbor

Fri Aug 26, 2016 2:58 am

If you're typing directly into terminal, you need to surround the thing with "{" and "}", so that it is treated as a single unit. The alternative is to enter it in a script at "/system script".
My hero!. hehe.. Thank you!!
 
User avatar
katem07
just joined
Posts: 17
Joined: Mon Apr 10, 2017 11:35 am
Location: SAR
Contact:

Re: Get Identity and adrees from neighbor

Mon Aug 13, 2018 1:38 am

Nice discussion ,
I want to display only the identity of the device , i do many tries to get the result but No success ...
/ip neighbor print  where  interface=bridge1 address~"^192.168"


i did a script to detect the rogue dhcp server and it must show the damaged devices identity .. here is the code :
# Count the number of devices who took rouge dhcp ip address
:local loop [:len [/ip neighbor find where interface=bridge1 address~"^192.168"]];

# Discover the identity of the devices who took rouge dhcp ip address <<>> Here is my issue !!
:local id [/ip neighbor find where interface=bridge1 address~"^192.168"]

# Show the number of the devices if found
:if ($loop>0) do={:log warning "$loop"

:delay 2s;

#Show The identity of the Devices if found <<>> Here is my issue !!
:log warning "$id"
}

# If there is no Rouge DHCP Server damages do log : No Loop  <<>> Here is another issue !! .. this logs with the twice situations !!!
:else do [:log warning "No Loop";]
i didn't know where is the wrong code !
 
berzins
just joined
Posts: 10
Joined: Thu Apr 05, 2018 2:46 pm

Re: Get Identity and adrees from neighbor

Mon Aug 13, 2018 3:22 pm

Nice discussion ,
I want to display only the identity of the device , i do many tries to get the result but No success ...
/ip neighbor print  where  interface=bridge1 address~"^192.168"


i did a script to detect the rogue dhcp server and it must show the damaged devices identity .. here is the code :
# Count the number of devices who took rouge dhcp ip address
:local loop [:len [/ip neighbor find where interface=bridge1 address~"^192.168"]];

# Discover the identity of the devices who took rouge dhcp ip address <<>> Here is my issue !!
:local id [/ip neighbor find where interface=bridge1 address~"^192.168"]

# Show the number of the devices if found
:if ($loop>0) do={:log warning "$loop"

:delay 2s;

#Show The identity of the Devices if found <<>> Here is my issue !!
:log warning "$id"
}

# If there is no Rouge DHCP Server damages do log : No Loop  <<>> Here is another issue !! .. this logs with the twice situations !!!
:else do [:log warning "No Loop";]
i didn't know where is the wrong code !



Hello, I think this might work for you, just replace your script with this. You can change the $interfaceParam and $addressParam to anything you like and it should output the correct identity. Feel free to ask any questions if something doesn't work for you.
# DEFINE YOUR SEARCH PARAMETERS

:local interfaceParam "bridge1"; 
:local addressParam "^192.168";

# INITIALIZE VALUES
:local loop [:len [/ip neighbor find where interface=$interfaceParam address~$addressParam]];
:local id [/ip neighbor find where interface=$interfaceParam address~$addressParam]
:local rogueIdentity;


# OUTPUT THE NUMBER OF ROGUE DEVICES DETECTED 
:if ($loop>0) do={:log warning "There are $loop rogue devices"} else={ :log warning "No rogue devices detected"};


# GET IDENTITIES OF ROGUE DEVICES
:foreach var in=$id do={
    :set $rogueIdentity [/ip neighbor get [/ip neighbor find where .id=$var] identity]; 
:log warning "Rogue device $rogueIdentity detected";



#:put $rogueIdentity;

}
 
User avatar
katem07
just joined
Posts: 17
Joined: Mon Apr 10, 2017 11:35 am
Location: SAR
Contact:

Re: Get Identity and adrees from neighbor

Wed Aug 15, 2018 12:09 am

Hello, I think this might work for you, just replace your script with this. You can change the $interfaceParam and $addressParam to anything you like and it should output the correct identity. Feel free to ask any questions if something doesn't work for you.
CODE: SELECT ALL

# DEFINE YOUR SEARCH PARAMETERS

:local interfaceParam "bridge1"; 
:local addressParam "^192.168";

# INITIALIZE VALUES
:local loop [:len [/ip neighbor find where interface=$interfaceParam address~$addressParam]];
:local id [/ip neighbor find where interface=$interfaceParam address~$addressParam]
:local rogueIdentity;


# OUTPUT THE NUMBER OF ROGUE DEVICES DETECTED 
:if ($loop>0) do={:log warning "There are $loop rogue devices"} else={ :log warning "No rogue devices detected"};



Thank you so much , It's works as expected :D

You are really a genius , And there is an Arabian saying : teach me fishing better than fish me a fish :)

Where can I find the basic rules for learning scripting ? :? :?

Best Regards my hero !
Last edited by katem07 on Sun Sep 08, 2019 6:03 pm, edited 1 time in total.
 
berzins
just joined
Posts: 10
Joined: Thu Apr 05, 2018 2:46 pm

Re: Get Identity and adrees from neighbor

Wed Aug 15, 2018 7:55 am

Hello, I think this might work for you, just replace your script with this. You can change the $interfaceParam and $addressParam to anything you like and it should output the correct identity. Feel free to ask any questions if something doesn't work for you.
CODE: SELECT ALL

# DEFINE YOUR SEARCH PARAMETERS

:local interfaceParam "bridge1"; 
:local addressParam "^192.168";

# INITIALIZE VALUES
:local loop [:len [/ip neighbor find where interface=$interfaceParam address~$addressParam]];
:local id [/ip neighbor find where interface=$interfaceParam address~$addressParam]
:local rogueIdentity;


# OUTPUT THE NUMBER OF ROGUE DEVICES DETECTED 
:if ($loop>0) do={:log warning "There are $loop rogue devices"} else={ :log warning "No rogue devices detected"};



Thank you so much , It's works a expected :D

You are really a genius , And there is an Arabian saying : teach me fishing better than fish me a fish :)

Where can I find the basic rules for learning scripting ? :? :?

Best Regards my hero !
Happy to help. :)

I suggest starting here https://wiki.mikrotik.com/wiki/Manual:Scripting , all you have to do is just take one router and try to write some basic things, whatever comes to mind, for example: Periodically check routers CPU temperature and output it to console or log. I suggest learning all about {find} and {get} commands in ROS, they are extremely valuable and will definitely be part of most scripts.

There are also some script examples to learn from here: https://wiki.mikrotik.com/wiki/Manual:S ... g-examples and here: https://wiki.mikrotik.com/wiki/Scripts , you can see how some things are done and build on that.

The best practice to get better at scripting your Mikrotik is to just do it, trial by error. You'll get there!
 
User avatar
katem07
just joined
Posts: 17
Joined: Mon Apr 10, 2017 11:35 am
Location: SAR
Contact:

Re: Get Identity and adrees from neighbor

Wed Aug 15, 2018 2:45 pm



Happy to help. :)

I suggest starting here https://wiki.mikrotik.com/wiki/Manual:Scripting , all you have to do is just take one router and try to write some basic things, whatever comes to mind, for example: Periodically check routers CPU temperature and output it to console or log. I suggest learning all about {find} and {get} commands in ROS, they are extremely valuable and will definitely be part of most scripts.

There are also some script examples to learn from here: https://wiki.mikrotik.com/wiki/Manual:S ... g-examples and here: https://wiki.mikrotik.com/wiki/Scripts , you can see how some things are done and build on that.

The best practice to get better at scripting your Mikrotik is to just do it, trial by error. You'll get there!
I am very grateful :)

I will review the links provided , Otherwise I was thought that scripting in MT are based on the Python language

Anyway, I will create a library of basic scripts ready to run , and I will put them here as part of the response to what I learned from you

Regard's

Who is online

Users browsing this forum: No registered users and 38 guests