Community discussions

MikroTik App
 
mboy
just joined
Topic Author
Posts: 12
Joined: Tue Feb 11, 2014 7:23 pm

Feature request - Winbox import from csv

Fri Mar 27, 2015 5:18 pm

Please add import from the csv file.
 
jandafields
Forum Guru
Forum Guru
Posts: 1515
Joined: Mon Sep 19, 2005 6:12 pm

Re: Feature request - Winbox import from csv

Sat Mar 28, 2015 8:59 pm

Please add import from the csv file.
What, exactly, are you wanting to import into winbox?
 
User avatar
ZeroByte
Forum Guru
Forum Guru
Posts: 4047
Joined: Wed May 11, 2011 6:08 pm

Re: Feature request - Winbox import from csv

Sun Mar 29, 2015 2:02 am

If you make your list o' stuff be in the format of valid routeros commands, you can ssh into the box and copy/paste.
 
jandafields
Forum Guru
Forum Guru
Posts: 1515
Joined: Mon Sep 19, 2005 6:12 pm

Re: Feature request - Winbox import from csv

Sun Mar 29, 2015 2:25 am

If you make your list o' stuff be in the format of valid routeros commands, you can ssh into the box and copy/paste.
Yes, you can already do that. You can copy and paste the commands in one large paste directly into the ssh terminal.

Also, you can put all the commands into a text file and use the import function on the router.

Read the wiki here:
http://wiki.mikrotik.com/wiki/Manual:Co ... figuration
 
mboy
just joined
Topic Author
Posts: 12
Joined: Tue Feb 11, 2014 7:23 pm

Re: Feature request - Winbox import from csv

Mon Mar 30, 2015 1:39 pm

I have more than 500 connections and manually add the address book is unrealistic.
 
User avatar
ZeroByte
Forum Guru
Forum Guru
Posts: 4047
Joined: Wed May 11, 2011 6:08 pm

Re: Feature request - Winbox import from csv

Mon Mar 30, 2015 10:03 pm

I have more than 500 connections and manually add the address book is unrealistic.
who said manually?

Make a new collumn in excell that uses the columns to generate the Mtik commands. Then drag that formula down the entire sheet, and voila.

eg:

a,b,c
192.168.50.27,ether1,bob
192.168.78.201,ether1,linda
192.168.99.55,ether2,ralph

create a column D that says: concatenate("/ip firewall add in-interface=",1b," src-address=",1a," comment=",1c)
copy the results of the formulae, paste into terminal window.

I'm not sure what you're trying to do, but if it's managing a long list of ppp secrets or some-such, then perhaps RADIUS is a good idea.
 
wisptech
just joined
Posts: 2
Joined: Fri May 26, 2017 5:45 pm

Re: Feature request - Winbox import from csv

Fri May 26, 2017 5:52 pm

I know this is a old thread that died long ago, but seeing as how I was looking for this very thing and couldn't find a solution. This is relevant so I'm bringing it back from the dead.

I wrote a PHP script to do import a list of IP addresses and related information from CSV into a Winbox .wbx file. Here is the code, feel free to update it or do with it as you wish. I hope someone finds it to be as useful as I do. I'm not a professional programmer so if you see something in my code that is wrong or could be improved, please let me and everyone else know so that we can fix it.
<?php
// ==== GENERAL INFORMATION ====
// Version 1.0
// 
// Christopher Tyler
// Network Engineer
// Total Highspeed Internet Solutions
// wisptech@gmail.com
//
// Winbox import wants a pretty specific format for their wbx files. I'm not a professional
// programmer so I don't know what all the hex codes do, but they are mostly static for the
// most part. The pirmay exception is that each hex variable needs to start with the length
// of the variable that follows. Other than that they are all the same throughout the file.
//
// There aren't any *nix or Windows specific lines of code so this script should work on
// any PHP capable platform but it has only been tested on Linux at this time.
//
// ==== CSV FORMAT ====
// The CSV that is expected currently needs to be in the format of
// group,ipaddress,username,password,secure mode, keep password, note
//
// Group, IP address, username, password and note are all obviously strings that may contain
// a varialbe lenght of alpha-numeric data. Secure mode and keep password however are binary
// arguments. 0 is off/diabled and 1 in on/enabled. So if you want to have secure mode on 
// you set it to 1 and 0 if you want it off, same for keep password.
//
// Do not create a label line, the csv should contain just the data you want to import and
// nothing else. As more options are added, this will reqire some changes to the format.


// Prepare the output file and remove any existing data in it
$outfile = 'winbox_output.wbx';
file_put_contents($outfile, NULL);

// Import the winbox.csv file into an array
$csv = array_map('str_getcsv', file('winbox.csv'));

// File header only needs to be inserted into the file once.
$bindata  = pack('C*',0x0F,0x10,0xC0,0xBE);

// Start looping through the array to create the address list
$x = 0;
foreach($csv as $host) {
  // Assign vales from the CSV file to variables as we go through them
  $group = $host[0];
  $ipaddr = $host[1];
  $username = $host[2];
  $password = $host[3];
  $securemode = $host[4];
  $keeppass = $host[5];
  $note = $host[6]; 
  
  // Spit some text out for the user so they can see what is being done for them
  print 'Imported: '.$group.' : '.$ipaddr.' : '.$username.' : '.$password.' : '.$securemode.' : '.$keeppass.' : '.$note."\r\n";
  
  $x++; // Increment $x by one

  // The group tag requires a '00 00' on subsequent calls, the first instance does
  // not have a leading '00 00', this section addresses that irregularity.
  if ($x > 1){
    $bindata .= pack('C*',0x00,0x00);
  }
  
  // For each heading, we have to determin the lenght of the string variable that we
  // want to write to the file and then convert that string into hex and store it in
  // a varible that will get written to the output file.

  // Group
  $length = (strlen($group) + 6);
  $bindata .= pack('C*',$length,0x00,0x05);
  $bindata .= "group";
  $bindata .= $group;

  // Host
  $ipaddr = trim($ipaddr, "\r\n");
  $length = (strlen($ipaddr) + 5);
  $bindata .= pack('C*',$length,0x00,0x04);
  $bindata .= "host";
  $bindata .= $ipaddr;
 
  // Keep password (1/0 to indicate on/off)
  $bindata .= pack('C*',0x0A,0x00,0x08);
  $bindata .= "keep-pwd";
  if ($keeppass == 1){
    $keeppass = pack('C*',0x01);
  } else {
    $keeppass = pack('C*',0x00);
  }
  $bindata .= $keeppass;

  // Login
  $length = (strlen($username) + 6);
  $bindata .= pack('C*',$length,0x00,0x05);
  $bindata .= "login";
  $bindata .= $username;

  // Note
  $length = (strlen($note) + 5);
  $bindata .= pack('C*',$length,0x00,0x04);
  $bindata .= "note";
  $bindata .= $note;

  // Password
  $length = (strlen($password) + 4);
  $bindata .= pack('C*',$length,0x00,0x03);
  $bindata .= "pwd";
  $bindata .= $password;

  // Secure mode (1/0 to indicate on/off)
  $bindata .= pack('C*',0x0D,0x00,0x0B);
  $bindata .= "secure-mode";
  if ($securemode == 1){
    $securemode = pack('C*',0x01);
  } else {
    $securemode = pack('C*',0x00);
  }
  $bindata .= $securemode;

  // Type (Note sure what this one is for)
  $bindata .= pack('C*',0x09,0x00,0x04);
  $bindata .= "type";
  $bindata .= "addr";

}

//End of file only needs placed once as well
$bindata .= pack('C*',0x00,0x00);

// Output the resulting data
file_put_contents($outfile, $bindata);
?>
 
User avatar
surfnet
Member Candidate
Member Candidate
Posts: 277
Joined: Wed Sep 01, 2004 6:38 pm

Re: Feature request - Winbox import from csv

Fri Jul 28, 2017 1:20 am

Hi
I'm trying to use your php script.
I keep getting an error
Parse error: syntax error, unexpected T_STRING in C:\php\winbox.php on line 45

I'm just trying with a single line CSV right now
I searched for that error and it talks about apostrophes
So I have tried the CSV 3 ways and they all fail with the same error

'group1','172.16.130.14','admin','blank',1,1,'Airport'

"group1","172.16.130.14","admin","blank",1,1,"Airport"

group1,172.16.130.14,admin,blank,1,1,Airport


any ideas on the correct csv format would be most helpful.
thanks
 
Sob
Forum Guru
Forum Guru
Posts: 9119
Joined: Mon Apr 20, 2009 9:11 pm

Re: Feature request - Winbox import from csv

Fri Jul 28, 2017 2:25 am

That error is not caused by csv data, it's something wrong in script. But the script is correct, so make sure you copied and saved it correctly and didn't change it by mistake.

Expected csv format is one of following (can be even mixed like this):
"Group 1","10.0.0.1","user1","pass1",1,1,"note 1"
Group 2,10.0.0.2,user2,pass2,0,1,note 2

Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot], raphaps and 71 guests