-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sftp.py
More file actions
147 lines (124 loc) · 5.07 KB
/
Copy pathtest_sftp.py
File metadata and controls
147 lines (124 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""
SFTP Connection Test Script
Tests connection to BisectHosting SFTP server and locates log files.
"""
import os
import sys
from dotenv import load_dotenv
import paramiko
# Load environment variables
load_dotenv()
SFTP_HOST = os.getenv("SFTP_HOST", "")
SFTP_PORT = os.getenv("SFTP_PORT", "22")
SFTP_USERNAME = os.getenv("SFTP_USERNAME", "")
# Workaround for # character in password
SFTP_PASSWORD_RAW = os.getenv("SFTP_PASSWORD", "")
if SFTP_PASSWORD_RAW and '#' not in SFTP_PASSWORD_RAW and os.path.exists('.sftp_password'):
with open('.sftp_password', 'r') as f:
SFTP_PASSWORD = f.read().strip()
else:
SFTP_PASSWORD = SFTP_PASSWORD_RAW
SFTP_LOG_PATH = os.getenv("SFTP_LOG_PATH", "")
def test_sftp_connection():
"""Test SFTP connection and explore directory structure."""
print("=" * 70)
print("SFTP Connection Test")
print("=" * 70)
if not SFTP_HOST or not SFTP_USERNAME or not SFTP_PASSWORD:
print("\n[ERROR] SFTP credentials not configured in .env")
print("Required: SFTP_HOST, SFTP_USERNAME, SFTP_PASSWORD")
return False
print(f"\n[INFO] Connecting to {SFTP_HOST}:{SFTP_PORT}")
print(f"[INFO] Username: {SFTP_USERNAME}")
try:
# Create SSH client
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect
ssh.connect(
hostname=SFTP_HOST,
port=int(SFTP_PORT),
username=SFTP_USERNAME,
password=SFTP_PASSWORD,
timeout=15
)
print("[OK] SSH connection successful!")
# Open SFTP
sftp = ssh.open_sftp()
print("[OK] SFTP session opened!")
# Get current directory
current_dir = sftp.getcwd() or "/"
print(f"\n[INFO] Current directory: {current_dir}")
# List root directory
print("\n[INFO] Root directory contents:")
try:
items = sftp.listdir('.')
for item in sorted(items):
try:
stat = sftp.stat(item)
item_type = "DIR" if paramiko.sftp_attr.S_ISDIR(stat.st_mode) else "FILE"
print(f" [{item_type}] {item}")
except Exception:
print(f" [?] {item}")
except Exception as e:
print(f"[ERROR] Could not list directory: {e}")
# Check for logs directory
print("\n[INFO] Searching for logs directory...")
log_dirs = ['/logs', 'logs', './logs', '/home/container/logs']
for log_dir in log_dirs:
try:
files = sftp.listdir(log_dir)
print(f"\n[OK] Found logs directory: {log_dir}")
print("[INFO] Contents:")
for f in sorted(files)[:10]: # Show first 10 files
try:
stat = sftp.stat(f"{log_dir}/{f}")
size_mb = stat.st_size / (1024 * 1024)
print(f" {f} ({size_mb:.2f} MB)")
except Exception:
print(f" {f}")
break
except Exception:
continue
else:
print("[WARNING] Could not find logs directory in common locations")
# Test configured log path
if SFTP_LOG_PATH:
print(f"\n[INFO] Testing configured log path: {SFTP_LOG_PATH}")
try:
stat = sftp.stat(SFTP_LOG_PATH)
size_mb = stat.st_size / (1024 * 1024)
print(f"[OK] Log file found! Size: {size_mb:.2f} MB")
# Read last few lines
print("\n[INFO] Reading last 5 lines of log file:")
with sftp.open(SFTP_LOG_PATH, 'r') as f:
f.seek(max(0, stat.st_size - 2000)) # Read last 2KB
lines = f.read().decode('utf-8', errors='ignore').split('\n')
for line in lines[-5:]:
if line.strip():
print(f" {line}")
except FileNotFoundError:
print(f"[ERROR] Log file not found: {SFTP_LOG_PATH}")
print("[INFO] Try one of these paths instead:")
print(" /logs/latest.log")
print(" logs/latest.log")
print(" /home/container/logs/latest.log")
except Exception as e:
print(f"[ERROR] Error reading log file: {e}")
# Cleanup
sftp.close()
ssh.close()
print("\n[OK] SFTP connection test successful!")
return True
except paramiko.AuthenticationException:
print("\n[ERROR] Authentication failed - check username/password")
return False
except paramiko.SSHException as e:
print(f"\n[ERROR] SSH error: {e}")
return False
except Exception as e:
print(f"\n[ERROR] Connection failed: {e}")
return False
if __name__ == "__main__":
success = test_sftp_connection()
sys.exit(0 if success else 1)