Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Find all files in a share and show old files
- """
- import time
- import sys
- from argparse import ArgumentParser
- from contextlib import closing
- from datetime import datetime as DateTime
- from datetime import timedelta as TimeDelta
- from typing import Union
- # pip3 install pysmb
- from smb.base import SharedDevice, SharedFile
- from smb.smb_structs import OperationFailure
- from smb.SMBConnection import SMBConnection
- def walk(
- connection: SMBConnection,
- share: str,
- path: str,
- date_min: DateTime,
- level: int = 0,
- ) -> None:
- try:
- shared_files = connection.listPath(share, path)
- except OperationFailure:
- return
- for file in shared_files:
- if file.filename == "." or file.filename == "..":
- continue
- name = file.filename
- write = DateTime.fromtimestamp(file.last_write_time)
- if write < date_min:
- print(path, write.isoformat(), sep=",")
- if file.isDirectory:
- walk(connection, share, path + "/" + file.filename, date_min, level + 1)
- def main(host, username, password, date):
- with closing(SMBConnection(username, password, username, username)) as conn:
- conn.connect(host)
- for share in conn.listShares():
- if share.type == SharedDevice.DISK_TREE:
- print(">>", share.name, "<<")
- walk(conn, share.name, "/", date)
- return 0
- def get_args():
- default_date = DateTime.now() - TimeDelta(days=365 * 5)
- parser = ArgumentParser()
- parser.add_argument("host", help="IP Address of smb host")
- parser.add_argument("username", help="username of user")
- parser.add_argument("password", help="password of user")
- parser.add_argument(
- "date",
- type=DateTime.fromisoformat,
- default=default_date,
- nargs="?",
- help="Find files which are younger than given iso-date.",
- )
- return parser.parse_args()
- if __name__ == "__main__":
- args = get_args()
- print(f"Seeking files before {args.date}")
- time.sleep(2)
- try:
- main(**vars(args))
- except KeyboardInterrupt:
- pass
- except Exception as e:
- print(repr(e), sys.stderr)
- sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement