56 lines
1.5 KiB
Python
56 lines
1.5 KiB
Python
|
|
import socket
|
||
|
|
from smb.SMBConnection import SMBConnection
|
||
|
|
|
||
|
|
SERVER = "192.168.45.4"
|
||
|
|
SHARE = "AGILENT"
|
||
|
|
USERNAME = "" # leave empty for guest/anonymous
|
||
|
|
PASSWORD = ""
|
||
|
|
CLIENT_NAME = socket.gethostname()
|
||
|
|
SERVER_NAME = SERVER # use IP for UNC path in TREE_CONNECT
|
||
|
|
|
||
|
|
|
||
|
|
def list_csv_files(server, share, username="", password=""):
|
||
|
|
"""Search an SMB1 share recursively and return all .csv file paths."""
|
||
|
|
conn = SMBConnection(
|
||
|
|
username, password,
|
||
|
|
CLIENT_NAME, SERVER_NAME,
|
||
|
|
use_ntlm_v2=True,
|
||
|
|
is_direct_tcp=True # use port 445 directly
|
||
|
|
)
|
||
|
|
|
||
|
|
if not conn.connect(server, 445):
|
||
|
|
print("Failed to connect to the share.")
|
||
|
|
return []
|
||
|
|
|
||
|
|
csv_files = []
|
||
|
|
|
||
|
|
def walk(path):
|
||
|
|
entries = conn.listPath(share, path)
|
||
|
|
for entry in entries:
|
||
|
|
if entry.filename in (".", ".."):
|
||
|
|
continue
|
||
|
|
full_path = f"{path}/{entry.filename}"
|
||
|
|
if entry.isDirectory:
|
||
|
|
walk(full_path)
|
||
|
|
elif entry.filename.lower().endswith(".csv"):
|
||
|
|
csv_files.append(full_path)
|
||
|
|
|
||
|
|
try:
|
||
|
|
walk("/")
|
||
|
|
finally:
|
||
|
|
conn.close()
|
||
|
|
|
||
|
|
return csv_files
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
print(f"Searching \\\\{SERVER}\\{SHARE} for CSV files...\n")
|
||
|
|
files = list_csv_files(SERVER, SHARE, USERNAME, PASSWORD)
|
||
|
|
|
||
|
|
if files:
|
||
|
|
print(f"Found {len(files)} CSV file(s):")
|
||
|
|
for f in files:
|
||
|
|
print(f" {f}")
|
||
|
|
else:
|
||
|
|
print("No CSV files found.")
|