Я не уверен, что это решит проблему, но именно так я бы заменил использование sleep
на after
.Я разделил цикл for на несколько функций.send_cmd(i)
запустить выполнение команды i и запустить check
, которые периодически проверяют готовность сервера, а затем планируют выборку вывода.
def cms():
canvas = Canvas(page3, relief = FLAT, background = "#D2D2D2", width = 694, height = 120)
canvas.pack()
txtOutput = Text(canvas, wrap = NONE, height =14, width = 86, borderwidth=2)
txtOutput.pack()
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(server, port=22, username='user', password='pass')
channel = ssh.invoke_shell()
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)
command = ['command1','command2','command3','command4','command5','command6','command7','command8']
def send_cmd(i):
if i == 6:
print("**Backup started**")
channel.send(command[i]+'\n')
check2()
else:
print(command[i] + "command has started**")
channel.send(command[i]+'\n')
check1(i)
def check1(i):
if not channel.recv_ready():
txtOutput.after(3000, check1, i) # wait 3s and re-check
else: # server is ready, wait for execution to end and fetch output
txtOutput.after(1000, get, command[i] + "command has ended**", i)
def check2():
if not channel.recv_ready():
txtOutput.after(10000, check2) # wait 3s and re-check
else: # server is ready, wait for execution to end and fetch output
txtOutput.after(240000, get, "**Backup completed**", 6)
def get(msg, i):
print(msg)
output = channel.recv(9999) #read in
print(output.decode('utf-8'))
txtOutput.insert(END, output.decode('utf-8'))
if i < len(command) - 1:
send_cmd(i + 1) # launch next command
else: # no more command to launch, close ssh
ssh.close()
send_cmd(0)