Guest User

Untitled

a guest
Dec 7th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. try:
  2. ssh = paramiko.SSHClient()
  3. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  4. ssh.connect(server, port=22, username='user', password='pass')
  5. channel = ssh.invoke_shell()
  6.  
  7. except paramiko.AuthenticationException:
  8. print("Authentication failed, please verify your credentials: %s")
  9. except paramiko.SSHException as sshException:
  10. print("Unable to establish SSH connection: %s" % sshException)
  11. except paramiko.BadHostKeyException as badHostKeyException:
  12. print("Unable to verify server's host key: %s" % badHostKeyException)
  13. except Exception as e:
  14. print(e.args)
  15.  
  16. #array of commands
  17. command = ['command1','command2','command3','command4','command5','command6','command7']
  18. progress["value"] = 0
  19. progress["maximum"] = len(command)
  20. i=0
  21. for commands in command:
  22. if i == 6:
  23. channel.send(command[i]+'n')#display command and press enter
  24. print("**Backup started**")
  25. while not channel.recv_ready():#Wait for the server to read and respond
  26. time.sleep(10)
  27. time.sleep(240)
  28. print("**Backup completed**")
  29. output2 = channel.recv(9999)
  30. print(output2.decode('utf-8'))
  31. bytess += 1
  32. progress["value"] = bytess
  33. txtOutput.insert(END,output2)
  34. txtOutput.update_idletasks()#update the output one line at time
  35. progress.update_idletasks()
  36. else:
  37. channel.send(command[i] + 'n')
  38. print(commands + "command has started**")#display command and press enter
  39. while not channel.recv_ready(): #Wait for the server to read and respond
  40. time.sleep(3)
  41. time.sleep(1)
  42. print(commands + "command has ended**")#wait enough for writing to (hopefully) be finished
  43. output = channel.recv(9999) #read in
  44. print(output.decode('utf-8'))
  45. time.sleep(0.2)
  46. bytess += 1
  47. progress["value"] = bytess
  48. txtOutput.insert(END,output.decode('utf-8'))
  49. txtOutput.update_idletasks()#update the output one line at time
  50. progress.update_idletasks()
  51.  
  52. i+=1
  53.  
  54. print("Completed we will now close connection!!")
  55. ssh.close()
Add Comment
Please, Sign In to add comment