Community discussions

MikroTik App
 
hogger
just joined
Topic Author
Posts: 11
Joined: Mon Dec 15, 2008 3:56 am
Location: Richmond, Nelson, New Zealand
Contact:

Obtain wireless card MAC address OK on 2.9 but fails on 3.1

Mon Dec 15, 2008 11:03 pm

I have inherited a Perl script from a previous employee that fails on version v3.x but works OK with v2.9.x.
There is one line of code that fails; which is below:
# Mac Getter
$MAC = `ssh admin\@192.168.0.1 \'/interface wireless print value-list\' | grep mac | awk \'{print \$2}\'`;
I am new to Perl and also using scripts with MikroTik router boards.
Can anyone help me with the following please:
1. Am I correct in thinking that the code obtains the MAC address of the wireless card form the value list?
2. Why is this failing with version 3.x?
3. What do I need to alter to make it work with v3.x
:)
 
SurferTim
Forum Guru
Forum Guru
Posts: 4637
Joined: Mon Jan 07, 2008 10:31 pm
Location: Miramar Beach, Florida

Re: Obtain wireless card MAC address OK on 2.9 but fails on 3.1

Mon Dec 15, 2008 11:20 pm

Greetings! Welcome to the forum.
The reason it fails is the 'value-list' doesn't function in 3.x. To get the mac of the card use:
/interface wireless get wlan1 mac-address
or replace wlan1 with the interface you want the mac from.
You can probably drop the | grep mac | part too.
 
hogger
just joined
Topic Author
Posts: 11
Joined: Mon Dec 15, 2008 3:56 am
Location: Richmond, Nelson, New Zealand
Contact:

Re: Obtain wireless card MAC address OK on 2.9 but fails on 3.1

Tue Dec 16, 2008 2:24 am

Thanks SurferTim for your prompt advice. Much appreciated.
I have amended the line of code to the following; matching your suggestion:
$MAC = `ssh admin\@192.168.0.1 \'/interface wireless get wlan mac-address\'`
However the $MAC variable still returns empty.
I must point out that the script I am using is run on a server; if that has any differance.

I now see the new set of commands available in v3.x once one selects the "get" command. Thanks for this enlightenment.
Tell me if I am wrong though but does one use these commands when a script is run by the scripting engine associated with the router board rather than with what my script is doing.

Can you help yet again please.
 
SurferTim
Forum Guru
Forum Guru
Posts: 4637
Joined: Mon Jan 07, 2008 10:31 pm
Location: Miramar Beach, Florida

Re: Obtain wireless card MAC address OK on 2.9 but fails on 3.1

Tue Dec 16, 2008 2:38 am

I just noticed the ssh format is incorrect also.
ssh 192.168.0.1 -l admin
But I am not sure how you are going to deal with the password, unless you already have some type of security key installed. Google 'ssh without password' for help on that.
 
hogger
just joined
Topic Author
Posts: 11
Joined: Mon Dec 15, 2008 3:56 am
Location: Richmond, Nelson, New Zealand
Contact:

Re: Obtain wireless card MAC address OK on 2.9 but fails on 3.1

Tue Dec 16, 2008 2:59 am

What might help is if I show you the full script before the said line concerned. The script uses a FTP session to login first.
#!/usr/bin/perl

use DBI;
use Mysql;
use Net::FTP;
`arp -d 192.168.0.1`;

$host1 = "192.168.0.1";
$username1 = "admin";
$password1 = "";
$filename1 = "all.rsc";
$directory1 = "/";

$key1 = "cpe.pub";
## Ftp
$ftp = Net::FTP->new("$host1", Timeout => 30, Debug   => 0) or die "Can't connect: $@\n";
$ftp->login($username1, $password1)       or die "Couldn't login\n";
$ftp->cwd($directory1);
$ftp->put($filename1, $filename1)                    or die "Couldn't put $filename\n";
$ftp->put($key1, $key1)                    or die "Couldn't put $key1\n";
$ftp->quit()    or warn "Couldn't quit.  Oh well.\n";

# Mac Getter
$MAC = `ssh admin\@192.168.0.1 \'/interface wireless print value-list\' | grep mac | awk \'{print \$2}\'`;
$MAC =~ s/\n//g;
$MAC =~ s/\r//g;
#####
The rest off the script works with the obtained mac-address and updates a MySQL database and also sets a series of router commands before finishing.

Now that you are informed with the above does your last post still apply :lol:

I know the solution to my problem does lie with the new way of accessing the wireless card mac-address in v3.x and you have helped me after a couple of days of trying by myself to sort this out. Therefore I realy do appreciate your commitment to my solution.
I am open to any suggestion and of course the previous author may not have written the original code that well.
 
SurferTim
Forum Guru
Forum Guru
Posts: 4637
Joined: Mon Jan 07, 2008 10:31 pm
Location: Miramar Beach, Florida

Re: Obtain wireless card MAC address OK on 2.9 but fails on 3.1

Tue Dec 16, 2008 4:19 am

The FTP part appears to move the key, so if all is well, you should be able to login without a password.
Did you try the new login?
ssh 192.168.0.1 -l admin

ADD: I would try this login from a shell first just to insure you can login without a password before continuing debugging the script.
 
hogger
just joined
Topic Author
Posts: 11
Joined: Mon Dec 15, 2008 3:56 am
Location: Richmond, Nelson, New Zealand
Contact:

Re: Obtain wireless card MAC address OK on 2.9 but fails on 3.1

Wed Dec 17, 2008 2:48 am

Yes I have tried the new login and it works fine.

I have finally solved the problem.
Thanks for you help too.

I have still kept the perl functions grep and awk. With the v3.x I used the detail command and then extracted the mac-address by striping off the "mac-address" header.
See below.
print "OBTAINING WIRELESS CARD MAC ADDRESS\n";
# Need to obtain system version because the method to obtain the wireless card MAC address is differant
$version = `ssh 192.168.0.1 -l admin \'/system package print value-list\' | grep version | awk \'{print \$2}\'`;
print "..... System Version = $version\n";
$versionShort = substr $version, 0, 1;


# Mac Getter
if ($versionShort == 2) {
        $MAC = `ssh 192.168.0.1 -l admin \'/interface wireless print value-list\' | grep mac | awk \'{print \$2}\'`;
}

if ($versionShort == 3) {
        $MAC = `ssh 192.168.0.1 -l admin \'/interface wireless print value-list detail\' | grep mac | awk \'{print \$5}\'`;
        $MAC = substr $MAC, 12;
}

$MAC =~ s/\n//g;
$MAC =~ s/\r//g;
 
Omniflux
just joined
Posts: 11
Joined: Sun Sep 19, 2004 1:27 am
Location: Nephi, UT. USA
Contact:

Re: Obtain wireless card MAC address OK on 2.9 but fails on 3.1

Wed Dec 17, 2008 3:00 am

The following should work for RouterOS 2.9+ (I tested it...it does not work on 2.8 though)
$MAC = `ssh admin\@192.168.0.1 ':put [interface wireless get wlan1 mac-address]'`;
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6283
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: Obtain wireless card MAC address OK on 2.9 but fails on 3.1

Thu Dec 18, 2008 1:02 pm

that works on 3.17 too the key here is. get returns value to a variable, so if you wrote that comand you got no output. you have to set :put, so all you get is mac address when execute
:put [/interface wireless get wlan1 mac-address]
in console. so when you feed this command to ssh as command to execute on target system, then you weill get deisred output, that you will not have to parse. And your perl just executes the command, as in in console, and assigns output to variable. so you have to test your commands in console to see the outpu

Who is online

Users browsing this forum: Majestic-12 [Bot] and 14 guests