linux smb 所有者,python-3.x - 在Linux上使用python从smb共享中获取文件的所有者。 - SO中文参考 - www.soinside.com...

这简直不是一件小事,可惜答案并不像我希望的那样简单。

如果以后有人会被这个同样的问题困扰,我就把这个答案贴出来,但希望也许有人能早点贴出更好的解决方案来

为了找到主人,我用 本库及其实例: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

问题是 pysmb 包只能给你所有者的SID,而不是他的名字。为了得到他的名字,你需要 做一个ldap查询,就像这个答案一样。 (转贴代码)。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)

但当然没有什么是容易的,我花了好几个小时才找到一种方法 在python中把字符串SID转换为二进制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]

... 最后你有完整的解决方案!享受:(

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值