Skip to content

Instantly share code, notes, and snippets.

@macsimom
Last active January 16, 2020 14:35
Show Gist options
  • Save macsimom/d16ab9cb0b9513fb551cba11810cfd69 to your computer and use it in GitHub Desktop.
Save macsimom/d16ab9cb0b9513fb551cba11810cfd69 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# Version 0.1
# This was (re)written quick and dirty. And by dirty I mean embarrasing.
import subprocess
import tempfile
import os
import re
dfsroot="//example.com/dfs"
dfsrootserveraddress='dfs01.example.com'
domain='example.com'
myshares = []
tempmountpoint = tempfile.mkdtemp(".nomaddfs")
dfsroot = dfsroot.replace('/'+domain+'/','/'+dfsrootserveraddress+'/')
subprocess.check_output(["mount","-a","-o","nobrowse,rdonly","-t","smbfs",dfsroot,tempmountpoint])
dfsanchors = os.listdir(tempmountpoint)
#print dfsanchors
for dfsanchor in dfsanchors:
anchorinfo = subprocess.check_output(["smbutil","dfs",("%s/%s"%(dfsroot,dfsanchor))])
for line in anchorinfo.split('\n'):
#print line
anchorurl = re.search('Network Address: (.+)',anchorinfo)
if anchorurl:
myshares.append([dfsanchor,"smb:/%s"%anchorurl.group(1),("smb:%s/%s"%(dfsroot,dfsanchor))])
break
#print subprocess.check_output("df")
subprocess.call(["umount",tempmountpoint])
os.rmdir(tempmountpoint)
pliststrings = []
pliststrings.append('<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n<plist version="1.0">')
pliststrings.append( """<dict>
<key>HomeMount</key>
<dict>
<key>Groups</key>
<array>
<string>All</string>
</array>
<key>Mount</key>
<false/>
<key>Options</key>
<array/>
</dict>""")
pliststrings.append( '<key>Shares</key><array>' )
for myshare in myshares:
#print "<dict>\n\t<key>%s</key>\n\t<string>%s</string>\n\t<key>%s</key>\n\t<string>%s</string>\n\t<key>%s</key>\n\t<string>%s</string>\n</dict>"%("Name",myshare[0],"ShareURL",myshare[1],"MountURL",myshare[2])
pliststrings.append( """<dict>
<key>AutoMount</key>
<false/>
<key>ConnectedOnly</key>
<true/>
<key>Groups</key>
<array/>
<key>LocalMount</key>
<string></string>
<key>Name</key>
<string>%s</string>
<key>Options</key>
<array/>
<key>URL</key>
<string>%s</string>
</dict>"""%(myshare[0],myshare[2]))
pliststrings.append( '</array>' )
pliststrings.append( '<key>Version</key><string>1</string></dict>' )
pliststrings.append( '</plist>' )
with open(os.path.expanduser('~/Library/Preferences/menu.nomad.shares.plist'),'w') as outfile:
outfile.write("\n".join(pliststrings))
print "\n".join(pliststrings)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment