bmc_hub/test_sftp.py

71 lines
2.0 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
print("🔌 Connecting to SFTP...")
ssh.connect(
hostname='sftp.acdu.dk',
port=9022,
username='sftp_bmccrm',
password='9,Bg_U9,Bg_U9,Bg_U',
timeout=10
)
sftp = ssh.open_sftp()
print("✅ Connected to SFTP\n")
# List current directory
print(f"📁 Current directory: {sftp.getcwd() or '/'}\n")
print("📂 Files in root:")
for item in sftp.listdir():
try:
stat = sftp.stat(item)
is_dir = stat.st_mode & 0o40000
print(f" {'📁' if is_dir else '📄'} {item} ({stat.st_size} bytes)")
except Exception as e:
print(f"{item} (error: {e})")
# Try to list /backups
print("\n📂 Checking /backups:")
try:
files = sftp.listdir('/backups')
print(f" ✅ /backups exists, contains {len(files)} items")
for f in files[:5]:
print(f" - {f}")
except Exception as e:
print(f" ⚠️ /backups: {e}")
# Try to create it
print("\n Trying to create /backups...")
try:
sftp.mkdir('/backups')
print(" ✅ Created /backups")
except Exception as e2:
print(f" ❌ Cannot create: {e2}")
# Try current directory upload
print("\n📤 Testing upload to current directory...")
test_file = "/tmp/test_upload.txt"
with open(test_file, 'w') as f:
f.write("Test upload from BMC Hub")
try:
sftp.put(test_file, 'test_upload.txt')
print(" ✅ Upload to root successful!")
sftp.remove('test_upload.txt')
print(" ✅ Cleanup successful")
except Exception as e:
print(f" ❌ Upload failed: {e}")
sftp.close()
ssh.close()
print("\n✅ Test complete")
except Exception as e:
print(f"❌ Connection failed: {e}")
import traceback
traceback.print_exc()