File transfer complete
This commit is contained in:
83
ai_mgmt.py
83
ai_mgmt.py
@@ -1,32 +1,36 @@
|
||||
import socket
|
||||
from pathlib import Path
|
||||
from smb.SMBConnection import SMBConnection
|
||||
|
||||
SERVER = "192.168.45.4"
|
||||
SHARE = "AGILENT"
|
||||
USERNAME = "" # leave empty for guest/anonymous
|
||||
USERNAME = ""
|
||||
PASSWORD = ""
|
||||
CLIENT_NAME = socket.gethostname()
|
||||
SERVER_NAME = SERVER # use IP for UNC path in TREE_CONNECT
|
||||
SERVER_NAME = SERVER
|
||||
|
||||
DATA_DIR = Path(__file__).parent / "data"
|
||||
|
||||
|
||||
def list_csv_files(server, share, username="", password=""):
|
||||
"""Search an SMB1 share recursively and return all .csv file paths."""
|
||||
def _connect():
|
||||
conn = SMBConnection(
|
||||
username, password,
|
||||
USERNAME, PASSWORD,
|
||||
CLIENT_NAME, SERVER_NAME,
|
||||
use_ntlm_v2=True,
|
||||
is_direct_tcp=True # use port 445 directly
|
||||
is_direct_tcp=True
|
||||
)
|
||||
if not conn.connect(SERVER, 445):
|
||||
raise ConnectionError("Failed to connect to SMB share.")
|
||||
return conn
|
||||
|
||||
if not conn.connect(server, 445):
|
||||
print("Failed to connect to the share.")
|
||||
return []
|
||||
|
||||
def list_csv_files(server=SERVER, share=SHARE, username=USERNAME, password=PASSWORD):
|
||||
"""Return a list of all .csv paths found on the share (recursive)."""
|
||||
conn = _connect()
|
||||
csv_files = []
|
||||
|
||||
def walk(path):
|
||||
entries = conn.listPath(share, path)
|
||||
for entry in entries:
|
||||
for entry in conn.listPath(share, path):
|
||||
if entry.filename in (".", ".."):
|
||||
continue
|
||||
full_path = f"{path}/{entry.filename}"
|
||||
@@ -43,13 +47,52 @@ def list_csv_files(server, share, username="", password=""):
|
||||
return csv_files
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(f"Searching \\\\{SERVER}\\{SHARE} for CSV files...\n")
|
||||
files = list_csv_files(SERVER, SHARE, USERNAME, PASSWORD)
|
||||
def transfer_csv_files():
|
||||
"""
|
||||
Copy all .csv files from the scope share to DATA_DIR,
|
||||
then delete them from the scope.
|
||||
Returns (copied, failed) counts.
|
||||
"""
|
||||
DATA_DIR.mkdir(exist_ok=True)
|
||||
conn = _connect()
|
||||
copied = 0
|
||||
failed = 0
|
||||
|
||||
if files:
|
||||
print(f"Found {len(files)} CSV file(s):")
|
||||
for f in files:
|
||||
print(f" {f}")
|
||||
else:
|
||||
print("No CSV files found.")
|
||||
try:
|
||||
csv_files = []
|
||||
|
||||
def walk(path):
|
||||
for entry in conn.listPath(SHARE, path):
|
||||
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)
|
||||
|
||||
walk("/")
|
||||
|
||||
for remote_path in csv_files:
|
||||
filename = Path(remote_path).name
|
||||
local_path = DATA_DIR / filename
|
||||
try:
|
||||
with open(local_path, "wb") as f:
|
||||
conn.retrieveFile(SHARE, remote_path, f)
|
||||
conn.deleteFiles(SHARE, remote_path)
|
||||
print(f" TRANSFERRED: {filename}")
|
||||
copied += 1
|
||||
except Exception as e:
|
||||
print(f" FAILED: {filename} — {e}")
|
||||
failed += 1
|
||||
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
return copied, failed
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(f"Transferring CSV files from \\\\{SERVER}\\{SHARE} to {DATA_DIR}...\n")
|
||||
copied, failed = transfer_csv_files()
|
||||
print(f"\nDone. {copied} transferred, {failed} failed.")
|
||||
|
||||
Reference in New Issue
Block a user