linux smb 所有者,python-3.x - 在Linux上使用python从smb共享中获取文件的所有者。 - SO中文参考 - www.soinside.com...
這簡(jiǎn)直不是一件小事,可惜答案并不像我希望的那樣簡(jiǎn)單。
如果以后有人會(huì)被這個(gè)同樣的問(wèn)題困擾,我就把這個(gè)答案貼出來(lái),但希望也許有人能早點(diǎn)貼出更好的解決方案來(lái)
為了找到主人,我用 本庫(kù)及其實(shí)例:from smb.SMBConnection import SMBConnection
conn = SMBConnection(username='', password='', domain=', my_name='', remote_name='')
conn.connect('')
sec_att = conn.getSecurity('', r'\some\file\path')
owner_sid = sec_att.owner
問(wèn)題是 pysmb 包只能給你所有者的SID,而不是他的名字。為了得到他的名字,你需要 做一個(gè)ldap查詢,就像這個(gè)答案一樣。 (轉(zhuǎn)貼代碼)。from ldap3 import Server, Connection, ALL
from ldap3.utils.conv import escape_bytes
s = Server('my_server', get_info=ALL)
c = Connection(s, 'my_user', 'my_password')
c.bind()
binary_sid = b'....' # your sid must be in binary format
c.search('my_base', '(objectsid=' + escape_bytes(binary_sid) + ')', attributes=['objectsid', 'samaccountname'])
print(c.entries)
但當(dāng)然沒(méi)有什么是容易的,我花了好幾個(gè)小時(shí)才找到一種方法 在python中把字符串SID轉(zhuǎn)換為二進(jìn)制SID。最后這就解決了:# posting the needed functions and omitting the class part
def byte(strsid):
'''
Convert a SID into bytes
strdsid - SID to convert into bytes
'''
sid = str.split(strsid, '-')
ret = bytearray()
sid.remove('S')
for i in range(len(sid)):
sid[i] = int(sid[i])
sid.insert(1, len(sid)-2)
ret += longToByte(sid[0], size=1)
ret += longToByte(sid[1], size=1)
ret += longToByte(sid[2], False, 6)
for i in range(3, len(sid)):
ret += cls.longToByte(sid[i])
return ret
def byteToLong(byte, little_endian=True):
'''
Convert bytes into a Python integer
byte - bytes to convert
little_endian - True (default) or False for little or big endian
'''
if len(byte) > 8:
raise Exception('Bytes too long. Needs to be <= 8 or 64bit')
else:
if little_endian:
a = byte.ljust(8, b'\x00')
return struct.unpack('
else:
a = byte.rjust(8, b'\x00')
return struct.unpack('>q', a)[0]
... 最后你有完整的解決方案!享受:(
總結(jié)
以上是生活随笔為你收集整理的linux smb 所有者,python-3.x - 在Linux上使用python从smb共享中获取文件的所有者。 - SO中文参考 - www.soinside.com...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 联想android4.4.2版本,联想Y
- 下一篇: 玉米加农炮怎么用 玉米的适用人群