[SOLVED/WORKAROUND] Toggle samba share through shell?


Recommended Posts

I am trying to turn on/off certain samba shares on a schedule using cron. What's the best command to pass? All I can think of is using sed to edit /etc/samba/smb-shares.conf, but this seems really complicated and would break the webgui options that were set. Is there a command that mimics what the webgui is doing when it turns on/off samba export or changes security options for a given share?

Edited by golli53
solved/workaround
Link to comment
  • 2 weeks later...
1 hour ago, golli53 said:

Couldn't figure this out using the shell, but ended up writing a short Python script to toggle options through HTTP to the webgui. CSRF tokens made it a bit trickier. Can send it out if anyone is also looking for a solution.

So how did you do it? Would be very useful for me to be able to interact with the GUI but I don't know how to do it with Python.

Link to comment
6 hours ago, testdasi said:

So how did you do it? Would be very useful for me to be able to interact with the GUI but I don't know how to do it with Python.

def toggle_unraid_share(url, setting=True, share_name='share'):
    from requests_html import HTMLSession
    session = HTMLSession()
    r = session.get(url)
    r.html.render()
    token = re.search(r"csrf_token=([A-Z0-9]+)", r.html.full_text).groups()[0]
    r2 = session.post(f"{url}/update.htm", 
        data={'shareName': share_name, 'shareExport': 'e' if setting else '-', 'shareSecurity': 'public', 'changeShareSecurity': 'Apply', 'csrf_token': token})
    r2.raise_for_status()
    session.close()

Here's the script in function form. Requires Python 3 and requests_html. Should be fairly easy to generalize it to other settings changes (by changing the data argument in the sessions.post call). URL is your unraid address, incl http(s)://

Edited by golli53
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.