2

I'm trying to perform backup using Paramiko. When backup occurs I let script sleep for 240 seconds hoping it is complete however sometimes this may take longer than this. Is there anyway I can use a loop to keep checking every 20 seconds to see if such keywords such as "backup complete" exist is the output variable.

import datetime, time
from time import sleep
from Tkinter import *
import paramiko
from paramiko_expect import SSHClientInteraction
def backup():
prompt = 'root@servername user'
try:
    client = paramiko.SSHClient()
    client.load_system_host_keys() 
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 

    client.connect(hostname=server, username=user, password=pass)

    interact = SSHClientInteraction(client, timeout=10, display=True)


except paramiko.AuthenticationException:
    print("Authentication failed, please verify your credentials: %s")
except paramiko.SSHException as sshException:
    print("Unable to establish SSH connection: %s" % sshException)
except paramiko.BadHostKeyException as badHostKeyException:
    print("Unable to verify server's host key: %s" % badHostKeyException)
except Exception as e:
    print(e.args)

    interact.send('su')
    interact.expect("Password:")


    interact.send("supassword")
    interact.expect(prompt)


    interact.send("who")
    interact.expect(prompt)

    cmd_output_who = interact.current_output_clean
    print cmd_output_who
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992

1 Answers1

1

SSHClient.invoke_shell is for implementing an interactive terminal sessions (like if you are implementing your own SSH terminal client), not for automating a command execution. A terminal is a black box with input and output. It does not have any API to execute a command and wait for it to complete.

Use SSHClient.exec_command to execute the command and Channel.recv_exit_status or Channel.exit_status_ready to wait for it to complete.
See Wait until task is completed on Remote Machine through Python.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992