Sftp get dirs
Jump to navigation
Jump to search
Simple script to get dirs
.env
export SFTP_HOST=eft-na.wtwco.com export SFTP_USER=PooBear export SFTP_PASS=<MY PASS> export BASE_DIR="/Usr/BDA/" export OUTPUT_FILE="my_end_dir_results.txt"
sudo apt install python3-venv python3 -m venv . ~/.venv . ~/.venv/bin/activate pip install pysftp .env
sftp-get-dirs.py
import pysftp
import os
import sys
import stat # to interpret file types
def list_remote_directories_recursive(sftp, remote_path, log_fn):
"""
Recursively lists all directories under a given remote path,
calling log_fn(path) for each directory found.
"""
try:
with sftp.cd(remote_path):
for item in sftp.listdir_attr('.'):
if stat.S_ISDIR(item.st_mode) and item.filename not in ('.', '..'):
sub_path = f"{sftp.pwd}/{item.filename}"
log_fn(sub_path)
list_remote_directories_recursive(sftp, sub_path, log_fn)
except pysftp.exceptions.PermissionDenied:
print(f"Permission denied to access: {remote_path}")
except pysftp.exceptions.NoSuchFile:
# remote_path was a file, skip it
pass
except Exception as e:
print(f"An error occurred processing '{remote_path}': {e}")
if __name__ == "__main__":
# --- env vars ---
HOSTNAME = os.environ.get("SFTP_HOST")
USERNAME = os.environ.get("SFTP_USER")
PASSWORD = os.environ.get("SFTP_PASS")
BASE_DIR = os.environ.get("BASE_DIR")
OUTPUT_FILE = os.environ.get("OUTPUT_FILE", "directory_list.txt")
PORT = 22 # default SFTP port
if not all([HOSTNAME, USERNAME, PASSWORD, BASE_DIR]):
print("Error: Please set SFTP_HOST, SFTP_USER, SFTP_PASS, and BASE_DIR environment variables.")
sys.exit(1)
sftp_connection = None
try:
# open output file
with open(OUTPUT_FILE, 'w', encoding='utf-8') as output_fh:
def log_dir(path):
print(path)
output_fh.write(path + "\n")
print(f"Saving directory list to: {OUTPUT_FILE}\n")
print(f"Connecting to {HOSTNAME} with user {USERNAME}...")
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None # disable hostkey checking for dev
sftp_connection = pysftp.Connection(
host=HOSTNAME,
username=USERNAME,
password=PASSWORD,
port=PORT,
cnopts=cnopts
)
print("Successfully connected to the SFTP server.\n")
print("Scanning starting from:", BASE_DIR)
log_dir(BASE_DIR) # include the base itself
list_remote_directories_recursive(sftp_connection, BASE_DIR, log_dir)
print("\nScan complete.")
except Exception as e:
print(f"A critical error occurred: {e}")
finally:
if sftp_connection:
sftp_connection.close()
print("SFTP connection closed.")