LoginSignup
0
0

More than 5 years have passed since last update.

CentOSからWindows共有フォルダを検索

Last updated at Posted at 2016-01-20

CentOS7 上で pysmbc を利用して共有フォルダを検索した際のメモ

準備

依存するパッケージをインストール

$ yum install -y libsmbclient libsmbclient-devel libffi-devel openldap-devel openssl-devel bzip2-devel zlib-devel python-devel

Python3の環境を作る

pipをインストール。

curl -kL https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python

virtualenvをインストール。

$ pip install virtualenv

pyzhonzをインストール。

$ curl -kL https://raw.github.com/saghul/pythonz/master/pythonz-install | bash

Python3をインストール。

$ pythonz install 3.5.1

virtualenvを作成。

$ virtualenv -p $(pythonz locate 3.5.1) venv

pysmbcをインストール

先程作成したvirtualenv環境にpysmbcをインストールする。

$ source ./venv/bin/activate
$ pip install pysmbc

試してみる

getdents()で取得したリストには'.'と'..'が含まれているので除外する点だけ注意。

test.py
import smbc

settings = dict(
    servername = 'SERVERNAME',
    domain = 'DOMAIN',
    username = 'USERNAME',
    password = 'PASSWORD',
)

# 認証コールバックを準備
cb = lambda context, share, workgroup, username, password: (workgroup,
                                                            settings['domain']+'\\'+settings['username'],
                                                            settings['password'])

c = smbc.Context(auth_fn=cb)


def search(root_dir):
    print(root_dir)
    entries = c.opendir(root_dir).getdents()
    print(len(entries))
    for entry in entries:
        if entry.name in ('.', '..'): # '.'と'..'を除外
            pass
        elif entry.smbc_type in (smbc.DIR, smbc.FILE_SHARE):
            # 下位フォルダを探索
            try:
                uri = root_dir + '/' + entry.name
                search(uri)
            except:
                pass
        elif entry.smbc_type == smbc.FILE:
            print(e.name)

if __name__ == '__main__':
    search('smb://' + settings['servername'])
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0