-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaws_ssh_client.py
More file actions
584 lines (489 loc) Β· 19.6 KB
/
aws_ssh_client.py
File metadata and controls
584 lines (489 loc) Β· 19.6 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#!/usr/bin/env python3
"""
AWS SSH Directory Access Script
Connects to AWS instance and performs directory operations
"""
import paramiko
import os
import sys
import argparse
from pathlib import Path
import logging
from typing import Optional, List, Dict, Any
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
class AWSSSHClient:
def __init__(self, hostname: str, username: str, key_path: str, port: int = 22):
"""
Initialize SSH client for AWS connection
Args:
hostname: AWS instance IP or hostname
username: SSH username
key_path: Path to SSH private key
port: SSH port (default 22)
"""
self.hostname = hostname
self.username = username
self.key_path = Path(key_path).expanduser()
self.port = port
self.client = None
self.sftp = None
def __enter__(self):
"""Context manager entry"""
if self.connect():
return self
else:
raise Exception("Failed to establish SSH connection")
def __exit__(self, exc_type, exc_val, exc_tb):
"""Context manager exit"""
self.close()
def connect(self) -> bool:
"""
Establish SSH connection to AWS instance
Returns:
bool: True if connection successful, False otherwise
"""
try:
# Create SSH client
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Load private key
if not self.key_path.exists():
logger.error(f"SSH key not found: {self.key_path}")
return False
private_key = paramiko.RSAKey.from_private_key_file(str(self.key_path))
# Connect
logger.info(f"Connecting to {self.hostname}...")
self.client.connect(
hostname=self.hostname,
port=self.port,
username=self.username,
pkey=private_key,
timeout=30
)
# Create SFTP client for file operations
self.sftp = self.client.open_sftp()
logger.info("Successfully connected to AWS instance")
return True
except Exception as e:
logger.error(f"Failed to connect: {str(e)}")
return False
def execute_command(self, command: str) -> Dict[str, Any]:
"""
Execute command on remote server
Args:
command: Command to execute
Returns:
dict: Contains stdout, stderr, and exit_code
"""
if not self.client:
raise Exception("Not connected to server")
try:
logger.info(f"Executing: {command}")
stdin, stdout, stderr = self.client.exec_command(command)
exit_code = stdout.channel.recv_exit_status()
stdout_data = stdout.read().decode('utf-8')
stderr_data = stderr.read().decode('utf-8')
return {
'stdout': stdout_data,
'stderr': stderr_data,
'exit_code': exit_code
}
except Exception as e:
logger.error(f"Command execution failed: {str(e)}")
return {'stdout': '', 'stderr': str(e), 'exit_code': -1}
def list_directory(self, path: str = '/') -> List[Dict[str, Any]]:
"""
List directory contents with detailed information
Args:
path: Directory path to list
Returns:
list: List of file/directory information
"""
try:
# Execute ls -lrth command
result = self.execute_command(f"ls -lrth {path}")
if result['exit_code'] != 0:
logger.error(f"Failed to list directory: {result['stderr']}")
return []
files = []
lines = result['stdout'].strip().split('\n')
# Skip the total line if present
if lines and lines[0].startswith('total'):
lines = lines[1:]
for line in lines:
if line.strip():
parts = line.split()
if len(parts) >= 9:
file_info = {
'permissions': parts[0],
'links': parts[1],
'owner': parts[2],
'group': parts[3],
'size': parts[4],
'month': parts[5],
'day': parts[6],
'time': parts[7],
'name': ' '.join(parts[8:])
}
files.append(file_info)
return files
except Exception as e:
logger.error(f"Failed to list directory: {str(e)}")
return []
def get_file_content(self, remote_path: str) -> Optional[str]:
"""
Read file content from remote server
Args:
remote_path: Path to file on remote server
Returns:
str: File content or None if failed
"""
try:
with self.sftp.open(remote_path, 'r') as file:
content = file.read()
return content
except Exception as e:
logger.error(f"Failed to read file {remote_path}: {str(e)}")
return None
def download_file(self, remote_path: str, local_path: str) -> bool:
"""
Download file from remote server
Args:
remote_path: Path to file on remote server
local_path: Local path to save file
Returns:
bool: True if successful, False otherwise
"""
try:
logger.info(f"Downloading {remote_path} to {local_path}")
self.sftp.get(remote_path, local_path)
logger.info("Download completed")
return True
except Exception as e:
logger.error(f"Download failed: {str(e)}")
return False
def upload_file(self, local_path: str, remote_path: str) -> bool:
"""
Upload file to remote server
Args:
local_path: Local file path
remote_path: Remote path to save file
Returns:
bool: True if successful, False otherwise
"""
try:
logger.info(f"Uploading {local_path} to {remote_path}")
self.sftp.put(local_path, remote_path)
logger.info("Upload completed")
return True
except Exception as e:
logger.error(f"Upload failed: {str(e)}")
return False
def change_directory(self, path: str) -> bool:
"""
Change current directory on remote server
Args:
path: Directory path
Returns:
bool: True if successful, False otherwise
"""
try:
result = self.execute_command(f"cd {path} && pwd")
if result['exit_code'] == 0:
logger.info(f"Changed directory to: {result['stdout'].strip()}")
return True
else:
logger.error(f"Failed to change directory: {result['stderr']}")
return False
except Exception as e:
logger.error(f"Failed to change directory: {str(e)}")
return False
def get_system_info(self) -> Dict[str, str]:
"""
Get system information from remote server
Returns:
dict: System information
"""
info = {}
commands = {
'hostname': 'hostname',
'uptime': 'uptime',
'disk_usage': 'df -h /',
'memory': 'free -h',
'cpu_info': 'lscpu | head -10',
'current_dir': 'pwd',
'user': 'whoami'
}
for key, command in commands.items():
result = self.execute_command(command)
if result['exit_code'] == 0:
info[key] = result['stdout'].strip()
else:
info[key] = f"Error: {result['stderr']}"
return info
def get_directory_summary(self, path: str) -> Dict[str, Any]:
"""
Get summary information about a directory
Args:
path: Directory path to analyze
Returns:
dict: Directory summary with file count, total size, etc.
"""
try:
files = self.list_directory(path)
if not files:
return {"path": path, "accessible": False, "error": "Directory empty or access denied"}
total_files = len(files)
total_dirs = len([f for f in files if f['permissions'].startswith('d')])
total_regular_files = total_files - total_dirs
# Calculate total size (only for files with numeric sizes)
total_size = 0
size_unit = "bytes"
for file_info in files:
size_str = file_info['size']
if size_str.replace('.', '').isdigit():
# Handle sizes like "1.2K", "3.4M", etc.
if size_str.endswith('K'):
total_size += float(size_str[:-1]) * 1024
elif size_str.endswith('M'):
total_size += float(size_str[:-1]) * 1024 * 1024
elif size_str.endswith('G'):
total_size += float(size_str[:-1]) * 1024 * 1024 * 1024
else:
total_size += float(size_str)
# Convert to human readable format
if total_size > 1024 * 1024 * 1024:
total_size = f"{total_size / (1024 * 1024 * 1024):.1f}GB"
elif total_size > 1024 * 1024:
total_size = f"{total_size / (1024 * 1024):.1f}MB"
elif total_size > 1024:
total_size = f"{total_size / 1024:.1f}KB"
else:
total_size = f"{int(total_size)} bytes"
return {
"path": path,
"accessible": True,
"total_items": total_files,
"directories": total_dirs,
"files": total_regular_files,
"total_size": total_size,
"largest_files": sorted(files, key=lambda x: self._parse_size(x['size']), reverse=True)[:3]
}
except Exception as e:
return {"path": path, "accessible": False, "error": str(e)}
def _parse_size(self, size_str: str) -> float:
"""Parse size string to bytes for sorting"""
if not size_str or not size_str.replace('.', '').replace('K', '').replace('M', '').replace('G', '').isdigit():
return 0
if size_str.endswith('K'):
return float(size_str[:-1]) * 1024
elif size_str.endswith('M'):
return float(size_str[:-1]) * 1024 * 1024
elif size_str.endswith('G'):
return float(size_str[:-1]) * 1024 * 1024 * 1024
else:
return float(size_str) if size_str.replace('.', '').isdigit() else 0
def get_ssh_config():
"""Get SSH configuration from .env file, command line args, or prompt for missing values"""
# Set up argument parser
parser = argparse.ArgumentParser(
description="Connect to AWS instance via SSH and perform directory operations",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Configuration priority (highest to lowest):
1. Command line arguments
2. .env file variables
3. Interactive prompts for missing values
.env file variables:
AWS_SSH_HOST - AWS instance hostname/IP
AWS_SSH_USER - SSH username (default: forge)
AWS_SSH_KEY - Path to SSH private key
AWS_SSH_PORT - SSH port (default: 22)
Examples:
python aws_ssh_client.py # Use .env file + prompts
python aws_ssh_client.py --key ~/.ssh/key # Override .env SSH key
python aws_ssh_client.py --verbose # Enable debug logging
"""
)
parser.add_argument(
"--host", "-H",
help="Override AWS instance hostname/IP from .env",
default=None
)
parser.add_argument(
"--user", "-u",
help="Override SSH username from .env",
default=None
)
parser.add_argument(
"--key", "-k",
help="Override SSH private key path from .env",
default=None
)
parser.add_argument(
"--port", "-p",
type=int,
help="Override SSH port from .env",
default=None
)
parser.add_argument(
"--verbose", "-v",
action="store_true",
help="Enable verbose logging"
)
args = parser.parse_args()
# Set logging level
if args.verbose:
logging.getLogger().setLevel(logging.DEBUG)
# Get values with priority: CLI args > .env file > defaults
host = args.host or os.getenv("AWS_SSH_HOST")
user = args.user or os.getenv("AWS_SSH_USER", "forge")
key = args.key or os.getenv("AWS_SSH_KEY")
port = args.port or int(os.getenv("AWS_SSH_PORT", "22"))
# Check for required values and prompt if missing
if not host:
logger.warning("AWS_SSH_HOST not found in .env file")
host = input("Enter AWS hostname/IP address: ").strip()
if not host:
logger.error("Hostname is required")
sys.exit(1)
if not key:
logger.warning("AWS_SSH_KEY not found in .env file")
# Try to find common SSH key locations
common_keys = [
"~/.ssh/id_rsa",
"~/.ssh/id_ed25519",
"~/.ssh/aws-key-2025",
"~/.ssh/id_ecdsa"
]
found_key = None
for key_path in common_keys:
expanded_path = Path(key_path).expanduser()
if expanded_path.exists():
found_key = str(expanded_path)
break
if found_key:
use_found = input(f"Found SSH key at {found_key}. Use this key? [Y/n]: ").strip().lower()
if use_found in ['', 'y', 'yes']:
key = found_key
if not key:
key = input("Enter path to SSH private key: ").strip()
if not key:
logger.error("SSH key path is required")
sys.exit(1)
# Create a simple config object
class Config:
def __init__(self, host, user, key, port):
self.host = host
self.user = user
self.key = key
self.port = port
return Config(host, user, key, port)
def main():
"""Main function to demonstrate AWS SSH operations"""
# Get configuration
config = get_ssh_config()
print(f"\nConnecting to {config.user}@{config.host}:{config.port}")
print(f"Using SSH key: {config.key}")
print("-" * 50)
# Create SSH client
ssh_client = AWSSSHClient(config.host, config.user, config.key, config.port)
try:
# Connect to server
if not ssh_client.connect():
logger.error("Failed to establish connection")
sys.exit(1)
# Get system information
print("\n=== System Information ===")
system_info = ssh_client.get_system_info()
for key, value in system_info.items():
print(f"{key.upper()}: {value}")
# List home directory
print("\n=== Home Directory Contents ===")
home_files = ssh_client.list_directory("/home/forge")
for file_info in home_files:
print(f"{file_info['permissions']} {file_info['owner']} {file_info['group']} "
f"{file_info['size']} {file_info['month']} {file_info['day']} "
f"{file_info['time']} {file_info['name']}")
# List root directory
print("\n=== Root Directory Contents ===")
root_files = ssh_client.list_directory("/")
for file_info in root_files[:10]: # Show first 10 items
print(f"{file_info['permissions']} {file_info['owner']} {file_info['group']} "
f"{file_info['size']} {file_info['month']} {file_info['day']} "
f"{file_info['time']} {file_info['name']}")
# Check specific directories from .env configuration
directories_env = os.getenv("AWS_SSH_DIRECTORIES", "")
if directories_env:
directories_to_check = [dir.strip() for dir in directories_env.split(",") if dir.strip()]
else:
# Default directories if not specified in .env
directories_to_check = [
f"/home/{config.user}",
"/var/log",
"/opt"
]
for directory in directories_to_check:
print(f"\n=== Directory Analysis: {directory} ===")
summary = ssh_client.get_directory_summary(directory)
if summary['accessible']:
print(f"π Total items: {summary['total_items']}")
print(f"π Directories: {summary['directories']}")
print(f"π Files: {summary['files']}")
print(f"πΎ Total size: {summary['total_size']}")
if summary['largest_files']:
print("π Largest files:")
for file_info in summary['largest_files']:
print(f" {file_info['name']} ({file_info['size']})")
# Show first few items for detailed view
dir_contents = ssh_client.list_directory(directory)
if dir_contents:
print(f"π Recent items (showing first 5):")
for file_info in dir_contents[:5]:
file_type = "π" if file_info['permissions'].startswith('d') else "π"
print(
f" {file_type} {file_info['name']} - {file_info['size']} ({file_info['month']} {file_info['day']} {file_info['time']})")
else:
print(f"β {summary['error']}")
# Example: Execute custom commands
print("\n=== Docker Containers (if running) ===")
docker_result = ssh_client.execute_command("docker ps 2>/dev/null || echo 'Docker not available'")
if docker_result['exit_code'] == 0 and 'CONTAINER ID' in docker_result['stdout']:
print("π³ Running Docker containers:")
print(docker_result['stdout'])
else:
print("π Docker not running or not installed")
# Show system resources
print("\n=== System Resources ===")
resource_commands = {
"πΎ Disk Usage": "df -h / 2>/dev/null | tail -1",
"π§ Memory Usage": "free -h 2>/dev/null | grep Mem",
"β‘ Load Average": "uptime 2>/dev/null | cut -d',' -f3-",
"π Running Processes": "ps aux --sort=-%cpu | head -5 2>/dev/null || ps aux | head -5"
}
for desc, cmd in resource_commands.items():
result = ssh_client.execute_command(cmd)
if result['exit_code'] == 0 and result['stdout'].strip():
print(f"{desc}: {result['stdout'].strip()}")
else:
print(f"{desc}: Unable to retrieve")
except KeyboardInterrupt:
logger.info("Operation cancelled by user")
except Exception as e:
logger.error(f"Unexpected error: {str(e)}")
logger.debug("Full traceback:", exc_info=True)
finally:
# Always close the connection safely
try:
if 'ssh_client' in locals():
ssh_client.close()
except Exception as e:
logger.debug(f"Error during cleanup: {e}")
pass # Don't fail on cleanup errors
if __name__ == "__main__":
main()