yeni
This commit is contained in:
@ -16,21 +16,39 @@ class SSHManager:
|
||||
|
||||
def connect(self):
|
||||
"""SSH bağlantısı kur"""
|
||||
import traceback
|
||||
|
||||
try:
|
||||
logger.info(f"SSH bağlantısı başlatılıyor: {self.ssh_credential.hostname}:{self.ssh_credential.port or 22}")
|
||||
|
||||
self.client = paramiko.SSHClient()
|
||||
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
|
||||
# Bağlantı parametrelerini logla
|
||||
logger.info(f"Bağlantı parametreleri:")
|
||||
logger.info(f" Host: {self.ssh_credential.hostname}")
|
||||
logger.info(f" Port: {self.ssh_credential.port or 22}")
|
||||
logger.info(f" Username: {self.ssh_credential.username}")
|
||||
|
||||
# SSH timeout değeri ekle
|
||||
self.client.connect(
|
||||
hostname=self.ssh_credential.hostname,
|
||||
username=self.ssh_credential.username,
|
||||
password=self.ssh_credential.password,
|
||||
port=self.ssh_credential.port or 22,
|
||||
look_for_keys=False,
|
||||
allow_agent=False
|
||||
allow_agent=False,
|
||||
timeout=30 # Timeout değeri ekle
|
||||
)
|
||||
|
||||
logger.info(f"SSH bağlantısı başarıyla kuruldu: {self.ssh_credential.hostname}")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
error_details = traceback.format_exc()
|
||||
logger.error(f'SSH bağlantı hatası: {str(e)}')
|
||||
return False
|
||||
logger.error(f'SSH bağlantı hata detayları:\n{error_details}')
|
||||
raise Exception(f"SSH bağlantısı kurulamadı: {str(e)}")
|
||||
|
||||
def close(self):
|
||||
"""SSH bağlantısını kapat"""
|
||||
@ -52,12 +70,33 @@ class SSHManager:
|
||||
"""
|
||||
SSH üzerinden komut çalıştır ve sonuçları döndür
|
||||
"""
|
||||
import traceback
|
||||
|
||||
try:
|
||||
if not self.client:
|
||||
self.connect()
|
||||
logger.info(f"SSH komutu çalıştırılıyor: {command}")
|
||||
|
||||
stdin, stdout, stderr = self.client.exec_command(command)
|
||||
if not self.client:
|
||||
logger.info("SSH istemcisi yok, yeniden bağlanılıyor...")
|
||||
self.connect()
|
||||
|
||||
if not self.client:
|
||||
raise Exception("SSH istemcisi oluşturulamadı")
|
||||
|
||||
logger.info("Komut çalıştırılıyor...")
|
||||
stdin, stdout, stderr = self.client.exec_command(command, timeout=120) # Timeout ekle
|
||||
logger.info("Komut çalıştırıldı, çıkış kodu bekleniyor...")
|
||||
|
||||
# Timeout ile çıkış kodu bekle
|
||||
import select
|
||||
channel = stdout.channel
|
||||
status_ready = select.select([channel], [], [], 120) # 120 saniye timeout
|
||||
|
||||
if not status_ready[0]:
|
||||
logger.error("Komut zaman aşımına uğradı!")
|
||||
raise Exception("Komut zaman aşımına uğradı")
|
||||
|
||||
exit_status = stdout.channel.recv_exit_status()
|
||||
logger.info(f"Komut çıkış kodu: {exit_status}")
|
||||
|
||||
# Binary veriyi oku
|
||||
stdout_data = stdout.read()
|
||||
|
||||
Reference in New Issue
Block a user