import os
import ShellExe
class SmbClient:
def __init__(self, url: str, name: str, pwd: str, group: str):
self.url = url
if not self.url.endswith("/"):
self.url = url + "/"
self.name = name
self.pwd = pwd
self.group = group
def ls_dir(self, path: str):
print("%s%%%s" % (self.name, self.pwd))
cmd = 'smbclient %s ' \
'-U %s%%%s ' \
'-W %s ' \
'-c "cd %s;ls"' % (self.url, self.name, self.pwd, self.group, path)
print("cmd: " + cmd)
logs = ShellExe.exe(cmd)
# return logs.splitlines(False)
result = []
for line in logs.splitlines(False):
if not (line.startswith(" .") or line.startswith('\t\t')):
line_trim: str = line.strip()
line_split = line_trim.split(" ")[0]
if line_split != '':
result.append(line_split)
return result
# smb://192.168.1.1/share/test.txt
# //192.168.1.1/share/test.txt
# test.txt
def download(self, url: str, local_path: str):
exp_local_save_path = os.path.expanduser(local_path)
print("want_download_url: " + url)
print("exp_local_save_path0: " + exp_local_save_path)
if url.startswith("smb:"):
url = url[url.find("smb:") + 4:]
print("want_download_url1: " + url)
if url.startswith(self.url):
url = url[url.find(self.url) + len(self.url):]
print("want_download_url2: " + url)
if url.endswith("/"):
url = url[:url.rfind("/")]
print("want_download_url3: " + url)
cd_to_download = url[:url.rfind("/")]
print("parent: " + cd_to_download)
mget_name = url[url.rfind("/") + 1:]
print("mkdir name: " + mget_name)
download_cmd = """smbclient %s -U %s%%%s -W %s -c 'prompt OFF;recurse ON;cd '%s';lcd '%s';mget %s'""" \
% (self.url, self.name, self.pwd, self.group, cd_to_download, exp_local_save_path, mget_name)
logs = ShellExe.exe(download_cmd)
print(logs)
if __name__ == "__main__":
client = SmbClient("//192.168.1.1/share/", "", "", "group.com")
print(client.download(
"smb://192.168.1.1/share/test.txt",
"~/Desktop/tmp/"))
smbclient python
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 今天青石的票圈出镜率最高的,莫过于张艺谋的新片终于定档了。 一张满溢着水墨风的海报一次次的出现在票圈里,也就是老谋...