LUA import from csv file script problems

You can share your Lua Scripts with everybody here.

LUA import from csv file script problems

Postby jeroen » Tue Nov 20, 2018 12:03 pm

Dear All,

I was hopping that somebody out here could help me in sorting out my problem.
I have manipulated a bit of LUA script that I found on this forum (thanks!) but I can't seem to get it working....
It imports the users from my csv file but if there are more than 1 entries(lines) it creates the first user correct, but the others as default with no details\information.

What am I doing wrong?

Code: Select all
local csv_path = ""  --the csv file path, run cmd on server: dofile("/")
local domain = ""     --the domain name

--translate csv string to a table
--this is a modified function from <Programming in Lua> Chapter.20
function fromCSV(s)
      s = s .. ','        -- ending comma
      local t = {}        -- table to collect fields
      local fieldstart = 1
      repeat
        -- next field is quoted? (start with `"'?)
        if string.find(s, '^"', fieldstart) then
          local a, c
          local i  = fieldstart
          repeat
            -- find closing quote
            a, i, c = string.find(s, '"("?)', i+1)
          until c ~= '"'    -- quote not followed by quote?
          if not i then error('unmatched "') end
          local f = string.sub(s, fieldstart+1, i-1)
     local nexti = i+1
          table.insert(t, (string.gsub(f, '""', '"')))
          fieldstart = nexti + 1
        else                -- unquoted; find next comma
     local nexti = string.find(s, ',', fieldstart)
     local temp = string.sub(s, fieldstart, nexti-1)

     local n = string.find(temp, '\n', 1)
     if n then
      temp = string.sub(temp, 1, n-1)
      nexti = fieldstart+n-1
     end

          table.insert(t, temp)
          fieldstart = nexti+1
        end
      until fieldstart > string.len(s)
      return t
end


--get the content of csv file
local fp = assert(io.open(csv_path, "r"))
local content = fp:read("*all")
fp:close()

local colnum = 10   --fields number of csv file
local username, password, homedir, attr, adddir, attr2, notename, noteemail, notememo, group
local t = fromCSV(content)
for i, s in ipairs(t) do
   if i % colnum == 1 then
      username = s
   elseif i % colnum == 2 then
      password = s
   elseif i % colnum == 3 then
      homedir = s
   elseif i % colnum == 4 then
      -- get directory attributes split by ',' (no spaces)
      attr = Split(s,",")
      local ii = 1
      for _,v in pairs(attr) do
         if v == "1" then
            attr[ii] = true
         else
            attr[ii] = false
         end
         ii = ii + 1
    end
    elseif i % colnum == 5 then
      adddir = s
   elseif i % colnum == 6 then
      -- get additional directory attributes split by ',' (no spaces)
      attr2 = Split(s,",")
      local ii = 1
      for _,v in pairs(attr2) do
         if v == "1" then
            attr2[ii] = true
         else
            attr2[ii] = false
         end
         ii = ii + 1
    end 
   elseif i % colnum == 7 then
      notename = s
      elseif i % colnum == 8 then
      noteemail = s
      elseif i % colnum == 9 then
      notememo = s     
   elseif i % colnum == 0 then
      groups = {}
      local arrayGroup = Split(s,",")
      for _, strGroup in ipairs(arrayGroup) do
         table.insert(groups,{strGroup})
      end
   end
   
   --not reading the first line
   if i > colnum and i % colnum == 0 then
      -- add user account
      c_AddUser(domain,username, md5(password),32,1,1,0,0,0,5,5,0,0,0,0,0,0,0,0,0,0,0,0,0,notename,0,0,0,0,noteemail,notememo,0,0,0,groups,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
      -- add user directory
      c_AddUserDirectory(domain, username, homedir, '/', true, attr[1], attr[2], attr[3], attr[4], attr[5], attr[6], attr[7], attr[8], attr[9], attr[10], attr[11])
      -- add additional directory
      c_AddUserDirectory(domain, username, adddir, '', false, attr2[1], attr2[2], attr2[3], attr2[4], attr2[5], attr2[6], attr2[7], attr2[8], attr2[9], attr2[10], attr2[11])
   end
end

print("import users successfully!")
jeroen
 
1
 
Wed Aug 17, 2016 1:31 pm

Return to Lua scripts

Who is online

Users browsing this forum: No registered users and 2 guests

cron