Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. import Tkinter as tk
  2. import paramiko
  3. import time
  4.  
  5.  
  6. class Application(tk.Tk):
  7. def __init__(self, *args, **kwargs):
  8. tk.Tk.__init__(self, *args, **kwargs)
  9.  
  10. container = tk.Frame(self)
  11. container.grid(row=0, column=0, sticky="nsew")
  12. container.grid_rowconfigure(0, weight=1)
  13. container.grid_columnconfigure(0, weight=1)
  14.  
  15. self.frames = {}
  16. for F in (Login, MainWindow):
  17. frame = F(container, self)
  18. self.frames[F] = frame
  19. frame.grid(row=0, column=0, sticky="nsew")
  20.  
  21. self.show_frame(Login)
  22.  
  23. def show_frame(self, cont):
  24. frame = self.frames[cont]
  25. frame.tkraise()
  26.  
  27.  
  28. class Login(tk.Frame):
  29. def __init__(self, parent, controller):
  30. tk.Frame.__init__(self, parent)
  31. self.ssh_client = paramiko.SSHClient()
  32. self.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  33.  
  34. self.parent = parent
  35. self.controller = controller
  36. self.grid(row=0, column=0)
  37.  
  38. self.user = tk.StringVar()
  39. self.user.set("my_username") # Default user
  40.  
  41. self.host_options = ["host1", "host2"]
  42. self.host = tk.StringVar()
  43. self.host.set(self.host_options[0]) # Default hostname
  44.  
  45. l_user = tk.Label(self, text="Username: ")
  46. l_user.grid(row=0, column=0, sticky=tk.E)
  47.  
  48. self.entry_user = tk.Entry(self)
  49. self.entry_user.grid(row=0, column=1, sticky=tk.W)
  50. self.entry_user.insert(0, self.user.get())
  51.  
  52. l_pwd = tk.Label(self, text="Password: ")
  53. l_pwd.grid(row=1, column=0, sticky=tk.E)
  54.  
  55. self.entry_pwd = tk.Entry(self, show="*")
  56. self.entry_pwd.grid(row=1, column=1, sticky=tk.W)
  57.  
  58. l_host = tk.Label(self, text="Hostname: ")
  59. l_host.grid(row=2, column=0, sticky=tk.E)
  60.  
  61. optionmenu_host = tk.OptionMenu(self, self.host, *self.host_options)
  62. optionmenu_host.grid(row=2, column=1, sticky=tk.W)
  63.  
  64. b_login = tk.Button(self, text="Log in", command=self.authorize)
  65. b_login.grid(row=3, column=0, sticky=tk.W)
  66.  
  67. b_quit = tk.Button(self, text="Quit", command=self.parent.destroy)
  68. b_quit.grid(row=4, column=0, sticky=tk.W)
  69.  
  70. def authorize(self):
  71. try:
  72. self.ssh_client.connect(hostname=self.host.get(), username=self.entry_user.get(), password=self.entry_pwd.get())
  73. self.controller.show_frame(MainWindow)
  74. except paramiko.AuthenticationException:
  75. l_error = tk.Label(self, text="Login failed...", fg="red")
  76. l_error.grid(row=4, column=1, sticky=tk.W)
  77. l_error.after(2000, l_error.destroy)
  78.  
  79.  
  80. class MainWindow(tk.Frame):
  81. def __init__(self, parent, controller):
  82. tk.Frame.__init__(self, parent)
  83. self.grid(row=0, column=0)
  84.  
  85. l = tk.Label(self, text="Log in was successful!")
  86. l.grid(row=0, column=0, sticky=tk.W)
  87.  
  88.  
  89. ###################################
  90. # run application
  91. if __name__ == "__main__":
  92. app = Application()
  93. app.mainloop()
  94. ###################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement